StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
haddWana.C
1 //$Id: haddWana.C,v 1.2 2012/03/12 23:11:43 smirnovd Exp $
2 // line added after tag=DNP2009 by Jan
3 // run list chaned to match final run selection for SL09g
4 
5 #include <string.h>
6 #include "TChain.h"
7 #include "TFile.h"
8 #include "TH1.h"
9 #include "TTree.h"
10 #include "TKey.h"
11 #include "Riostream.h"
12 
13 TList *FileList;
14 TFile *Target;
15 
16 
17 int isAPS2010pol = 0; /* affects only setP1-P4, not setA-D,
18  set it to 0 to see all usable polarized fills */
19 
20 
21 
22 void MergeRootfile( TDirectory *target, TList *sourcelist );
23 
24 
25 void haddWana(char *set = "sumFeb21_2011", TString iPath = "./")
26 {
27  TString out = iPath;
28 
29  if (strstr("sumFeb21_2011", set) > 0) {
30  char *runL = " cutsOnly_Feb21 raw_sumFeb21";
31  }
32  else if (strstr("setXX", set) > 0) {
33  char *runL = "HHHHH "; //total: NNN
34  }
35  else {
36  printf(" hadd: set=%s= NOT found, quit\n", set);
37  return;
38  }
39  printf(" hadd: set=%s= path=%s= ...\n", set, iPath.Data());
40  out += set;
41  Target = TFile::Open( out + ".wana.hist.root", "RECREATE" );
42  FileList = new TList();
43  printf("sum Output '%s' \n", Target->GetName());
44 
45  char *run = strtok(runL, " "); // init 'strtok'
46  int i = 1;
47  do {
48  printf("add run %d '%s' \n", i++, run);
49  if (isAPS2010pol && strstr(set, "run9setP") > 0 ) { // remove runs from fills w/o official pol for APS-2010
50  int irun = atoi(run + 1);
51  if (irun >= 10083013 && irun <= 10083058) {
52  printf("\tdrop %s from F10415\n", run);
53  continue;
54  }
55  if (irun >= 10098015 && irun <= 10098015) {
56  printf("\tdrop %s from F10508\n", run);
57  continue;
58  }
59  }
60  TString fullName = iPath + run + ".wana.hist.root";
61  FileList->Add( TFile::Open(fullName));
62  }
63  while (run = strtok(0, " ")); // advance by one nam
64 
65  MergeRootfile( Target, FileList );
66  printf("finished Output '%s' \n", Target->GetName());
67 
68 }
69 
70 void MergeRootfile( TDirectory *target, TList *sourcelist )
71 {
72 
73  cout << "Target path: " << target->GetPath() << endl;
74  TString path( (char*)strstr( target->GetPath(), ":" ) );
75  path.Remove( 0, 2 );
76 
77  TFile *first_source = (TFile*)sourcelist->First();
78  first_source->cd( path );
79  TDirectory *current_sourcedir = gDirectory;
80 
81  int nh = 0;
82  // loop over all keys in this directory
83  TChain *globChain = 0;
84  TIter nextkey( current_sourcedir->GetListOfKeys() );
85  TKey *key;
86  while ( (key = (TKey*)nextkey())) {
87  const char *name = key->GetName();
88  nh++;
89  if (nh % 100 == 0) printf("nh=%d addingX %s\n", nh, name);
90 
91  // read object from first source file
92  first_source->cd( path );
93  TObject *obj = key->ReadObj();
94 
95  if ( obj->IsA()->InheritsFrom( "TH1" ) ) {
96  // descendant of TH1 -> merge it
97 
98  // cout << "Merging histogram " << obj->GetName() << endl;
99  TH1 *h1 = (TH1*)obj;
100 
101  // loop over all source files and add the content of the
102  // correspondant histogram to the one pointed to by "h1"
103  TFile *nextsource = (TFile*)sourcelist->After( first_source );
104  while ( nextsource ) {
105 
106  // make sure we are at the correct directory level by cd'ing to path
107  nextsource->cd( path );
108  TH1 *h2 = (TH1*)gDirectory->Get( h1->GetName() );
109  if ( h2 ) {
110  h1->Add( h2 );
111  delete h2; // don't know if this is necessary, i.e. if
112  // h2 is created by the call to gDirectory above.
113  }
114 
115  nextsource = (TFile*)sourcelist->After( nextsource );
116  }
117  }
118  else if ( obj->IsA()->InheritsFrom( "TTree" ) ) {
119 
120  // loop over all source files create a chain of Trees "globChain"
121  const char* obj_name = obj->GetName();
122 
123  globChain = new TChain(obj_name);
124  globChain->Add(first_source->GetName());
125  TFile *nextsource = (TFile*)sourcelist->After( first_source );
126  // const char* file_name = nextsource->GetName();
127  // cout << "file name " << file_name << endl;
128  while ( nextsource ) {
129 
130  globChain->Add(nextsource->GetName());
131  nextsource = (TFile*)sourcelist->After( nextsource );
132  }
133 
134  }
135  else if ( obj->IsA()->InheritsFrom( "TDirectory" ) ) {
136  // it's a subdirectory
137 
138  cout << "Found subdirectory " << obj->GetName() << endl;
139 
140  // create a new subdir of same name and title in the target file
141  target->cd();
142  TDirectory *newdir = target->mkdir( obj->GetName(), obj->GetTitle() );
143 
144  // newdir is now the starting point of another round of merging
145  // newdir still knows its depth within the target file via
146  // GetPath(), so we can still figure out where we are in the recursion
147  MergeRootfile( core, newdir, sourcelist );
148 
149  }
150  else {
151 
152  // object is of no type that we know or can handle
153  cout << "Unknown object type, name: "
154  << obj->GetName() << " title: " << obj->GetTitle() << endl;
155  }
156 
157  // now write the merged histogram (which is "in" obj) to the target file
158  // note that this will just store obj in the current directory level,
159  // which is not persistent until the complete directory itself is stored
160  // by "target->Write()" below
161  if ( obj ) {
162  target->cd();
163 
165  if (obj->IsA()->InheritsFrom( "TTree" ))
166  globChain->Write( key->GetName() );
167  else
168  obj->Write( key->GetName() );
169  }
170 
171  } // while ( ( TKey *key = (TKey*)nextkey() ) )
172 
173  // save modifications to target file
174  target->Write();
175 
176 }
177 
178 
179 /*
180 
181  This macro will add histograms from a list of root files and write them
182  to a target root file. The target file is newly created and must not be
183  identical to one of the source files.
184 
185  Author: Sven A. Schmidt, sven.schmidt@cern.ch
186  Date: 13.2.2001
187 
188  This code is based on the hadd.C example by Rene Brun and Dirk Geppert,
189  which had a problem with directories more than one level deep.
190  (see macro hadd_old.C for this previous implementation).
191 
192  The macro from Sven has been enhanced by
193  Anne-Sylvie Nicollerat <Anne-Sylvie.Nicollerat@cern.ch>
194  to automatically add Trees (via a chain of trees).
195 
196  To use this macro, modify the file names in function hadd.
197 
198  NB: This macro is provided as a tutorial.
199  Use $ROOTSYS/bin/hadd to merge many histogram files
200 
201  */
202 
203 
204 
205 // $Log: haddWana.C,v $
206 // Revision 1.2 2012/03/12 23:11:43 smirnovd
207 // *** empty log message ***
208 //
209 // Revision 1.1 2011/02/23 14:00:12 balewski
210 // *** empty log message ***
211 //
212 // Revision 1.18 2010/05/19 20:49:50 balewski
213 // removed 5 runs tagged by Ross
214 //
215 // Revision 1.17 2010/03/15 17:05:51 balewski
216 // cleanup, used for W AL sort March 15, 2010
217 //
218 // Revision 1.16 2010/03/14 19:37:27 balewski
219 // Removed F10383, has TPC problem according to Gene
220 //
221 // Revision 1.15 2010/02/24 18:26:24 balewski
222 // added macros computing/plotting AL
223 //
224 // Revision 1.14 2010/02/06 01:12:46 balewski
225 // skips unpol CNI fills
226 //
227 // Revision 1.13 2010/02/04 03:48:25 balewski
228 // add ET for lumi monitor
229 //
230 // Revision 1.12 2010/01/27 22:12:26 balewski
231 // spin code matched to x-section code
232 //
233 // Revision 1.11 2010/01/10 03:01:39 balewski
234 // cleanup & nicer histos
235 //
236 // Revision 1.10 2010/01/06 05:21:59 balewski
237 // cleanup
238 //
239 // Revision 1.9 2010/01/06 04:22:18 balewski
240 // added Q/PT plot for Zs, more cleanup
241 //
242 // Revision 1.8 2010/01/05 03:23:02 balewski
243 // change logic for filling btow status tables, added printout to Z-code
244 //
245 // Revision 1.7 2010/01/04 05:12:02 balewski
246 // added 4x4 cut to Z-algo, cleanup
247 //
248 // Revision 1.6 2010/01/03 04:38:27 balewski
249 // reorganized Z-algo
250 //
251 // Revision 1.5 2010/01/03 01:58:19 balewski
252 // run list for setABCD updated to SL09g
253 //
254 // Revision 1.4 2009/12/30 18:27:52 balewski
255 // after tag I added a test line
256 //
257 // Revision 1.3 2009/12/30 18:27:02 balewski
258 // added tag for testing
259 //
260 // Revision 1.2 2009/12/08 16:53:01 balewski
261 // *** empty log message ***
262 //
263 // Revision 1.1 2009/11/23 23:00:20 balewski
264 // code moved spin-pool
265 //