]> Creatis software - clitk.git/blobdiff - tests_dav/clitkMergeRootFiles.cxx
Moved from repository clitk.private to tools
[clitk.git] / tests_dav / clitkMergeRootFiles.cxx
index 5b62d84e7608235360b0459f96a66ae9c23dbce2..4bd0634a763afd5e84cc234a4b0e5613472d6b9a 100644 (file)
@@ -10,6 +10,7 @@
 
 #include "clitkMergeRootFiles_ggo.h"
 #include "clitkCommon.h"
+#include "GateMergeManager.hh"
 #include <string> 
 #include <TROOT.h>
 #include <TPluginManager.h>
 #include <TKey.h>
 #include <TFileMerger.h>
 #include <TTree.h>
+#include <TChain.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;
+struct PetInputFile
+{
+       string filename;
+       double mean_time;
+};
 
-void mergePetRoot(const Strings& input_filenames, const std::string& output_filename)
+bool sort_pet_input_file(const PetInputFile& a, const PetInputFile& b)
 {
-       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;
-       }
-}
+       return a.mean_time<b.mean_time;
+};
 
+typedef std::vector<PetInputFile> PetInputFiles;
 
 //-----------------------------------------------------------------------------
 int main(int argc, char * argv[]) {
@@ -84,17 +55,38 @@ int main(int argc, char * argv[]) {
 
   { // Detect Pet output
          bool all_pet_output = true;
-         Strings input_filenames;
+         PetInputFiles pet_input_files;
          for (uint i=0; i<args_info.input_given; i++) 
          {
                  const char* filename = args_info.input_arg[i];
-                 input_filenames.push_back(filename);
+                 PetInputFile input_file;
+                 input_file.filename = 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;
+                 cout << "testing " << filename << " is_pet_output " << is_pet_output;
+
+                 if (is_pet_output)
+                 {
+                         double time;
+                         double time_accum = 0;
+                         singles->SetBranchAddress("time",&time);
+                         size_t total = singles->GetEntries();
+                         for (size_t kk=0; kk<total; kk++)
+                         {
+                                 singles->GetEntry(kk);
+                                 time_accum += time;
+                         }
+                                 
+                         input_file.mean_time = time_accum/total;
+                         pet_input_files.push_back(input_file);
+                         cout << " mean_time " << input_file.mean_time;
+                 }
+
+                 cout << endl;
+
                  handle->Close();
                  delete handle;
                  all_pet_output &= is_pet_output;
@@ -103,7 +95,16 @@ int main(int argc, char * argv[]) {
 
          if (all_pet_output)
          {
-                 mergePetRoot(input_filenames,args_info.output_arg);
+                 GateMergeManager manager(args_info.fastmerge_given,args_info.verbose_arg,true,0,"");
+
+                 cout << "sorting input file using singles time" << endl;
+                 std::sort(pet_input_files.begin(),pet_input_files.end(),sort_pet_input_file);
+
+                 Strings input_filenames;
+                 for (PetInputFiles::const_iterator iter=pet_input_files.begin(); iter!=pet_input_files.end(); iter++)
+                         input_filenames.push_back(iter->filename);
+
+                 manager.StartMergingFromFilenames(input_filenames,args_info.output_arg);
                  return 0;
          }
   }