StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StMuIOMaker.cxx
1 /***************************************************************************
2  *
3  * $Id: StMuIOMaker.cxx,v 1.21 2011/08/18 18:41:36 fisyak Exp $
4  * Author: Frank Laue, BNL, laue@bnl.gov
5  *
6  * Made it integrated to StIOMaker for applying Grid Collector
7  * Wei-Ming Zhang KSU 3/8/2004
8  *
9  **************************************************************************/
10 #include "StarClassLibrary/StTimer.hh"
11 //#include "StMuEmcUtil.h"
12 #include "StMessMgr.h"
13 #include "StMuDebug.h"
14 #include "StMuIOMaker.h"
15 #include "StMuDst.h"
16 #include "StMuEvent.h"
17 
18 #include "TFile.h"
19 #include "THack.h"
20 #include "TChain.h"
21 #include "TClonesArray.h"
22 
23 #include "THack.h"
24 ClassImp(StMuIOMaker)
25 
26 //-----------------------------------------------------------------------
27 //-----------------------------------------------------------------------
28 StMuIOMaker::StMuIOMaker(const char* name, const char *ioFile) :
29  StMuDstMaker(name)
30 {
31  mOutFile =0;
32 
33 
34  mNumberOfEvents=0;
35  mCurrentIndex =0;
36  mEventCounter =0;
37 
38  mSplit =99;
39  mCompression =9;
40  mBufferSize =65536*4;
41  mMuSave =0;
42  mBadInFile =0;
43  mIoMode =0;
44  mCloseWrite =1;
45 
46 
47 
48 
49 }
50 
51 //-----------------------------------------------------------------------
52 //-----------------------------------------------------------------------
53 StMuIOMaker::~StMuIOMaker() {
54  DEBUGMESSAGE("");
55  if(mMuSave && !mBadInFile && !mCloseWrite) closeMuWrite();
56  DEBUGMESSAGE3("out");
57 }
58 
59 //-----------------------------------------------------------------------
60 //-----------------------------------------------------------------------
61 //-----------------------------------------------------------------------
71  DEBUGMESSAGE("");
72  TDataSet *muDstSet = AddObj(mStMuDst,".const");
73  if (muDstSet ) muDstSet ->SetName("MuDst");
74 
75 // borrow StIOMaker::TString TreeName to pass a save flag for MuDst
76  if(fTreeName == "MuSave") mMuSave = true;
77 
78  DEBUGVALUE(mMuSave);
79  DEBUGMESSAGE2("out");
80  return 0;
81 }
82 //-----------------------------------------------------------------------
83 //-----------------------------------------------------------------------
85 // same as "Read mode" of StTreeMaker
86  int iret=0,ntry=13;
87  while(1999) {
88  iret = MakeRead();
89  if (iret!=kStErr && ntry--) break;
90  Warning("Make","%d *** ReadError ***\n",ntry);
91  }
92  return iret;
93 }
94 //-----------------------------------------------------------------------
95 //-----------------------------------------------------------------------
111 int StMuIOMaker::Make(int index){
112  DEBUGMESSAGE("");
113 // as default clear(0)
114  //clear();
115  DEBUGVALUE3(index);
116  mCurrentIndex = index;
117  DEBUGVALUE3(mNumberOfEvents);
118  if ( mCurrentIndex >= mNumberOfEvents) return kStEOF;
119  if ( mCurrentIndex < 0 ) return kStErr;
120  if (!mChain) return kStEOF;
121  int bytes = mChain->GetEntry(mCurrentIndex);
122  if (bytes <= 0) {
123  LOG_WARN << "#### " << fFile.Data() << " could not read event " << index
124  << ", TTree::GetEntry returned " << bytes << endm;
125  return kStWarn;
126  }
127 
128  DEBUGVALUE(mMuSave);
129  if(mMuSave) { mOutTree->Fill(); THack::IsTreeWritable(mOutTree); }
130 // One event would be skipped in StChain if making increment here!!!!
131 // mCurrentIndex++;
132  DEBUGVALUE3(bytes);
133  mStMuDst->set(this);
134  mEventCounter++;
135  fillHddr();
136  mStMuDst->collectVertexTracks();
137  return kStOk;
138 }
139 //-----------------------------------------------------------------------
140 //-----------------------------------------------------------------------
141 int StMuIOMaker::Make(int major, int minor){
142  DEBUGMESSAGE("");
143  DEBUGVALUE3(major);
144  DEBUGVALUE3(minor);
145  int index = mChain->GetEntryNumberWithIndex(major,minor);
146  DEBUGMESSAGE("out");
147  return Make(index);
148 }
149 //-----------------------------------------------------------------------
150 //-----------------------------------------------------------------------
151 int StMuIOMaker::Make(const StUKey& key){
152  DEBUGMESSAGE("");
153  int index = mChain->GetEntryNumberWithIndex(key.GetRunId(),key.GetEventId());
154  DEBUGVALUE3(index);
155  DEBUGMESSAGE("out");
156  return Make(index);
157 }
158 //-----------------------------------------------------------------------
159 //-----------------------------------------------------------------------
161  DEBUGMESSAGE("");
162  LOG_INFO << "###### StMuIOMaker read " << mEventCounter << " events" << endm;
163  if(mMuSave && !mBadInFile && !mCloseWrite) {
164  if (mOutTree) mOutTree->AutoSave();
165  closeMuWrite();
166  }
167  DEBUGMESSAGE3("out");
168  return kStOK;
169 }
170 
171 // implementaion of virtual methods of base StIOInterFace
172 //-----------------------------------------------------------------------
173 //-----------------------------------------------------------------------
174 Int_t StMuIOMaker:: MakeRead(const StUKey &key) {
175  DEBUGMESSAGE("");
176  if(!mBadInFile) {
177  if(key.IsNull()) { // sequential reading in normal case
178  DEBUGMESSAGE("Seq");
179  return Make(mCurrentIndex+1);
180  }
181  else { // GC
182  DEBUGMESSAGE("GC");
183  return Make(key);
184  }
185  }
186  else { // Bad input file
187  DEBUGMESSAGE("BadFile");
188  return kStEOF;
189  }
190 }
191 //-----------------------------------------------------------------------
192 //-----------------------------------------------------------------------
193 // Open() in BASE StIOInterace returns 1999 if not implemented in DERIVED here
194 Int_t StMuIOMaker::Open(const char*) {
195  DEBUGMESSAGE("");
196  mBadInFile = false;
197  mCloseWrite = true;
198  int iret = openRead();
199  if(mMuSave && !mBadInFile) {
200  openMuWrite();
201  mCloseWrite = false;
202  }
203  return iret;
204 }
205 //-----------------------------------------------------------------------
206 //-----------------------------------------------------------------------
207 // Close() in BASE StIOInterace will quit if not implemented in DERIVED here
208 void StMuIOMaker::Close(Option_t *) {
209  DEBUGMESSAGE("");
210  if(mMuSave && !mBadInFile && !mCloseWrite) closeMuWrite();
211  closeRead();
212  DEBUGMESSAGE3("out");
213 }
214 
215 // private methods
216 //-----------------------------------------------------------------------
217 //-----------------------------------------------------------------------
218 int StMuIOMaker::openRead() {
219  DEBUGMESSAGE("");
220  DEBUGVALUE(fFile.Data());
221  mChain = new TChain("MuDst");
222 
223  mChain->Add(fFile.Data());
224  DEBUGVALUE3(mChain);
225 
226  mNumberOfEvents = (int)mChain->GetEntries();
227 
228  if(mNumberOfEvents == 1234567890) {
229  LOG_WARN << "#### " << fFile.Data() << " Corrupted!" << endm;
230  mBadInFile = true;
231  closeRead();
232  return kStOK;
233  }
234 
235  StTimer timer;
236  timer.reset();
237  timer.start();
238 
239  setBranchAddresses(mChain);
240  mChain->BuildIndex("MuEvent.mEventInfo.mRunId","MuEvent.mEventInfo.mId");
241  timer.stop();
242  LOG_INFO << " Index of " << mNumberOfEvents << " events buit in "
243  << timer.elapsedTime() << " seconds " << endm;
244 
245 
246  mCurrentIndex = -1;
247  DEBUGMESSAGE3("out");
248  return kStOK;
249 }
250 
251 //-----------------------------------------------------------------------
252 //-----------------------------------------------------------------------
253 void StMuIOMaker::openMuWrite() {
254  DEBUGMESSAGE("");
255  mOutFileName = fFile.Data();
256  while (mOutFileName.find("/")!=string::npos) {
257  int pos = mOutFileName.find("/");
258  mOutFileName.erase(0,pos+1);
259  }
260  int pos = mOutFileName.find("MuDst");
261  mOutFileName = mOutFileName.insert(pos,"Sel.");
262  DEBUGVALUE(mOutFileName.c_str());
263 
264  mOutFile = new TFile(mOutFileName.c_str(),"RECREATE","StMuDst");
265  if (mOutFile->IsZombie()) {
266  LOG_FATAL << "StMuIOMaker::openMuWrite" << " Can not create TFile object for "
267  << mOutFileName.c_str() << " file" << endm;
268  }
269 
270  mOutFile->SetCompressionLevel(mCompression);
271 
272  TTree *tree = mChain->GetTree();
273  mOutTree = new TTree("MuDst", "StMuDst", mSplit);
274 #if ROOT_VERSION_CODE < ROOT_VERSION(5,26,0)
275  Long64_t MAXLONG=100000000000LL; // 100 GB
276  LOG_INFO << "Tree size MAX will be " << (float) MAXLONG/1000/1000/1000 << " GB " << endm;
277  mOutTree->SetMaxTreeSize(MAXLONG); // limited to 1.9 GB - set to maximum
278 #endif
279  mOutTree = tree->CloneTree(0);
280 
281  DEBUGMESSAGE3("out");
282 }
283 //-----------------------------------------------------------------------
284 //-----------------------------------------------------------------------
285 void StMuIOMaker::closeMuWrite(){
286  DEBUGMESSAGE("");
287  LOG_INFO << __PRETTY_FUNCTION__ << endm;
288  if (mOutTree && mOutFile) {
289  LOG_INFO << " ##### " << __PRETTY_FUNCTION__ << " "
290  << " ##### " << endm;
291  LOG_INFO << " ##### File=" << mOutFile->GetName() << " "
292  << " ##### " << endm;
293  LOG_INFO << " ##### NumberOfEvents= " << mOutTree->GetEntries() << " "
294  << " ##### " << endm;
295  }
296  StTimer timer;
297  timer.reset();
298  timer.start();
299 
300  if(mOutTree) mOutTree->Write();
301  mOutTree = 0;
302  if(mOutFile) mOutFile->Close();
303  mOutFile = 0;
304 
305  timer.stop();
306  LOG_INFO << " Writing took " << timer.elapsedTime() << " seconds " << endm;
307  mCloseWrite = true;
308  DEBUGMESSAGE3("out");
309 }
310 /***************************************************************************
311  *
312  * $Log: StMuIOMaker.cxx,v $
313  * Revision 1.21 2011/08/18 18:41:36 fisyak
314  * set max. tree size = 100 GB
315  *
316  * Revision 1.20 2011/04/19 22:50:08 fisyak
317  * Use default size of TTree (100 GB) for ROOT >= 5.26.0
318  *
319  * Revision 1.19 2009/05/22 23:48:18 fine
320  * Test I/O errors after filling the TTree
321  *
322  * Revision 1.18 2009/05/22 22:25:31 fine
323  * Add the Zombue test for TFile ctors
324  *
325  * Revision 1.17 2009/04/28 22:15:10 perev
326  * CleanUp
327  *
328  * Revision 1.16 2009/03/10 23:43:53 jeromel
329  * Set tree size to max size
330  *
331  * Revision 1.15 2007/08/31 01:55:14 mvl
332  * Added protection against corrupted files by checking for return code -1 from TTree:GetEntry(). StMuDstMaker will silently skip these events; StMuIOMaker returns kStWarn.
333  *
334  * Revision 1.14 2007/05/16 18:50:48 mvl
335  * Cleanup of output. Replaced cout with LOG_INFO etc.
336  *
337  * Revision 1.13 2005/08/19 19:46:05 mvl
338  * Further updates for multiple vertices. The main changes are:
339  * 1) StMudst::primaryTracks() now returns a list (TObjArray*) of tracks
340  * belonging to the 'current' primary vertex. The index number of the
341  * 'current' vertex can be set using StMuDst::setCurrentVertex().
342  * This also affects StMuDst::primaryTracks(int i) and
343  * StMuDst::numberOfprimaryTracks().
344  * 2) refMult is now stored for all vertices, in StMuPrimaryVertex. The
345  * obvious way to access these numbers is from the StMuprimaryVertex structures,
346  * but for ebakcward compatibility a function is provided in StMuEvent as well
347  * (this is the only function taht works for existing MuDst)
348  *
349  * As an aside, I've also changes the internals of StMuDst::createStEvent and
350  * StMuDst::fixTrackIndices() to be able to deal with a larger range of index numbers for tracks as generated by Ittf.
351  *
352  * BIG FAT WARNING: StMudst2StEventMaker and StMuDstFilterMaker
353  * do not fully support the multiple vertex functionality yet.
354  *
355  * Revision 1.12 2004/10/19 01:46:46 mvl
356  * Removed call to StMuDstMaker::clear() in Make() (Obsolete)
357  *
358  * Revision 1.11 2004/07/02 01:51:09 perev
359  * Wei-Ming Zhang developments
360  *
361  * Revision 1.10 2004/04/20 18:49:16 perev
362  * Big reorganization, now StMuIOMkaer inherits from StMuDstMaker
363  *
364  * Revision 1.5 2004/04/02 03:24:54 jeromel
365  * Changes implements PMD and TOF. TOF is clearly incomplete.
366  *
367  * Revision 1.4 2004/02/17 04:56:36 jeromel
368  * Extended help, added crs support, restored __GNUC__ for PRETTY_FUNCTION(checked once
369  * more and yes, it is ONLY defined in GCC and so is __FUCTION__), use of a consistent
370  * internal __PRETTYF__, return NULL if no case selected (+message) and protected against
371  * NULL mChain.
372  *
373  * Revision 1.3 2003/09/11 05:49:20 perev
374  * ansi corrs
375  *
376  * Revision 1.2 2003/09/09 18:16:53 laue
377  * StMuIOMaker: embedded documentation added
378  * StMuTimer: name of define changed (was same as StTimer)
379  *
380  **************************************************************************/
381 
382 
383 
TTree * tree()
Returns pointer to the current TTree, the top level io structure that holds the event, track, v0, etc. information in branches of that tree.
Definition: StMuDstMaker.h:427
static void set(StMuDstMaker *maker)
set the pointers to the TClonesArrays
Definition: StMuDst.cxx:146
TString fTreeName
r=read,w=write,u=update
Definition: StIOInterFace.h:31
static void collectVertexTracks()
Helper function to collect tracks for the current prim vertex.
Definition: StMuDst.cxx:260
Definition: Stypes.h:43
Definition: Stypes.h:42
Definition: Stypes.h:40
int mCurrentIndex
holds the # of events in the current chain (file)
Definition: StMuIOMaker.h:82
TString fFile
Tree name.
Definition: StIOInterFace.h:32
bool mMuSave
holds the index of the last event read
Definition: StMuIOMaker.h:83
Definition: StFileI.h:13
Definition: Stypes.h:44
Definition: Stypes.h:41