susy_cfa  b611ccad937ea179f86a1f5663960264616c0a20
example_converter.cxx
Go to the documentation of this file.
1 #include <cstdlib>
2 
3 #include <iostream>
4 #include <string>
5 
6 #include "TFile.h"
7 #include "TDirectory.h"
8 #include "TTree.h"
9 
10 using namespace std;
11 
12 int main(int argc, char *argv[]){
13  if(argc != 3){
14  cerr << "Error: 2 arguments required." << endl;
15  cerr << "Usage: example_converter.exe input_file_name output_file_name" << endl;
16  return EXIT_FAILURE;
17  }
18 
19  if(string(argv[1]) == string(argv[2])){
20  cerr << "Input and output file are the same: " << argv[1] << '.' << endl;
21  return EXIT_FAILURE;
22  }
23 
24  TFile in_file(argv[1], "read");
25  if(in_file.IsZombie() || !in_file.IsOpen()){
26  cerr << "Could not open input file " << argv[1] << '.' << endl;
27  return EXIT_FAILURE;
28  }
29 
30  string cfa = "configurableAnalysis";
31  TDirectory *dir = static_cast<TDirectory*>(in_file.Get(cfa.c_str()));
32  if(!dir){
33  cfa = "cfA";
34  dir = static_cast<TDirectory*>(in_file.Get(cfa.c_str()));
35  if(!dir){
36  cerr << "File " << argv[1] << " has nither a cfA nor a configurableAnalysis TDirectory." << endl;
37  return EXIT_FAILURE;
38  }
39  }
40 
41  TTree *event_a = static_cast<TTree*>(dir->Get("eventA"));
42  if(!event_a){
43  cerr << "Could not find eventA in subdirectory " << cfa << " in file " << argv[1] << '.' << endl;
44  return EXIT_FAILURE;
45  }
46 
47  TTree *event_b = static_cast<TTree*>(dir->Get("eventB"));
48  if(!event_b){
49  cerr << "Could not find eventB in subdirectory " << cfa << " in file " << argv[1] << '.' << endl;
50  return EXIT_FAILURE;
51  }
52 
53  TTree *event_a_out = event_a->CloneTree(0);
54  if(!event_a_out){
55  cerr << "Could not clone eventA." << endl;
56  return EXIT_FAILURE;
57  }
58 
59  TTree *event_b_out = event_b->CloneTree(0);
60  if(!event_b_out){
61  cerr << "Could not clone eventB." << endl;
62  return EXIT_FAILURE;
63  }
64 
65  TFile out_file(argv[2], "recreate");
66  if(out_file.IsZombie() || !out_file.IsOpen()){
67  cerr << "Could not open output file " << argv[2] << '.' << endl;
68  return EXIT_FAILURE;
69  }
70  if(!out_file.cd()){
71  cerr << "Could not cd into " << argv[2] << '.' << endl;
72  return EXIT_FAILURE;
73  }
74 
75  TDirectory *out_dir = out_file.mkdir(cfa.c_str());
76  if(!out_dir){
77  cerr << "Could not create TDirectory " << cfa << '.' << endl;
78  return EXIT_FAILURE;
79  }
80 
81  if(!out_dir->cd()){
82  cerr << "Could not cd into " << cfa << '.' << endl;
83  return EXIT_FAILURE;
84  }
85 
86  if(!event_a_out->Write()){
87  cerr << "Could not write eventA." << endl;
88  return EXIT_FAILURE;
89  }
90 
91  if(!event_b_out->Write()){
92  cerr << "Could not write eventB." << endl;
93  return EXIT_FAILURE;
94  }
95 
96  out_file.Close();
97  in_file.Close();
98 
99  return EXIT_SUCCESS;
100 }
int main(int argc, char *argv[])
STL namespace.
Definition: cfa.hpp:12