]> Creatis software - clitk.git/commitdiff
variable name set
authorPierre Gueth <pierre.gueth@creatis.insa-lyon.fr>
Mon, 4 Feb 2013 12:18:11 +0000 (13:18 +0100)
committerDavid Sarrut <david.sarrut@creatis.insa-lyon.fr>
Fri, 26 Jul 2013 06:32:31 +0000 (08:32 +0200)
tests_dav/clitkMergeRootFiles.cxx

index 86887f0322526f58c9d12ad5561d52cccebc6efb..5b62d84e7608235360b0459f96a66ae9c23dbce2 100644 (file)
 #include <TROOT.h>
 #include <TPluginManager.h>
 #include <TFile.h>
+#include <TKey.h>
 #include <TFileMerger.h>
 #include <TTree.h>
+#include <TH1.h>
+#include <TH2.h>
 #include <iostream>
+#include <set>
 
 using std::endl;
 using std::cout;
 
+typedef std::list<std::string> Strings;
+typedef std::list<TFile*> Handles;
+typedef std::set<std::string> StringsSet;
+
+void mergePetRoot(const Strings& input_filenames, const std::string& output_filename)
+{
+       TFile* output_handle = TFile::Open(output_filename.c_str(),"RECREATE");
+
+       Handles input_handles;
+       StringsSet tree_names;
+       StringsSet h1_names;
+       StringsSet h2_names;
+       for (Strings::const_iterator iter=input_filenames.begin(); iter!=input_filenames.end(); iter++)
+       {
+               TFile* handle = TFile::Open(iter->c_str(),"READ");
+               assert(handle);
+
+               TIter key_next(handle->GetListOfKeys());
+               while (TKey* key = static_cast<TKey*>(key_next()))
+               {
+                       const std::string name = key->GetName();
+                       const bool is_tree = key->ReadObj()->InheritsFrom("TTree");
+                       const bool is_h1 = key->ReadObj()->InheritsFrom("TH1");
+                       const bool is_h2 = key->ReadObj()->InheritsFrom("TH2");
+                       if (is_tree) tree_names.insert(name);
+                       if (is_h1) h1_names.insert(name);
+                       if (is_h2) h2_names.insert(name);
+               }
+
+               input_handles.push_back(handle);
+       }
+
+       cout << "found " << tree_names.size() << " tree "<< h1_names.size() << " histo1d " << h2_names.size() << " histo2d" << endl;
+
+
+       for (Handles::const_iterator iter=input_handles.begin(); iter!=input_handles.end(); iter++)
+       {
+               (*iter)->Close();
+               delete *iter;
+       }
+}
+
+
 //-----------------------------------------------------------------------------
 int main(int argc, char * argv[]) {
 
@@ -35,21 +82,32 @@ int main(int argc, char * argv[]) {
     FATAL("Error, please provide at least two inputs files");
   }
 
-  // Detect Pet output
-  bool all_pet_output = true;
-  for (uint i=0; i<args_info.input_given; i++) 
-  {
-         const char* filename = args_info.input_arg[i];
-         TFile* handle = TFile::Open(filename,"READ");
-         TTree* hits = dynamic_cast<TTree*>(handle->Get("Hits"));
-         TTree* singles = dynamic_cast<TTree*>(handle->Get("Singles"));
-         const bool is_pet_output = (hits!=NULL) && (singles!=NULL);
-         cout << "testing " << filename << " is_pet_output " << is_pet_output << endl;
-         handle->Close();
-         delete handle;
-         all_pet_output &= is_pet_output;
+  { // Detect Pet output
+         bool all_pet_output = true;
+         Strings input_filenames;
+         for (uint i=0; i<args_info.input_given; i++) 
+         {
+                 const char* filename = args_info.input_arg[i];
+                 input_filenames.push_back(filename);
+                 TFile* handle = TFile::Open(filename,"READ");
+                 assert(handle);
+                 TTree* hits = dynamic_cast<TTree*>(handle->Get("Hits"));
+                 TTree* singles = dynamic_cast<TTree*>(handle->Get("Singles"));
+                 const bool is_pet_output = (hits!=NULL) && (singles!=NULL);
+                 cout << "testing " << filename << " is_pet_output " << is_pet_output << endl;
+                 handle->Close();
+                 delete handle;
+                 all_pet_output &= is_pet_output;
+         }
+         cout << "all_pet_output " << all_pet_output << endl;
+
+         if (all_pet_output)
+         {
+                 mergePetRoot(input_filenames,args_info.output_arg);
+                 return 0;
+         }
   }
-  cout << "all_pet_output " << all_pet_output << endl;
+
 
   // Merge
   TFileMerger * merger = new TFileMerger;