2 =================================================
3 * @file clitkMergeRootFiles.cxx
4 * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
9 =================================================*/
11 #include "clitkMergeRootFiles_ggo.h"
12 #include "clitkCommon.h"
15 #include <TPluginManager.h>
18 #include <TFileMerger.h>
29 typedef std::list<std::string> Strings;
30 typedef std::list<TFile*> Handles;
31 typedef std::set<std::string> StringsSet;
33 void mergeSingleTree(TChain* chain, TFile* output_handle, const std::string& tree_name)
35 assert(chain->FindBranch("runID"));
36 assert(chain->FindBranch("eventID"));
37 assert(chain->FindBranch("time"));
43 chain->SetBranchAddress("eventID",&eventID);
44 chain->SetBranchAddress("runID",&runID);
45 chain->SetBranchAddress("time",&time);
48 TTree* output_tree = chain->CloneTree(0);
50 const long int nentries = chain->GetEntries();
51 for (long int kk=0; kk<nentries; kk++)
54 //cout << kk << "/" << chain->GetTreeNumber() << endl;
62 void mergeGateTree(TChain* chain, TFile* output_handle, const std::string& tree_name)
66 void mergeCoinTree(TChain* chain, TFile* output_handle, const std::string& tree_name)
70 void mergePetRoot(const Strings& input_filenames, const std::string& output_filename)
72 TFile* output_handle = TFile::Open(output_filename.c_str(),"RECREATE");
74 Handles input_handles;
75 StringsSet tree_names;
78 for (Strings::const_iterator iter=input_filenames.begin(); iter!=input_filenames.end(); iter++)
80 TFile* handle = TFile::Open(iter->c_str(),"READ");
83 TIter key_next(handle->GetListOfKeys());
84 while (TKey* key = static_cast<TKey*>(key_next()))
86 const std::string name = key->GetName();
87 const bool is_tree = key->ReadObj()->InheritsFrom("TTree");
88 const bool is_h1 = key->ReadObj()->InheritsFrom("TH1");
89 const bool is_h2 = key->ReadObj()->InheritsFrom("TH2");
90 if (is_tree) tree_names.insert(name);
91 if (is_h1) { h1_names.insert(name); cout << name << "## " << static_cast<TH1D*>(key->ReadObj())->GetMean() << endl; }
92 if (is_h2) h2_names.insert(name);
95 input_handles.push_back(handle);
98 for (Handles::const_iterator iter=input_handles.begin(); iter!=input_handles.end(); iter++)
104 cout << "found " << tree_names.size() << " tree "<< h1_names.size() << " histo1d " << h2_names.size() << " histo2d" << endl;
107 cout << "merging trees" << endl;
108 for (StringsSet::const_iterator iter_tree=tree_names.begin(); iter_tree!=tree_names.end(); iter_tree++)
110 cout << " tree " << *iter_tree;
111 TChain* chain = new TChain(iter_tree->c_str());
112 for (Strings::const_iterator iter_filename=input_filenames.begin(); iter_filename!=input_filenames.end(); iter_filename++)
113 chain->Add(iter_filename->c_str());
114 cout << " nentries " << chain->GetEntries() << endl;
116 if ((*iter_tree) == "Singles" || (*iter_tree) == "Hits") mergeSingleTree(chain,output_handle,*iter_tree);
117 if ((*iter_tree) == "Gate") mergeGateTree(chain,output_handle,*iter_tree);
118 if ((*iter_tree) == "Coincidences") mergeCoinTree(chain,output_handle,*iter_tree);
128 //-----------------------------------------------------------------------------
129 int main(int argc, char * argv[]) {
131 gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo", "*",
132 "TStreamerInfo", "RIO", "TStreamerInfo()");
135 GGO(clitkMergeRootFiles, args_info);
138 if (args_info.input_given < 2) {
139 FATAL("Error, please provide at least two inputs files");
142 { // Detect Pet output
143 bool all_pet_output = true;
144 Strings input_filenames;
145 for (uint i=0; i<args_info.input_given; i++)
147 const char* filename = args_info.input_arg[i];
148 input_filenames.push_back(filename);
149 TFile* handle = TFile::Open(filename,"READ");
151 TTree* hits = dynamic_cast<TTree*>(handle->Get("Hits"));
152 TTree* singles = dynamic_cast<TTree*>(handle->Get("Singles"));
153 const bool is_pet_output = (hits!=NULL) && (singles!=NULL);
154 cout << "testing " << filename << " is_pet_output " << is_pet_output << endl;
157 all_pet_output &= is_pet_output;
159 cout << "all_pet_output " << all_pet_output << endl;
163 mergePetRoot(input_filenames,args_info.output_arg);
170 TFileMerger * merger = new TFileMerger;
171 for (uint i=0; i<args_info.input_given; i++) merger->AddFile(args_info.input_arg[i]);
172 merger->OutputFile(args_info.output_arg);
175 // this is the end my friend
178 //-----------------------------------------------------------------------------