]> Creatis software - clitk.git/blob - tests_dav/clitkMergeRootFiles.cxx
Moved from repository clitk.private to tools
[clitk.git] / tests_dav / clitkMergeRootFiles.cxx
1 /**
2    =================================================
3    * @file   clitkMergeRootFiles.cxx
4    * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
5    * @date   01 Apr 2009
6    * 
7    * @brief  
8    * 
9    =================================================*/
10
11 #include "clitkMergeRootFiles_ggo.h"
12 #include "clitkCommon.h"
13 #include "GateMergeManager.hh"
14 #include <string> 
15 #include <TROOT.h>
16 #include <TPluginManager.h>
17 #include <TFile.h>
18 #include <TKey.h>
19 #include <TFileMerger.h>
20 #include <TTree.h>
21 #include <TChain.h>
22 #include <TH1.h>
23 #include <TH2.h>
24 #include <iostream>
25
26 using std::endl;
27 using std::cout;
28
29 struct PetInputFile
30 {
31         string filename;
32         double mean_time;
33 };
34
35 bool sort_pet_input_file(const PetInputFile& a, const PetInputFile& b)
36 {
37         return a.mean_time<b.mean_time;
38 };
39
40 typedef std::vector<PetInputFile> PetInputFiles;
41
42 //-----------------------------------------------------------------------------
43 int main(int argc, char * argv[]) {
44
45   gROOT->GetPluginManager()->AddHandler("TVirtualStreamerInfo", "*",
46       "TStreamerInfo", "RIO", "TStreamerInfo()");
47
48   // init command line
49   GGO(clitkMergeRootFiles, args_info);
50
51   // Check parameters
52   if (args_info.input_given < 2) {
53     FATAL("Error, please provide at least two inputs files");
54   }
55
56   { // Detect Pet output
57           bool all_pet_output = true;
58           PetInputFiles pet_input_files;
59           for (uint i=0; i<args_info.input_given; i++) 
60           {
61                   const char* filename = args_info.input_arg[i];
62                   PetInputFile input_file;
63                   input_file.filename = filename;
64                   TFile* handle = TFile::Open(filename,"READ");
65                   assert(handle);
66                   TTree* hits = dynamic_cast<TTree*>(handle->Get("Hits"));
67                   TTree* singles = dynamic_cast<TTree*>(handle->Get("Singles"));
68                   const bool is_pet_output = (hits!=NULL) && (singles!=NULL);
69                   cout << "testing " << filename << " is_pet_output " << is_pet_output;
70
71                   if (is_pet_output)
72                   {
73                           double time;
74                           double time_accum = 0;
75                           singles->SetBranchAddress("time",&time);
76                           size_t total = singles->GetEntries();
77                           for (size_t kk=0; kk<total; kk++)
78                           {
79                                   singles->GetEntry(kk);
80                                   time_accum += time;
81                           }
82                                   
83                           input_file.mean_time = time_accum/total;
84                           pet_input_files.push_back(input_file);
85                           cout << " mean_time " << input_file.mean_time;
86                   }
87
88                   cout << endl;
89
90                   handle->Close();
91                   delete handle;
92                   all_pet_output &= is_pet_output;
93           }
94           cout << "all_pet_output " << all_pet_output << endl;
95
96           if (all_pet_output)
97           {
98                   GateMergeManager manager(args_info.fastmerge_given,args_info.verbose_arg,true,0,"");
99
100                   cout << "sorting input file using singles time" << endl;
101                   std::sort(pet_input_files.begin(),pet_input_files.end(),sort_pet_input_file);
102
103                   Strings input_filenames;
104                   for (PetInputFiles::const_iterator iter=pet_input_files.begin(); iter!=pet_input_files.end(); iter++)
105                           input_filenames.push_back(iter->filename);
106
107                   manager.StartMergingFromFilenames(input_filenames,args_info.output_arg);
108                   return 0;
109           }
110   }
111
112
113   // Merge
114   TFileMerger * merger = new TFileMerger;
115   for (uint i=0; i<args_info.input_given; i++) merger->AddFile(args_info.input_arg[i]);
116   merger->OutputFile(args_info.output_arg);
117   merger->Merge();
118
119   // this is the end my friend  
120   return 0;
121 }
122 //-----------------------------------------------------------------------------