StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
hadd.C
1 #include <string.h>
2 #include "TChain.h"
3 #include "TFile.h"
4 #include "TH1.h"
5 #include "TTree.h"
6 #include "TKey.h"
7 
8 
9 TList *FileList;
10 TFile *Target;
11 
12 void MergeRootfile( TDirectory *target, TList *sourcelist );
13 
14 void hadd(int set=1) {
15  TString iPath;
16  iPath="/star/data05/scratch/balewski/sched-calibTPv6/R";
17 
18 
19  TString out=iPath;
20  out="./";
21  char *runL=0;
22  if(set==1) {//fms day 43-50
23  runL="9043035 9043036 9043040 9043041 9043042 9043043 9043044 9043047 9043048 9043061 9044023 9044034 9044035 9045006 9045007 9045011 9045014 9045020 9045021 9046008 9046009 9046015 9046016 9046017 9046024 9046025 9046026 9046029 9046030 9046031 9046035 9046037 9046071 9046072 9046078 9046082 9046084 9046087 9046089 9046091 9046093 9046094 9046095 9046098 9046099 9046103 9046104 9047001 9047013 9047029 9047031 9047037 9047038 9047039 9047043 9047070 9047071 9047075 9047077 9047078 9047081 9047082 9047083 9048005 9048007 9048008 9048036 9048042 9048044 9049016 9049017 9049018 9049040 9049044 9049045 9049048 9049049 9049050"; out+="sumD43.barCal";
24  } else if(set==2) {//fms day 50-53
25  runL="9050016 9050017 9050018 9050030 9050031 9050033 9050034 9050035 9050051 9050052 9050070 9050071 9050074 9050076 9050077 9050078 9050079 9050080 9050081 9050082 9050089 9050090 9050091 9050092 9050093 9050103 9050179 9050185 9050189 9051001 9051002 9051003 9051007 9051012 9051015 9051083 9051084 9051086 9051090 9051091 9051092 9051095 9051096 9051102 9051103 9052002 9052003 9052005 9052006 9052007 9052008 9052009 9052030 9052032 9052033 9052037 9052038 9052039 9052042 9052043 9052044 9052045 9052046 9052047 9052054 9052055 9052056 9052057 9052058 9053005 9053006 9053010 9053011 9053012 9053017 9053018 9053019 9053020 9053021 9053023 9053024 9053027 9053031 9053037 9053039 9053040 9053041 9053058 9053060 9053062 9053063 9053081 9053082 9053083 9053084 9053108 9053115 9053116 9053117 9053119 9053121 9053123 9053129 9053130 9053132 9053136 9053137 9053138 9053140"; out+="sumD50.barCal";
26  } else if(set==3) {//fms day 54-58
27  runL="9054001 9054002 9054008 9054009 9054010 9054012 9054013 9054019 9054021 9054022 9054023 9054028 9054029 9054030 9054031 9054032 9054033 9054034 9054035 9054036 9054042 9054043 9054044 9054045 9054046 9054057 9054058 9054059 9054064 9054065 9054067 9054069 9054071 9054072 9054073 9054074 9054075 9054076 9054080 9054083 9054084 9054085 9055012 9055013 9055014 9055023 9055024 9055025 9055028 9055029 9055031 9055032 9055036 9056005 9056008 9056009 9056013 9056014 9056016 9056017 9056019 9056020 9056021 9056022 9056025 9056029 9056030 9056031 9056036 9056037 9056038 9056039 9056067 9056068 9056069 9056074 9056076 9056077 9056079 9056081 9056082 9056085 9056087 9056091 9056092 9056093 9056095 9056097 9056102 9057004 9057009 9057011 9057013 9057014 9057020 9057026 9057028 9057030 9057032 9057037 9057039 9057042 9057043 9057045 9057047 9057069 9057073 9057076 9057078 9057079 9057081 9057082 9057086 9057087 9058055 9058064 9058068 "; out+="sumD54.barCal";
28  } else if(set==4) { // fms day 59
29  runL="9059003 9059004 9059005 9059007 9059009 9059013 9059014 9059015 9059018 9059041 9059043 9059044 9059046 9059051 9059052 9059057 9059058 9059059 9059060 9059061 9059069 9060008 9060015 9062008 9062009 9062010 9062012 9062013 9062014 9062015 9062016 9062019 9062020 9062033 9062034 9062035 9062036 9062039 9062043 9062044 9062045 9062046 9062047 9062057 9062081 9062082 9062085 9062087 9062088 9062101 9062102 9062109 9062110 9063037 9063121 9063126 9063138 9063139 9063142 9063143 9064001 9064002 9064005 9064006 9064009 9064010 9064011 9060016 9060017 9060018 9060022 9060023 9060027 9060031 9060032 9060036 9060040 9060041 9060064 9060068 9060070 9060074 9060075 9060077 9060078 9060079 9060085 9060086 9060087 9061007 9061008 9061011 9061013 9061014 9061015 9061019 9061022 9061023 9061026 9061028 9061030 9061031 9061037 9061051 9061067 9061082 9061096 9061098 9061099 9061100 9061101 9061102 9061103 9061104 9061105"; out+="sumD59.barCal";
30  } else if(set==5) { // fms Day 64
31  runL="9064019 9064020 9064023 9064025 9064027 9064029 9064030 9064031 9064032 9064033 9064036 9064037 9064041 9064042 9064043 9065009 9065010 9065016 9065018 9065021 9065023 9065026 9065030 9065031 9065032 9065057 9065058 9065062 9065063 9065065 9065068 9065073 9066001 9066012 9066013 9066015 9066016 9066027 9066036 9066042 9066045 9066049 9066053 9066058 9066059 9066065 9066066 9066068 9066070 9066071 9066074 9066086 9066087 9067005 9067007 9067008 9067010 9067013 9067018 9067019 9067020 9067022 9067023 9067028 9067040 9067041 9067044 9067045 9067060 9067061 9067063 9067064 9067065 9067095 9068008 9068009 9068010 9068012 9068014 9068015 9068021 9068022 9068032 9068035 9068037 9068038 9068055 9068056 9068058 9068063 9068066 9068074 9068075 9068078 9068079 9068080 9068081 9068082 9068084 9068085 9068086 9068087 9068088 9068124 9069005 9069009 9069010 9069011 9069013 9069014 9069021 9069023 9069024 9069025 9069042 9069053 9069058 9069059 9069064 9069065 9069066 9069079 9070006 9070007"; out+="sumD64.barCal";
32  } else if(set==0) { // all together, second pass
33  char *runL="sumD43 sumD50 sumD54 sumD59 sumD64"; out+="sumD43-70"; iPath="./";
34  } else assert(7==8);
35 
36  Target = TFile::Open( out+".hist.root", "RECREATE" );
37  printf("Output-->%s\n",Target->GetName());
38  FileList = new TList();
39 
40  char *run=strtok(runL," "); // init 'strtok'
41  int i=0;
42  int m0=0,m1=0;
43  do {
44  printf("add run i=%d '%s' nFound=%d\n",i++,run,m1+1);
45  TString fullName=iPath+run+".barCal.hist.root";
46  fd=TFile::Open(fullName);
47  if(fd==0){m0++; continue;}
48  m1++;
49  FileList->Add( fd);
50  // if(m1>3) break;//tmp
51  } while(run=strtok(0," ")); // advance by one nam
52 
53  MergeRootfile( Target, FileList );
54  printf("%d files on merge list, found %d, missed %d\n",i,m1,m0);
55 }
56 
57 void MergeRootfile( TDirectory *target, TList *sourcelist ) {
58 
59  // cout << "Target path: " << target->GetPath() << endl;
60  TString path( (char*)strstr( target->GetPath(), ":" ) );
61  path.Remove( 0, 2 );
62 
63  TFile *first_source = (TFile*)sourcelist->First();
64  first_source->cd( path );
65  TDirectory *current_sourcedir = gDirectory;
66 
67  int nh=0;
68  // loop over all keys in this directory
69  TChain *globChain = 0;
70  TIter nextkey( current_sourcedir->GetListOfKeys() );
71  TKey *key;
72  while ( (key = (TKey*)nextkey())) {
73  const char *name=key->GetName();
74  if(nh%5==0) printf("nh=%d addingX %s\n",nh,name);
75  nh++;
76 
77  // read object from first source file
78  first_source->cd( path );
79  TObject *obj = key->ReadObj();
80 
81  if ( obj->IsA()->InheritsFrom( "TH1" ) ) {
82  // descendant of TH1 -> merge it
83 
84  // cout << "Merging histogram " << obj->GetName() << endl;
85  TH1 *h1 = (TH1*)obj;
86 
87  // loop over all source files and add the content of the
88  // correspondant histogram to the one pointed to by "h1"
89  TFile *nextsource = (TFile*)sourcelist->After( first_source );
90  while ( nextsource ) {
91 
92  // make sure we are at the correct directory level by cd'ing to path
93  nextsource->cd( path );
94  TH1 *h2 = (TH1*)gDirectory->Get( h1->GetName() );
95  if ( h2 ) {
96  h1->Add( h2 );
97  delete h2; // don't know if this is necessary, i.e. if
98  // h2 is created by the call to gDirectory above.
99  }
100 
101  nextsource = (TFile*)sourcelist->After( nextsource );
102  }
103  }
104  else if ( obj->IsA()->InheritsFrom( "TTree" ) ) {
105 
106  // loop over all source files create a chain of Trees "globChain"
107  const char* obj_name= obj->GetName();
108 
109  globChain = new TChain(obj_name);
110  globChain->Add(first_source->GetName());
111  TFile *nextsource = (TFile*)sourcelist->After( first_source );
112  // const char* file_name = nextsource->GetName();
113  // cout << "file name " << file_name << endl;
114  while ( nextsource ) {
115 
116  globChain->Add(nextsource->GetName());
117  nextsource = (TFile*)sourcelist->After( nextsource );
118  }
119 
120  } else if ( obj->IsA()->InheritsFrom( "TDirectory" ) ) {
121  // it's a subdirectory
122 
123  cout << "Found subdirectory " << obj->GetName() << endl;
124 
125  // create a new subdir of same name and title in the target file
126  target->cd();
127  TDirectory *newdir = target->mkdir( obj->GetName(), obj->GetTitle() );
128 
129  // newdir is now the starting point of another round of merging
130  // newdir still knows its depth within the target file via
131  // GetPath(), so we can still figure out where we are in the recursion
132  MergeRootfile( core,newdir, sourcelist );
133 
134  } else {
135 
136  // object is of no type that we know or can handle
137  cout << "Unknown object type, name: "
138  << obj->GetName() << " title: " << obj->GetTitle() << endl;
139  }
140 
141  // now write the merged histogram (which is "in" obj) to the target file
142  // note that this will just store obj in the current directory level,
143  // which is not persistent until the complete directory itself is stored
144  // by "target->Write()" below
145  if ( obj ) {
146  target->cd();
147 
149  if(obj->IsA()->InheritsFrom( "TTree" ))
150  globChain->Write( key->GetName() );
151  else
152  obj->Write( key->GetName() );
153  }
154 
155  } // while ( ( TKey *key = (TKey*)nextkey() ) )
156 
157  // save modifications to target file
158  target->Write();
159 
160 }
161 
162 
163 /*
164 
165  This macro will add histograms from a list of root files and write them
166  to a target root file. The target file is newly created and must not be
167  identical to one of the source files.
168 
169  Author: Sven A. Schmidt, sven.schmidt@cern.ch
170  Date: 13.2.2001
171 
172  This code is based on the hadd.C example by Rene Brun and Dirk Geppert,
173  which had a problem with directories more than one level deep.
174  (see macro hadd_old.C for this previous implementation).
175 
176  The macro from Sven has been enhanced by
177  Anne-Sylvie Nicollerat <Anne-Sylvie.Nicollerat@cern.ch>
178  to automatically add Trees (via a chain of trees).
179 
180  To use this macro, modify the file names in function hadd.
181 
182  NB: This macro is provided as a tutorial.
183  Use $ROOTSYS/bin/hadd to merge many histogram files
184 
185  */
186