Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

NuInputEvents.cxx

Go to the documentation of this file.
00001 
00002 
00003 // Coded by Jeff Hartnell Jan/2007 onwards
00004 //                                                                    
00005 // Contact: j.j.hartnell@rl.ac.uk
00007 
00008 #include <cassert>
00009 #include <fstream>
00010 
00011 #include "glob.h" // For globbing that works better than ROOT's
00012 
00013 #include "TChain.h"
00014 #include "TFile.h"
00015 #include "TMath.h"
00016 #include "TObjString.h"
00017 #include "TRegexp.h"
00018 #include "TSystem.h"
00019 
00020 #include "MessageService/MsgService.h"
00021 
00022 #include "NtupleUtils/NuInputEvents.h"
00023 
00024 using std::endl;
00025 using std::cout;
00026 using std::vector;
00027 
00028 string NuInputEvents::fInputFileName="";
00029 
00030 CVSID("$Id: NuInputEvents.cxx,v 1.9 2009/11/06 15:39:47 bckhouse Exp $");
00031 
00032 //......................................................................
00033 
00034 NuInputEvents::NuInputEvents()
00035 {
00036   MSG("NuInputEvents",Msg::kDebug)
00037     <<"Running NuInputEvents Constructor..."<<endl;
00038   
00039   //initialise data members
00040   fUseCachedNuEvents=false;//use tree as std
00041   fUseCachedNuMCEvents=false;//use tree as std
00042   fpvNuEvents=0;
00043   fpvNuMCEvents=0;
00044   //fNuEventsIter;//what to do with this?
00045   //fNuMCEventsIter;//what to do with this?
00046   fInputFileDir=0;
00047   fEntriesNuEvent=0;
00048   fFirstRunNumberNuEvent=999;
00049   this->ResetNuEventTreePositionToStart();//sets fEntry=-1;
00050   fEntriesNuMCEvent=0;
00051   fFirstRunNumberNuMCEvent=999;
00052   this->ResetNuMCEventTreePositionToStart();//sets fEntry=-1;
00053   fNuEvent=0;
00054   fNuMCEvent=0;
00055   fChainNuEvent=0;
00056   fChainNuMCEvent=0;
00057 
00058   MSG("NuInputEvents",Msg::kDebug)
00059     <<"Finished NuInputEvents Constructor"<<endl;
00060 }
00061 
00062 //......................................................................
00063 
00064 NuInputEvents::~NuInputEvents()
00065 {
00066   MSG("NuInputEvents",Msg::kDebug)
00067     <<"Running NuInputEvents Destructor..."<<endl;
00068   
00069   //clean up memory
00070   if (fChainNuEvent) {
00071     delete fChainNuEvent;
00072     fChainNuEvent=0;
00073   }
00074   //clean up memory
00075   if (fChainNuMCEvent) {
00076     delete fChainNuMCEvent;
00077     fChainNuMCEvent=0;
00078   }
00079   //fNuEvent is only ever used as a pointer and doesn't own any memory
00080 
00081   MSG("NuInputEvents",Msg::kDebug)
00082     <<"Finished NuInputEvents Destructor"<<endl;
00083 }
00084 
00085 //......................................................................
00086 
00087 void NuInputEvents::InitialiseChains()
00088 {
00089   if (fChainNuMCEvent || fChainNuEvent) {
00090     MSG("NuInputEvents",Msg::kInfo)
00091       <<"Chains already initialised... nothing to do"<<endl;
00092   }
00093   else this->MakeChain();
00094 }
00095 
00096 //......................................................................
00097 
00098 TDirectory* NuInputEvents::OpenNextInputFile() 
00099 {
00100   //check if input file name exists
00101   if (fInputFileName=="") {
00102     MSG("NuInputEvents",Msg::kWarning)
00103       <<"No input file given so returning NULL, fInputFileName="
00104       <<fInputFileName<<endl;
00105     return NULL;
00106   }
00107 
00108   //create a vector to store the list of files
00109   static vector<string> fileList;
00110   if (fileList.size()==0) {
00111     MSG("NuInputEvents",Msg::kInfo)
00112       <<"Searching for files..."<<endl;
00113     fileList=this->GetListOfFilesInDir(fInputFileName);
00114     
00115     //check if file found
00116     if (fileList.size()==0) {
00117       MSG("NuInputEvents",Msg::kWarning)
00118         <<"No file found so returning NULL, fInputFileName="
00119         <<fInputFileName<<endl;
00120       return NULL;
00121     }
00122 
00123     //print out files found    
00124     MSG("NuInputEvents",Msg::kInfo)
00125       <<"Found "<<fileList.size()<<" files:"<<endl;
00126     for (vector<string>::iterator it=fileList.begin();
00127          it!=fileList.end();++it) {
00128       MSG("NuInputEvents",Msg::kInfo)
00129         <<"  File name="<<*it<<endl;
00130     }
00131   }
00132   
00133   //get an iterator
00134   static vector<string>::iterator it=fileList.begin();
00135   
00136   //check if iterator points to anything
00137   if (it==fileList.end()) return NULL;
00138   
00139   //to avoid changing the global configuration
00140   //get the original directory
00141   TDirectory* directoryOrig=gDirectory;
00142   //cout<<"Original directory is:"<<endl;
00143   //directoryOrig->Print();
00144   
00145   //keep a pointer to the file
00146   static TFile* file=0;
00147   static TFile* firstFile=0;
00148   //close the file if it exists
00149   //don't close the first file... the clones of histos in the first
00150   //file seem to disappear if the file is closed
00151   //not closing the first file is a nasty work around 
00152   if (file) {
00153     if (file!=firstFile) {
00154       cout<<"Closing file="<<file->GetName()<<endl;
00155       file->Close();
00156       file=0;//reset the pointer
00157     }
00158     else cout<<"Not closing the first file to be opened (avoids a segv)"
00159              <<endl;
00160   }
00161 
00162   //open the new file
00163   cout<<"Opening file... name="<<(*it).c_str()<<endl;
00164   file=TFile::Open((*it).c_str());
00165   if (!firstFile) firstFile=file;
00166 
00167   //increment the iterator
00168   ++it;
00169 
00170   //store the directory of the file
00171   fInputFileDir=gDirectory;
00172   
00173   //reset the global directory to the original directory
00174   gDirectory=directoryOrig;
00175   
00176   return fInputFileDir;
00177 }
00178 
00179 //......................................................................
00180 
00181 TDirectory* NuInputEvents::OpenInputFile() 
00182 {
00183   if (!fInputFileDir) {
00184     //to avoid changing the global configuration
00185     //get the original directory
00186     TDirectory* directoryOrig=gDirectory;
00187     //cout<<"Original directory is:"<<endl;
00188     //directoryOrig->Print();
00189     
00190     if (fInputFileName=="") return NULL;
00191     
00192     vector<string> fileList;
00193     fileList=this->GetListOfFilesInDir(fInputFileName);
00194     
00195     //print out files found    
00196     MSG("NuInputEvents",Msg::kInfo)
00197       <<"Found "<<fileList.size()<<" files:"<<endl;
00198     for (vector<string>::iterator it=fileList.begin();
00199          it!=fileList.end();++it) {
00200       MSG("NuInputEvents",Msg::kInfo)
00201         <<"  File name="<<*it<<endl;
00202     }
00203 
00204     if (fileList.size()>0) {
00205       cout<<"Opening (1st) file="<<fileList[0]<<endl;
00206       TFile::Open(fileList[0].c_str());
00207     }
00208     else {
00209       MSG("NuInputEvents",Msg::kWarning)
00210         <<"No file found so returning NULL, fInputFileName="
00211         <<fInputFileName<<endl;
00212       return NULL;
00213     }
00214     
00215     //store the directory of the file
00216     fInputFileDir=gDirectory;
00217     
00218     //reset the global directory to the original directory
00219     gDirectory=directoryOrig;
00220   }
00221   else {
00222     MSG("NuInputEvents",Msg::kWarning)
00223       <<"A file has already been opened, fInputFileName="
00224       <<fInputFileName
00225       <<", fInputFileDir="
00226       <<endl;
00227     fInputFileDir->Print();
00228   }
00229 
00230   return fInputFileDir;
00231 }
00232 
00233 //......................................................................
00234 
00235 void NuInputEvents::ResetNuEventLoopPositionToStart()
00236 {
00239   if (fUseCachedNuEvents) {
00240     this->ResetNuEventCachePositionToStart();
00241   }
00242   else {
00243     this->ResetNuEventTreePositionToStart();
00244   }
00245 }
00246 
00247 //......................................................................
00248 
00249 void NuInputEvents::ResetNuMCEventLoopPositionToStart()
00250 {
00253   if (fUseCachedNuMCEvents) {
00254     this->ResetNuMCEventCachePositionToStart();
00255   }
00256   else {
00257     this->ResetNuMCEventTreePositionToStart();
00258   }
00259 }
00260 
00261 //......................................................................
00262 
00263 void NuInputEvents::ResetNuEventTreePositionToStart()
00264 {
00265   //this is one before the first so that GetNext works properly
00266   fEntryNuEvent=-1;
00267 }
00268 
00269 //......................................................................
00270 
00271 void NuInputEvents::ResetNuMCEventTreePositionToStart()
00272 {
00273   //this is one before the first so that GetNext works properly
00274   fEntryNuMCEvent=-1;
00275 }
00276 
00277 //......................................................................
00278 
00279 void NuInputEvents::ResetNuEventCachePositionToStart()
00280 {
00281   if (fpvNuEvents) {
00282     fNuEventsIter=fpvNuEvents->begin();
00283     //this is one before the first so that GetNext works properly
00284     --fNuEventsIter;
00285   }
00286   else {
00287     MSG("NuInputEvents",Msg::kError) 
00288       <<"Can't reset cache position to start before cache is created"
00289       <<endl
00290       <<"Will exit here..."<<endl;
00291     assert(false);
00292   }
00293 }
00294 
00295 //......................................................................
00296 
00297 void NuInputEvents::ResetNuMCEventCachePositionToStart()
00298 {
00299   if (fpvNuMCEvents) {
00300     fNuMCEventsIter=fpvNuMCEvents->begin();
00301     //this is one before the first so that GetNext works properly
00302     --fNuMCEventsIter;
00303   }
00304   else {
00305     MSG("NuInputEvents",Msg::kError) 
00306       <<"Can't reset cache position to start before cache is created"
00307       <<endl
00308       <<"Will exit here..."<<endl;
00309     assert(false);
00310   }
00311 }
00312 
00313 //......................................................................
00314 
00315 Int_t NuInputEvents::GetEntriesNuEvent() const
00316 {
00317   if (fUseCachedNuEvents) {
00318     return this->GetCacheEntriesNuEvent();
00319   }
00320   else {
00321     return this->GetTreeEntriesNuEvent();
00322   }
00323 }
00324 
00325 //......................................................................
00326 
00327 Int_t NuInputEvents::GetEntriesNuMCEvent() const
00328 {
00329   if (fUseCachedNuMCEvents) {
00330     return this->GetCacheEntriesNuMCEvent();
00331   }
00332   else {
00333     return this->GetTreeEntriesNuMCEvent();
00334   }
00335 }
00336 
00337 //......................................................................
00338 
00339 Int_t NuInputEvents::GetTreeEntriesNuEvent() const
00340 {
00341   if (fChainNuEvent) {
00342     return fEntriesNuEvent;
00343   }
00344   else {
00345     MSG("NuInputEvents",Msg::kError) 
00346       <<"Can't get NuEvent tree entries before chain is created"<<endl
00347       <<"Will exit here..."<<endl;
00348     assert(false);
00349     return -1;//keep the compiler quiet
00350   }
00351 }
00352 
00353 //......................................................................
00354 
00355 Int_t NuInputEvents::GetTreeEntriesNuMCEvent() const
00356 {
00357   if (fChainNuMCEvent) {
00358     return fEntriesNuMCEvent;
00359   }
00360   else {
00361     //MSG("NuInputEvents",Msg::kError) 
00362     //<<"Can't get NuMCEvent tree entries before chain is created"<<endl
00363     //<<"Will exit here..."<<endl;
00364     //assert(false);
00365     //return -1;//keep the compiler quiet
00366     
00367     MSG("NuInputEvents",Msg::kWarning) 
00368       <<"Can't get NuMCEvent tree entries when chain doesn't exist."
00369       <<" Returning ZERO entries"<<endl;
00370     return 0;
00371   }
00372 }
00373 
00374 //......................................................................
00375 
00376 Int_t NuInputEvents::GetCacheEntriesNuEvent() const
00377 {
00378   if (fpvNuEvents) {
00379     return fpvNuEvents->size();
00380   }
00381   else {
00382     MSG("NuInputEvents",Msg::kError) 
00383       <<"Can't get NuEvent cache entries before cache is created"<<endl
00384       <<"Will exit here..."<<endl;
00385     assert(false);
00386     return -1;//keep the compiler quiet
00387   }
00388 }
00389 
00390 //......................................................................
00391 
00392 Int_t NuInputEvents::GetCacheEntriesNuMCEvent() const
00393 {
00394   if (fpvNuMCEvents) {
00395     return fpvNuMCEvents->size();
00396   }
00397   else {
00398     MSG("NuInputEvents",Msg::kError) 
00399       <<"Can't get NuMCEvent cache entries before cache is created"
00400       <<endl
00401       <<"Will exit here..."<<endl;
00402     assert(false);
00403     return -1;//keep the compiler quiet
00404   }
00405 }
00406 
00407 //......................................................................
00408 
00409 void NuInputEvents::PrintNewFileName() const 
00410 {
00411   static int lastFileno = -1;
00412   if (fUseCachedNuEvents) return;
00413   int current = fChainNuEvent->GetTreeNumber();
00414   
00415   if (current > lastFileno) {
00416     lastFileno = current;
00417     MSG("NuInputEvents",Msg::kInfo) << "*** New File: " << fChainNuEvent->GetFile()->GetName() << endl;
00418   }
00419   
00420 }
00421 
00422 //......................................................................
00423 
00424 void NuInputEvents::PrintNewMCFileName() const 
00425 {
00426   static int lastFileno = -1;
00427   if (fUseCachedNuMCEvents) return;
00428   int current = fChainNuMCEvent->GetTreeNumber();
00429   
00430   if (current > lastFileno) {
00431     lastFileno = current;
00432     MSG("NuInputEvents",Msg::kInfo) << "*** New File: " << fChainNuMCEvent->GetFile()->GetName() << endl;
00433   }
00434   
00435 }
00436 
00437 //......................................................................
00438 
00439 void NuInputEvents::SetLoopVariablesNuEvent(Int_t entry,Int_t printMode)
00440 {
00441   if (printMode==1){
00442     Float_t fract=ceil(fEntriesNuEvent/20.);  
00443     if (ceil(((Float_t)entry)/fract)==((Float_t)entry)/fract){
00444       MSG("NuInputEvents",Msg::kDebug) 
00445         <<"Fraction of loop complete: "<<entry 
00446         <<"/"<<fEntriesNuEvent<<"  ("
00447         <<(Int_t)(100.*entry/fEntriesNuEvent)<<"%)"<<endl;
00448     }
00449   }
00450 
00451   //get the snarl/entry
00452   fChainNuEvent->GetEntry(entry); 
00453 }
00454 
00455 //......................................................................
00456 
00457 void NuInputEvents::SetLoopVariablesNuMCEvent(Int_t entry,Int_t printMode)
00458 {
00459   if (printMode==1){
00460     Float_t fract=ceil(fEntriesNuMCEvent/20.);  
00461     if (ceil(((Float_t)entry)/fract)==((Float_t)entry)/fract){
00462       MSG("NuInputEvents",Msg::kDebug) 
00463         <<"Fraction of loop complete: "<<entry 
00464         <<"/"<<fEntriesNuMCEvent<<"  ("
00465         <<(Int_t)(100.*entry/fEntriesNuMCEvent)<<"%)"<<endl;
00466     }
00467   }
00468 
00469   //get the snarl/entry
00470   fChainNuMCEvent->GetEntry(entry); 
00471 }
00472 
00473 //......................................................................
00474 
00475 void NuInputEvents::UseNuEventCache(Bool_t useCache)
00476 {
00477   if (useCache==true) {
00478     MSG("NuInputEvents",Msg::kInfo) 
00479       <<"Setting NuInputEvents object to use the cache"<<endl;
00480     fUseCachedNuEvents=true;
00481   }
00482   else {
00483     MSG("NuInputEvents",Msg::kInfo) 
00484       <<"Setting NuInputEvents object to use the tree"<<endl;
00485     fUseCachedNuEvents=false;
00486   }
00487 }
00488 
00489 //......................................................................
00490 
00491 void NuInputEvents::UseNuMCEventCache(Bool_t useCache)
00492 {
00493   if (useCache==true) {
00494     MSG("NuInputEvents",Msg::kInfo) 
00495       <<"Setting NuInputEvents object to use the cache"<<endl;
00496     fUseCachedNuMCEvents=true;
00497   }
00498   else {
00499     MSG("NuInputEvents",Msg::kInfo) 
00500       <<"Setting NuInputEvents object to use the tree"<<endl;
00501     fUseCachedNuMCEvents=false;
00502   }
00503 }
00504 
00505 //......................................................................
00506 
00507 void NuInputEvents::InitialiseNuEventCache()
00508 {
00509   MSG("NuInputEvents",Msg::kInfo) 
00510     <<"Running NuInputEvents::InitialiseNuEventCache"<<endl;
00511 
00512   //create the vector of NuEvents and initialise
00513   fpvNuEvents=new vector<NuEvent*>;
00514 
00515   //make space in memory for objects
00516   fpvNuEvents->reserve(50000);
00517 
00518   //initialise the iterator
00519   fNuEventsIter=fpvNuEvents->begin();
00520   //this is one before the first so that GetNext works properly
00521   --fNuEventsIter;
00522 
00523   MSG("NuInputEvents",Msg::kInfo) 
00524     <<"   Cache size="<<fpvNuEvents->size()<<endl;
00525 }
00526 
00527 //......................................................................
00528 
00529 void NuInputEvents::InitialiseNuMCEventCache()
00530 {
00531   MSG("NuInputEvents",Msg::kInfo) 
00532     <<"Running NuInputEvents::InitialiseNuMCEventCache"<<endl;
00533 
00534   //create the vector of NuMCEvents and initialise
00535   fpvNuMCEvents=new vector<NuMCEvent*>;
00536 
00537   //make space in memory for objects
00538   fpvNuMCEvents->reserve(50000);
00539 
00540   //initialise the iterator
00541   fNuMCEventsIter=fpvNuMCEvents->begin();
00542   //this is one before the first so that GetNext works properly
00543   --fNuMCEventsIter;
00544 
00545   MSG("NuInputEvents",Msg::kInfo) 
00546     <<"   Cache size="<<fpvNuMCEvents->size()<<endl;
00547 }
00548 
00549 //......................................................................
00550 
00551 void NuInputEvents::AddNuEventToCache(const NuEvent& nu)
00552 {
00553   //create a new NuEvent on the heap
00554   NuEvent* nuClone=new NuEvent();
00555 
00556   //clone the NuEvent
00557   *nuClone=nu;
00558 
00559   //add the nuClone to the cache
00560   if (fpvNuEvents) {
00561     fpvNuEvents->push_back(nuClone);
00562   }
00563   else cout<<"Ahhhh, fpvNuEvents==0, can't AddNuEventToCache"<<endl;
00564 }
00565 
00566 //......................................................................
00567 
00568 void NuInputEvents::AddNuMCEventToCache(const NuMCEvent& nu)
00569 {
00570   //create a new NuMCEvent on the heap
00571   NuMCEvent* nuClone=new NuMCEvent();
00572 
00573   //clone the NuMCEvent
00574   *nuClone=nu;
00575 
00576   //add the nuClone to the cache
00577   if (fpvNuMCEvents) {
00578     fpvNuMCEvents->push_back(nuClone);
00579   }
00580   else cout<<"Ahhhh, fpvNuMCEvents==0, can't AddNuMCEventToCache"<<endl;
00581 }
00582 
00583 //......................................................................
00584 
00585 const NuEvent& NuInputEvents::GetNextCachedNuEvent(Msg::LogLevel_t
00586                                                    /*logLevel*/)
00587 {
00589 
00590   //increment the iterator to the next nuEvent
00591   ++fNuEventsIter;
00592 
00593   //return the NuEvent
00594   return *(*fNuEventsIter);
00595 }
00596 
00597 //......................................................................
00598 
00599 const NuMCEvent& NuInputEvents::GetNextCachedNuMCEvent(Msg::LogLevel_t
00600                                                    /*logLevel*/)
00601 {
00603 
00604   //increment the iterator to the next nuEvent
00605   ++fNuMCEventsIter;
00606 
00607   //return the NuMCEvent
00608   return *(*fNuMCEventsIter);
00609 }
00610 
00611 //......................................................................
00612 
00613 const NuEvent& NuInputEvents::GetNextTreeNuEvent(Msg::LogLevel_t
00614                                                  logLevel)
00615 {
00616 
00617   if (fEntryNuEvent==-1) {
00618     MSG("NuInputEvents",Msg::kInfo) 
00619       <<"Running NuInputEvents::GetNextNuEvent for first entry..."
00620       <<endl;
00621   }
00622   else if (fEntryNuEvent>=fEntriesNuEvent) {
00623     MSG("NuInputEvents",Msg::kError) 
00624       <<"Asked for entry="<<fEntryNuEvent
00625       <<" beyond end of tree. Total tree entries="
00626       <<fEntriesNuEvent<<endl
00627       <<"Will exit here..."<<endl;
00628     assert(false);
00629   }
00630   
00631   //increment the entry to get
00632   ++fEntryNuEvent;
00633   
00634   if (logLevel>=Msg::kInfo){
00635     Float_t fract=ceil(fEntriesNuEvent/20.);  
00636     if (ceil(((Float_t)fEntryNuEvent)/fract)==
00637         ((Float_t)fEntryNuEvent)/fract){
00638       MSG("NuInputEvents",Msg::kInfo) 
00639         <<"Fraction of loop complete: "<<fEntryNuEvent 
00640         <<"/"<<fEntriesNuEvent<<"  ("
00641         <<(Int_t)(100.*fEntryNuEvent/fEntriesNuEvent)<<"%)"<<endl;
00642     }
00643   }
00644 
00645   //get the snarl/entry
00646   fChainNuEvent->GetEntry(fEntryNuEvent); 
00647 
00648   //return the next NuEvent
00649   return *fNuEvent;
00650 }
00651 
00652 //......................................................................
00653 
00654 const NuMCEvent& NuInputEvents::GetNextTreeNuMCEvent(Msg::LogLevel_t
00655                                                  logLevel)
00656 {
00657 
00658   if (fEntryNuMCEvent==-1) {
00659     MSG("NuInputEvents",Msg::kInfo) 
00660       <<"Running NuInputEvents::GetNextNuMCEvent for first entry..."
00661       <<endl;
00662   }
00663   else if (fEntryNuMCEvent>=fEntriesNuMCEvent) {
00664     MSG("NuInputEvents",Msg::kError) 
00665       <<"Asked for entry="<<fEntryNuMCEvent
00666       <<" beyond end of tree. Total tree entries="
00667       <<fEntriesNuMCEvent<<endl
00668       <<"Will exit here..."<<endl;
00669     assert(false);
00670   }
00671   
00672   //increment the entry to get
00673   ++fEntryNuMCEvent;
00674   
00675   if (logLevel>=Msg::kInfo){
00676     Float_t fract=ceil(fEntriesNuMCEvent/20.);  
00677     if (ceil(((Float_t)fEntryNuMCEvent)/fract)==
00678         ((Float_t)fEntryNuMCEvent)/fract){
00679       MSG("NuInputEvents",Msg::kInfo) 
00680         <<"Fraction of loop complete: "<<fEntryNuMCEvent 
00681         <<"/"<<fEntriesNuMCEvent<<"  ("
00682         <<(Int_t)(100.*fEntryNuMCEvent/fEntriesNuMCEvent)<<"%)"<<endl;
00683     }
00684   }
00685 
00686   //get the snarl/entry
00687   fChainNuMCEvent->GetEntry(fEntryNuMCEvent); 
00688 
00689   //return the next NuMCEvent
00690   return *fNuMCEvent;
00691 }
00692 
00693 //......................................................................
00694 
00695 const NuEvent& NuInputEvents::GetNextNuEvent(Msg::LogLevel_t logLevel)
00696 {
00697   const NuEvent* pnu=0;
00698   
00699   if (fUseCachedNuEvents) {
00700     pnu=&this->GetNextCachedNuEvent(logLevel);
00701   }
00702   else {
00703     pnu=&this->GetNextTreeNuEvent(logLevel);
00704   }
00705 
00706   return *pnu;
00707 }
00708 
00709 //......................................................................
00710 
00711 const NuMCEvent& NuInputEvents::GetNextNuMCEvent(Msg::LogLevel_t logLevel)
00712 {
00713   const NuMCEvent* pnu=0;
00714   
00715   if (fUseCachedNuMCEvents) {
00716     pnu=&this->GetNextCachedNuMCEvent(logLevel);
00717   }
00718   else {
00719     pnu=&this->GetNextTreeNuMCEvent(logLevel);
00720   }
00721 
00722   return *pnu;
00723 }
00724 
00725 //......................................................................
00726 
00727 void NuInputEvents::InputFileName(string f)
00728 {
00729   if (f!=""){
00730     MSG("NuInputEvents",Msg::kInfo)
00731       <<"Running with input file name="<<f<<endl;
00732     fInputFileName=f;
00733   }
00734 }
00735 
00736 //......................................................................
00737 
00738 std::vector<std::string> NuInputEvents::MakeFileList()
00739 {
00741 
00742   vector<string> fileList;
00743   
00744   if (fInputFileName!=""){
00745     MSG("NuInputEvents",Msg::kInfo)
00746       <<"Running NuInputEvents::MakeFileList with fInputFileName="
00747       <<fInputFileName<<endl;
00748     
00749     Int_t findGives=fInputFileName.find(".root");
00750     //cout<<"find gives="<<fInputFileName.find(".root")<<endl;
00751 
00752     //check if there is a wildcard
00753     if (findGives>0 && 
00754         TString(fInputFileName.c_str()).MaybeWildcard()) {
00755       MSG("NuInputEvents",Msg::kInfo)
00756         <<"Found wildcard so getting list of files in directory"<<endl;
00757       fileList=this->GetListOfFilesInDir(fInputFileName);
00758       if(fileList.empty())
00759         MSG("NuInputEvents", Msg::kError)
00760           << "No files matched wildcard, will fall back to NUEVENT" << endl;
00761     }
00762     else if (findGives>0){
00763       MSG("NuInputEvents",Msg::kInfo)
00764         <<"Adding file direct to list"<<endl;
00765       //add the file direct to the list
00766       fileList.push_back(fInputFileName);
00767     }
00768     else{//treat the file as a list of root files 
00769       ifstream inputFile(fInputFileName.c_str());
00770       
00771       //check if file exists
00772       if (inputFile){
00773         //variables to hold input from file
00774         string file="";
00775         
00776         //read in from the text file and fill objects
00777         while(inputFile>>file) {
00778           MSG("NuInputEvents",Msg::kInfo)
00779             <<"Found input file name="<<file<<endl;
00780           fileList.push_back(file);
00781         }
00782         MSG("NuInputEvents",Msg::kInfo)
00783           <<"Files names found in txt file="<<fileList.size()<<endl;
00784       }
00785       else{
00786         MSG("NuInputEvents",Msg::kError)
00787           <<endl<<endl
00788           <<"**********************************************************"
00789           <<endl<<"Input txt file of file names does not exist!"<<endl
00790           <<"InputFileName="<<fInputFileName<<endl
00791           <<"**********************************************************"
00792           <<endl<<endl<<"Program will exit here"<<endl;
00793         exit(0);
00794       }
00795     }
00796   }
00797   //return the fileList if files were found
00798   if (fileList.size()>0) return fileList;
00799   
00800   //Check the env variable to find files
00801   char* envVariable=getenv("NUEVENT");
00802   if (envVariable==NULL){
00803     MSG("NuInputEvents",Msg::kError)
00804       <<endl<<endl
00805       <<"*************************************************************"
00806       <<endl<<"Environmental variable NUEVENT not set!"<<endl
00807       <<"Please set NUEVENT to the directory containing the"
00808       <<" NuEvent*.root files"<<endl
00809       <<"Note: If more than one file is found they will be"
00810       <<" concatenated and treated as one"<<endl
00811       <<"*************************************************************"
00812       <<endl<<endl<<"Program will exit here"<<endl;
00813     exit(0);
00814   }
00815   string sEnv=envVariable;
00816   MSG("NuInputEvents",Msg::kInfo)
00817     <<"Looking for NuEvent*.root files using the env variable"<<endl
00818     <<"NUEVENT="<<sEnv<<endl;
00819   sEnv+="/NuEvent*.root";
00820   fileList.push_back(sEnv);
00821 
00822   return fileList;
00823 }
00824 
00825 //......................................................................
00826 
00827 std::vector<std::string> NuInputEvents::
00828 GetListOfFilesInDir(std::string wildcardString) const
00829 {
00830   std::vector<std::string> fileList;
00831   
00832   glob_t g;
00833   glob(wildcardString.c_str(),
00834        // GLOB_NOCHECK | // If no match return pattern
00835        GLOB_TILDE, // Expand ~'s
00836        0, &g);
00837   for(unsigned int i = 0; i < g.gl_pathc; ++i)
00838     fileList.push_back(g.gl_pathv[i]);
00839   globfree(&g);
00840 
00841   // glob output is sorted by default
00842 
00843   return fileList;
00844 }
00845 
00846 //......................................................................
00847 
00848 void NuInputEvents::MakeChain()
00849 {
00850   //get the files to open
00851   vector<string> fileList=this->MakeFileList();
00852   
00853   //sanity check
00854   if (fileList.size()<=0) cout<<"Ahhh no files in fileList"<<endl;
00855   
00856   //open the file
00857   TFile* file=TFile::Open((*fileList.begin()).c_str(),"READ");
00858   
00859   //check if the NuEvent exists
00860   if (this->ObjectExistsInFile(file,"s")) {
00861     //create a chain with NuEvent tree
00862     fChainNuEvent=new TChain("s");
00863   }
00864   else {
00865     MSG("NuInputEvents",Msg::kInfo)
00866       <<"Cannot run MakeChain when tree does not exist in file."
00867       <<" Skipping this step..."<<endl;
00868     return;
00869   }
00870   
00871   //check if NuMCEvent exists
00872   if (this->ObjectExistsInFile(file,"mc")) {
00873     //create a chain with NuEvent tree
00874     fChainNuMCEvent=new TChain("mc");
00875     //fChainNuMCEvent->Print();
00876   }
00877       
00878   //add the files to the chain  
00879   Int_t nfNuEvent=0;
00880   Int_t nfNuMCEvent=0;
00881   for (vector<string>::iterator file=fileList.begin();
00882        file!=fileList.end();++file){
00883     
00884     //test if file already exists
00885     ifstream openOk((*file).c_str()); 
00886 
00887     //check if a wildcard was used because ifstream can't open wildcards
00888     Int_t stars=(*file).find("*");
00889     Int_t quest=(*file).find("?");
00890 
00891     //check if file existed
00892     if (!openOk && !(quest>=0 || stars>=0)){
00893       MSG("NuInputEvents",Msg::kInfo)
00894         <<endl<<endl
00895         <<"***********************************************************"
00896         <<endl<<"Can't find file="<<*file<<endl
00897         <<"Note: you can't use '~/'. It has to be the full path"<<endl
00898         <<"***********************************************************"
00899         <<endl<<endl
00900         <<"Exiting here!"<<endl;
00901       exit(0);
00902     }
00903     
00904     if (fChainNuEvent) {
00905       MSG("NuInputEvents",Msg::kInfo)<<"Adding file="<<*file<<endl;
00906       nfNuEvent+=fChainNuEvent->Add((*file).c_str());
00907       if (fChainNuMCEvent) {
00908         nfNuMCEvent+=fChainNuMCEvent->Add((*file).c_str());
00909       }
00910     }
00911     else {
00912       MSG("NuInputEvents",Msg::kWarning)
00913         <<"fChainNuEvent is a null pointer"<<endl;
00914     }
00915   }
00916 
00917   if(nfNuEvent==0){
00918     MSG("NuInputEvents",Msg::kError)
00919       <<endl<<endl
00920       <<"*************************************************************"
00921       <<endl<<"No NuEvent*.root files found"<<endl
00922       <<"Please set NUEVENT to the directory containing the"
00923       <<" NuEvent*.root files"<<endl
00924       <<"Or give the txt file containing the files to be input"<<endl
00925       <<"Note: If more than one file is found they will be"
00926       <<" concatenated in a TChain and treated as one"<<endl
00927       <<"*************************************************************"
00928       <<endl<<endl<<"Program will exit here"<<endl;
00929     exit(0);
00930   }
00931 
00932   if (nfNuMCEvent) {
00933     MSG("NuInputEvents",Msg::kInfo) 
00934       <<"NuMCEvent information:"<<endl;
00935     //fChainNuMCEvent->Show(0);
00936     MSG("NuInputEvents",Msg::kInfo)
00937       <<endl<<"Added "<<nfNuMCEvent<<" file(s) to NuMCEvent TChain"
00938       <<endl;
00939   }
00940   else {
00941     MSG("NuInputEvents",Msg::kInfo)
00942       <<endl<<"Zero files added to NuMCEvent TChain"<<endl;
00943   }
00944     
00945   MSG("NuInputEvents",Msg::kInfo) 
00946     <<"NuEvent information:"<<endl;
00947   //fChainNuEvent->Show(0);
00948   
00949   MSG("NuInputEvents",Msg::kInfo)
00950     <<endl<<"Analysing "<<nfNuEvent<<" file(s). Reading from disk..."
00951     <<endl;
00952 }
00953 
00954 //......................................................................
00955 
00956 void NuInputEvents::InitialiseNuEventBranch()
00957 {
00958   MSG("NuInputEvents",Msg::kDebug)
00959     <<"Running the SetupNuEventTree method..."<<endl;
00960 
00961   //set up tree
00962   if (fChainNuEvent) {
00963     fChainNuEvent->SetBranchAddress("s",&fNuEvent);
00964     
00965     //set the number of entries in the tree
00966     fEntriesNuEvent=static_cast<Int_t>(fChainNuEvent->GetEntries());
00967     MSG("NuInputEvents",Msg::kInfo)
00968       <<"NuEvent tree has "<<fEntriesNuEvent<<" entries"<<endl;
00969     if (fEntriesNuEvent>0){
00970       //get the snarl/entry
00971       fChainNuEvent->GetEntry(0); 
00972       MSG("NuInputEvents",Msg::kInfo)
00973         <<"NuInputEvents::First run number="<<fNuEvent->run<<endl;
00974       fFirstRunNumberNuEvent=fNuEvent->run;
00975     }
00976   }
00977   else cout<<"No fChainNuEvent to setup, initialise first"<<endl;
00978   
00979   MSG("NuInputEvents",Msg::kDebug)
00980     <<"Finished the SetupNuEventTree method"<<endl;
00981 }
00982 
00983 //......................................................................
00984 
00985 void NuInputEvents::InitialiseNuMCEventBranch()
00986 {
00987   MSG("NuInputEvents",Msg::kDebug)
00988     <<"Running the SetupNuMCEventTree method..."<<endl;
00989 
00990   //set up tree
00991   if (fChainNuMCEvent) {
00992     fChainNuMCEvent->SetBranchAddress("mc",&fNuMCEvent);
00993   
00994     //set the number of entries in the tree
00995     fEntriesNuMCEvent=static_cast<Int_t>(fChainNuMCEvent->GetEntries());
00996     MSG("NuInputEvents",Msg::kInfo)
00997       <<"NuMCEvent tree has "<<fEntriesNuMCEvent<<" entries"<<endl;
00998     if (fEntriesNuMCEvent>0){
00999       //get the snarl/entry
01000       fChainNuMCEvent->GetEntry(0); 
01001       MSG("NuInputEvents",Msg::kInfo)
01002         <<"NuInputEvents::First run number="<<fNuMCEvent->run<<endl;
01003       fFirstRunNumberNuMCEvent=fNuMCEvent->run;
01004     }
01005   }
01006   else cout<<"No fChainNuMCEvent to setup, initialise first"<<endl;
01007 
01008   MSG("NuInputEvents",Msg::kDebug)
01009     <<"Finished the SetupNuMCEventTree method"<<endl;
01010 }
01011 
01012 //......................................................................
01013 
01014 Bool_t NuInputEvents::ObjectExistsInFile(TFile* file,
01015                                          std::string objectName) const
01016 {
01017   if (!file) {
01018     MSG("NuInputEvents",Msg::kWarning)
01019       <<"File does not exist so can't test if object exists!"<<endl;
01020     return false;
01021   }
01022 
01023   TList* listOfKeys=file->GetListOfKeys();
01024 
01025   static TFile* lastFile=0;
01026   if (lastFile!=file){
01027     //MSG("NuInputEvents",Msg::kInfo)
01028     //<<"File contains these keys:"<<endl;
01029     //listOfKeys->Print();//too verbose when lots of histos
01030   }
01031   lastFile=file;
01032 
01033   TObject* ob=listOfKeys->FindObject(objectName.c_str());
01034   if (ob) {
01035     MSG("NuInputEvents",Msg::kInfo)
01036       <<"Requested object with name="<<objectName
01037       <<" exists in file:"<<endl;
01038     ob->Print();
01039     return true;
01040   }
01041   else {
01042     MSG("NuInputEvents",Msg::kInfo)
01043       <<"Requested object with name="<<objectName
01044       <<" does NOT exist in file"<<endl;
01045     return false;
01046   }
01047 }
01048 
01049 //......................................................................

Generated on Mon Feb 15 11:07:14 2010 for loon by  doxygen 1.3.9.1