00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "StarClassLibrary/StTimer.hh"
00011
00012 #include "StMessMgr.h"
00013 #include "StMuDebug.h"
00014 #include "StMuIOMaker.h"
00015 #include "StMuDst.h"
00016 #include "StMuEvent.h"
00017
00018 #include "TFile.h"
00019 #include "THack.h"
00020 #include "TChain.h"
00021 #include "TClonesArray.h"
00022
00023 #include "THack.h"
00024 ClassImp(StMuIOMaker)
00025
00026
00027
00028 StMuIOMaker::StMuIOMaker(const char* name, const char *ioFile) :
00029 StMuDstMaker(name)
00030 {
00031 mOutFile =0;
00032
00033
00034 mNumberOfEvents=0;
00035 mCurrentIndex =0;
00036 mEventCounter =0;
00037
00038 mSplit =99;
00039 mCompression =9;
00040 mBufferSize =65536*4;
00041 mMuSave =0;
00042 mBadInFile =0;
00043 mIoMode =0;
00044 mCloseWrite =1;
00045
00046
00047
00048
00049 }
00050
00051
00052
00053 StMuIOMaker::~StMuIOMaker() {
00054 DEBUGMESSAGE("");
00055 if(mMuSave && !mBadInFile && !mCloseWrite) closeMuWrite();
00056 DEBUGMESSAGE3("out");
00057 }
00058
00059
00060
00061
00070 int StMuIOMaker::Init(){
00071 DEBUGMESSAGE("");
00072 TDataSet *muDstSet = AddObj(mStMuDst,".const");
00073 if (muDstSet ) muDstSet ->SetName("MuDst");
00074
00075
00076 if(fTreeName == "MuSave") mMuSave = true;
00077
00078 DEBUGVALUE(mMuSave);
00079 DEBUGMESSAGE2("out");
00080 return 0;
00081 }
00082
00083
00084 int StMuIOMaker::Make(){
00085
00086 int iret=0,ntry=13;
00087 while(1999) {
00088 iret = MakeRead();
00089 if (iret!=kStErr && ntry--) break;
00090 Warning("Make","%d *** ReadError ***\n",ntry);
00091 }
00092 return iret;
00093 }
00094
00095
00111 int StMuIOMaker::Make(int index){
00112 DEBUGMESSAGE("");
00113
00114
00115 DEBUGVALUE3(index);
00116 mCurrentIndex = index;
00117 DEBUGVALUE3(mNumberOfEvents);
00118 if ( mCurrentIndex >= mNumberOfEvents) return kStEOF;
00119 if ( mCurrentIndex < 0 ) return kStErr;
00120 if (!mChain) return kStEOF;
00121 int bytes = mChain->GetEntry(mCurrentIndex);
00122 if (bytes <= 0) {
00123 LOG_WARN << "#### " << fFile.Data() << " could not read event " << index
00124 << ", TTree::GetEntry returned " << bytes << endm;
00125 return kStWarn;
00126 }
00127
00128 DEBUGVALUE(mMuSave);
00129 if(mMuSave) { mOutTree->Fill(); THack::IsTreeWritable(mOutTree); }
00130
00131
00132 DEBUGVALUE3(bytes);
00133 mStMuDst->set(this);
00134 mEventCounter++;
00135 fillHddr();
00136 mStMuDst->collectVertexTracks();
00137 return kStOk;
00138 }
00139
00140
00141 int StMuIOMaker::Make(int major, int minor){
00142 DEBUGMESSAGE("");
00143 DEBUGVALUE3(major);
00144 DEBUGVALUE3(minor);
00145 int index = mChain->GetEntryNumberWithIndex(major,minor);
00146 DEBUGMESSAGE("out");
00147 return Make(index);
00148 }
00149
00150
00151 int StMuIOMaker::Make(const StUKey& key){
00152 DEBUGMESSAGE("");
00153 int index = mChain->GetEntryNumberWithIndex(key.GetRunId(),key.GetEventId());
00154 DEBUGVALUE3(index);
00155 DEBUGMESSAGE("out");
00156 return Make(index);
00157 }
00158
00159
00160 int StMuIOMaker::Finish() {
00161 DEBUGMESSAGE("");
00162 LOG_INFO << "###### StMuIOMaker read " << mEventCounter << " events" << endm;
00163 if(mMuSave && !mBadInFile && !mCloseWrite) {
00164 if (mOutTree) mOutTree->AutoSave();
00165 closeMuWrite();
00166 }
00167 DEBUGMESSAGE3("out");
00168 return kStOK;
00169 }
00170
00171
00172
00173
00174 Int_t StMuIOMaker:: MakeRead(const StUKey &key) {
00175 DEBUGMESSAGE("");
00176 if(!mBadInFile) {
00177 if(key.IsNull()) {
00178 DEBUGMESSAGE("Seq");
00179 return Make(mCurrentIndex+1);
00180 }
00181 else {
00182 DEBUGMESSAGE("GC");
00183 return Make(key);
00184 }
00185 }
00186 else {
00187 DEBUGMESSAGE("BadFile");
00188 return kStEOF;
00189 }
00190 }
00191
00192
00193
00194 Int_t StMuIOMaker::Open(const char*) {
00195 DEBUGMESSAGE("");
00196 mBadInFile = false;
00197 mCloseWrite = true;
00198 int iret = openRead();
00199 if(mMuSave && !mBadInFile) {
00200 openMuWrite();
00201 mCloseWrite = false;
00202 }
00203 return iret;
00204 }
00205
00206
00207
00208 void StMuIOMaker::Close(Option_t *) {
00209 DEBUGMESSAGE("");
00210 if(mMuSave && !mBadInFile && !mCloseWrite) closeMuWrite();
00211 closeRead();
00212 DEBUGMESSAGE3("out");
00213 }
00214
00215
00216
00217
00218 int StMuIOMaker::openRead() {
00219 DEBUGMESSAGE("");
00220 DEBUGVALUE(fFile.Data());
00221 mChain = new TChain("MuDst");
00222
00223 mChain->Add(fFile.Data());
00224 DEBUGVALUE3(mChain);
00225
00226 mNumberOfEvents = (int)mChain->GetEntries();
00227
00228 if(mNumberOfEvents == 1234567890) {
00229 LOG_WARN << "#### " << fFile.Data() << " Corrupted!" << endm;
00230 mBadInFile = true;
00231 closeRead();
00232 return kStOK;
00233 }
00234
00235 StTimer timer;
00236 timer.reset();
00237 timer.start();
00238
00239 setBranchAddresses(mChain);
00240 mChain->BuildIndex("MuEvent.mEventInfo.mRunId","MuEvent.mEventInfo.mId");
00241 timer.stop();
00242 LOG_INFO << " Index of " << mNumberOfEvents << " events buit in "
00243 << timer.elapsedTime() << " seconds " << endm;
00244
00245
00246 mCurrentIndex = -1;
00247 DEBUGMESSAGE3("out");
00248 return kStOK;
00249 }
00250
00251
00252
00253 void StMuIOMaker::openMuWrite() {
00254 DEBUGMESSAGE("");
00255 mOutFileName = fFile.Data();
00256 while (mOutFileName.find("/")!=string::npos) {
00257 int pos = mOutFileName.find("/");
00258 mOutFileName.erase(0,pos+1);
00259 }
00260 int pos = mOutFileName.find("MuDst");
00261 mOutFileName = mOutFileName.insert(pos,"Sel.");
00262 DEBUGVALUE(mOutFileName.c_str());
00263
00264 mOutFile = new TFile(mOutFileName.c_str(),"RECREATE","StMuDst");
00265 if (mOutFile->IsZombie()) {
00266 LOG_FATAL << "StMuIOMaker::openMuWrite" << " Can not create TFile object for "
00267 << mOutFileName.c_str() << " file" << endm;
00268 }
00269
00270 mOutFile->SetCompressionLevel(mCompression);
00271
00272 TTree *tree = mChain->GetTree();
00273 mOutTree = new TTree("MuDst", "StMuDst", mSplit);
00274 #if ROOT_VERSION_CODE < ROOT_VERSION(5,26,0)
00275 Long64_t MAXLONG=100000000000LL;
00276 LOG_INFO << "Tree size MAX will be " << (float) MAXLONG/1000/1000/1000 << " GB " << endm;
00277 mOutTree->SetMaxTreeSize(MAXLONG);
00278 #endif
00279 mOutTree = tree->CloneTree(0);
00280
00281 DEBUGMESSAGE3("out");
00282 }
00283
00284
00285 void StMuIOMaker::closeMuWrite(){
00286 DEBUGMESSAGE("");
00287 LOG_INFO << __PRETTY_FUNCTION__ << endm;
00288 if (mOutTree && mOutFile) {
00289 LOG_INFO << " ##### " << __PRETTY_FUNCTION__ << " "
00290 << " ##### " << endm;
00291 LOG_INFO << " ##### File=" << mOutFile->GetName() << " "
00292 << " ##### " << endm;
00293 LOG_INFO << " ##### NumberOfEvents= " << mOutTree->GetEntries() << " "
00294 << " ##### " << endm;
00295 }
00296 StTimer timer;
00297 timer.reset();
00298 timer.start();
00299
00300 if(mOutTree) mOutTree->Write();
00301 mOutTree = 0;
00302 if(mOutFile) mOutFile->Close();
00303 mOutFile = 0;
00304
00305 timer.stop();
00306 LOG_INFO << " Writing took " << timer.elapsedTime() << " seconds " << endm;
00307 mCloseWrite = true;
00308 DEBUGMESSAGE3("out");
00309 }
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383