StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
bfcread_hist_files_add.C
1 // $Id: bfcread_hist_files_add.C,v 2.24 2018/03/21 02:37:32 genevb Exp $
2 // $Log: bfcread_hist_files_add.C,v $
3 // Revision 2.24 2018/03/21 02:37:32 genevb
4 // Improper use of endl corrected
5 //
6 // Revision 2.23 2017/02/27 21:41:36 genevb
7 // StEvent.so now depends on StBichsel.so
8 //
9 // Revision 2.22 2016/06/10 15:51:47 genevb
10 // Fix memory leak
11 //
12 // Revision 2.21 2015/03/19 17:51:28 genevb
13 // Minimize dependencies (tpc_Tables, number of files)
14 //
15 // Revision 2.20 2013/03/14 17:28:31 genevb
16 // StTpcDb.so now depends on StEvent.so
17 //
18 // Revision 2.19 2010/03/17 02:53:06 genevb
19 // Add hists even if not in first file
20 //
21 // Revision 2.18 2010/03/16 15:05:22 jeromel
22 // Fix for the move of TpcC def
23 //
24 // Revision 2.17 2007/12/10 18:03:22 genevb
25 // Additional library needed
26 //
27 // Revision 2.16 2007/04/03 21:15:13 genevb
28 // Minor fix for StIOMaker/StFile pairing
29 //
30 // Revision 2.15 2004/09/01 14:41:23 jeromel
31 // Thanks to Janet S., some correction to broken macro
32 // fin logic change
33 // gPad / example commented out
34 //
35 // Revision 2.14 2002/09/06 02:50:22 genevb
36 // Update compatibility with library changes
37 //
38 // Revision 2.13 2000/07/26 19:53:45 lansdell
39 // made changes for creating new QA histograms
40 //
41 // Revision 2.12 2000/07/07 04:18:55 lansdell
42 // loops over all branches of multiple hist.root files and saves the summed histograms to a new hist.root file (thanks Gene!)
43 //
44 // Revision 2.11 2000/06/29 22:42:22 lansdell
45 // now write to specified hist branch properly
46 //
47 // Revision 2.10 2000/06/29 05:47:17 lansdell
48 // checked for null pointer
49 //
50 // Revision 2.9 2000/06/29 05:04:24 lansdell
51 // now outputs .hist.root file with particular maker's histograms (need to update to do all histogram branches)
52 //
53 // Revision 2.8 2000/06/23 23:16:12 lansdell
54 // now reads .hist.root file list from text file; be sure to use nevents <= the number of .hist.root files
55 //
56 // Revision 2.7 2000/06/23 21:34:21 kathy
57 // remove unneeded macro; make bfcread_hist_files_add work with a chain - cleanup
58 //
59 // Revision 2.6 2000/06/23 20:57:21 kathy
60 // getting ready to write output hist file
61 //
62 // Revision 2.4 2000/06/23 20:18:09 kathy
63 // move example draw out of file loop to prepare for writing hist out to .hist.root file
64 //
65 // Revision 2.3 2000/06/23 15:52:34 kathy
66 // cleanup - only need to use IOMk, not IOMk1,2 etc
67 //
68 // Revision 2.2 2000/06/23 15:25:30 kathy
69 // now use the new methods CopyHists & AddHists in StHistUtil
70 //
71 // Revision 2.1 2000/06/22 21:16:14 kathy
72 // new macro that will read in 2 hist.root files and add the contents of the histograms together into a new set of histograms
73 //
74 
75 //=======================================================================
76 // owner: Kathy Turner
77 //
78 // bfcread_hist_files_add.C
79 //
80 // what it does:
81 // - reads in different *.hist.root files produced from a chain
82 // (such as bfc)
83 // - creates a copy of all histograms in the first file (newHist)
84 // - adds contents of all histograms in subsequent files
85 // with the same NAME into the newHist
86 //
87 // inputs:
88 // nevents - number of files to process (assuming 1 "event" per file)
89 // fileName - .txt file with list of hist.root files from bfc output
90 // outHistFile - output .hist.root file
91 // TopDirTree - top level directory tree in your input hist file
92 // (this is 3rd argument of constructor for StTreeMaker that
93 // you probably used to write the *.hist.root file)
94 // NOTE: if you ran bfc, then the TopDirTree = bfcTree !!
95 //
96 //======================================================================
97 
98 
99 void bfcread_hist_files_add(
100  int nevents=3,
101  const Char_t *fileName="fileList.txt",
102  const Char_t *outHistFile="kathy",
103  const Char_t *TopDirTree="bfcTree")
104 {
105  cout << "bfcread_hist_files_add.C, input file list = "
106  << fileName << endl;
107  cout << "bfcread_hist_files_add.C, output file list = "
108  << outHistFile << endl;
109  cout << "bfcread_hist_files_add.C, top level directory in hist file = "
110  << TopDirTree << endl;
111 
112  gSystem->Load("St_base");
113  gSystem->Load("StChain");
114  gSystem->Load("StIOMaker");
115  gSystem->Load("StarClassLibrary");
116  gSystem->Load("StUtilities");
117  gSystem->Load("StAnalysisUtilities");
118  gSystem->Load("libglobal_Tables");
119  gSystem->Load("libStDb_Tables");
120  gSystem->Load("StDetectorDbMaker");
121  gSystem->Load("libStDb_Tables.so");
122  gSystem->Load("StBichsel");
123  gSystem->Load("StEvent");
124  gSystem->Load("StTpcDb");
125  gSystem->Load("StPmdUtil");
126  gSystem->Load("St_QA_Maker");
127  gSystem->Load("StTreeMaker");
128 
129 // read file list from text file (maximum of 8192 files)
130  char **fList = new char[8192][256];
131  ifstream fin(fileName);
132  if (fin) {
133  int j=0;
134  cout << "Input file " << fileName << " found." << endl;
135  for (Int_t i=0;i<nevents;i++){
136  char *line = new char[256];
137  fin >> line;
138  if (fin.eof()) break;
139  fList[j] = line;
140  cout << " Added file : " << fList[j] << endl;
141  j++;
142  }
143  }
144  fin.close();
145 
146 // set up write chain which knows about hist and tree makers
147  StChain* chainW = new StChain("writeChain");
148  char *MakerHistDir= new char[256];
149 
150 // set up output file structure
151  StTreeMaker* treeMk = new StTreeMaker("outTree",outHistFile,TopDirTree);
152  treeMk->SetIOMode("w");
153  treeMk->SetBranch("histBranch");
154  treeMk->Init();
155 
156 // loop over files:
157  Int_t istat=0;
158  Int_t ifl=0;
159  Int_t hCCount=0;
160 
161  StFile* fff = new StFile(fList);
162  nevents = TMath::Min(nevents,fff->GetNFiles());
163  StIOMaker *IOMk = new StIOMaker("IO","r",fff,TopDirTree);
164  IOMk->SetIOMode("r");
165  IOMk->SetBranch("*",0,"0"); //deactivate all branches
166  IOMk->SetBranch("histBranch",0,"r"); //activate hist Branch
167  IOMk->Init();
168 // we DON'T want IOMaker in the write chain
169  IOMk->Shunt();
170 
171 // create arrays for histmakers, histutils for multiple branches/files
172  StHistMaker* HM[128];
173  StHistUtil* HU[128];
174  int nbranch=0;
175 
176  EventLoop: if (ifl<nevents && istat==0) {
177 
178  cout << "\nNow processing file : " << ifl+1 << "\n" << endl;
179 
180 // --- each file contains only histograms (1 "event" == 1 Make call)
181 
182  IOMk->Clear();
183  istat = IOMk->Open();
184  if (istat) goto EventLoop;
185  istat = IOMk->Make();
186  if (istat) goto EventLoop;
187  // from list_all macro
188  Event = IOMk->GetDataSet("hist");
189  if (!Event) {
190  cout << " No histogram branch found in file!" << endl;
191  }
192 
193  // loop over all branches in the file
194  TDataSetIter nextHistList(Event);
195  St_ObjectSet *histContainer = 0;
196  TList *dirList = 0;
197  while (histContainer = (St_ObjectSet *)nextHistList.Next()) {
198  dirList = (TList *) histContainer->GetObject();
199  strcpy(MakerHistDir,histContainer->GetName());
200  MakerHistDir[strlen(MakerHistDir)-4] = 0;
201  //if (strcmp(MakerHistDir,"IO")==0 || strcmp(MakerHistDir,"IO_Root")==0) {
202  // cout << " --- Skipping " << MakerHistDir << endl;
203  // continue;
204  //}
205  cout << "\n --- MakerHistDir : " << MakerHistDir << endl;
206 
207 // check for new branches and instantiate them
208  int bnum=-1;
209  for (int l=0; l<nbranch; l++) {
210  if (!strcmp(MakerHistDir,HM[l]->GetName())) {
211  bnum = l;
212  break;
213  }
214  }
215 
216  if (bnum==-1) {
217  bnum = nbranch++;
218  HM[bnum] = new StHistMaker(MakerHistDir);
219  HM[bnum]->Init();
220  HU[bnum] = new StHistUtil();
221  HU[bnum]->SetPntrToMaker(IOMk);
222 
223 // get the TList pointer to the histograms for this branch:
224  dirList = HU[bnum]->FindHists(MakerHistDir);
225 
226 // now make a copy of all histograms into the new histograms!
227  hCCount = HU[bnum]->CopyHists(dirList);
228 
229  cout << "bfcread_hist_files_add.C, # histograms copied = " <<
230  hCCount << endl;
231 
232  } // first time
233 
234  else {
235  dirList = HU[bnum]->FindHists(MakerHistDir);
236 
237 // now make a copy of all histograms into my new histograms!
238  hCCount = HU[bnum]->AddHists(dirList);
239 
240  cout << "bfcread_hist_files_add.C, # histograms added = " <<
241  hCCount << endl;
242 
243  } //else (ifl not #1)
244 
245 
246  HM[bnum]->SetHArraySize(HU[bnum]->getNewHistSize());
247  HM[bnum]->SetHArray(HU[bnum]->getNewHist());
248  HM[bnum]->Make();
249  HU[bnum]->Clear();
250 
251 // to see an example of histograms being added together:
252 // TH1** kathyArray = HU[bnum]->getNewHist();
253 // for (Int_t imk=0;imk<hCCount;imk++) {
254 // if (strcmp(kathyArray[imk]->GetName(),"TabQaEvsumTrkTot")==0) {
255 // kathyArray[imk]->Draw();
256 // gPad->Update();
257 // } // if strcmp -- to draw
258 // } // for -- end of example
259  } // end while loop
260 
261  ifl++;
262  istat = IOMk->Close();
263  goto EventLoop;
264  } // loop over files
265 
266  treeMk->Make();
267  treeMk->Finish();
268  IOMk->Finish();
269 } // end of the macro!
virtual Int_t Finish()
Definition: StTree.h:125
virtual Int_t Make()
Definition: StHistMaker.cxx:34
virtual void SetIOMode(Option_t *iomode="w")
number of transactions
Definition: StIOInterFace.h:35
virtual Int_t Make()
virtual TDataSet * Next() const
Definition: TDataSet.cxx:447
virtual const char * GetName() const
special overload
Definition: StMaker.cxx:237
Definition: AgUStep.h:26
virtual TObject * GetObject() const
The depricated method (left here for the sake of the backward compatibility)
Definition: TObjectSet.h:56