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

IoModules/EventHandle.cxx

Go to the documentation of this file.
00001 
00002 // $Id: EventHandle.cxx,v 1.1 2007/02/09 04:44:13 fmwk Exp $
00003 //
00004 // Interface between the I/O system and event data. Allows for partial
00005 // I/O of various major components of an event.
00006 //
00007 // messier@indiana.edu
00009 #include "IoModules/EventHandle.h"
00010 #include "EventDataModel/Event.h"
00011 #include "EventDataModel/EventHeader.h"
00012 #include <iostream>
00013 #include <list>
00014 #include <string>
00015 #include <cassert>
00016 #include "TFile.h"
00017 #include "TTree.h"
00018 #include "TBranch.h"
00019 using namespace io;
00020 
00021 static const char* gsEventTree = "EventTree";
00022 
00023 //......................................................................
00027 EventHandle::EventHandle() :
00028   fIndex         ( 0 ),
00029   fInputFile     ( 0 ),
00030   fOutputFile    ( 0 ),
00031   fEventTree     ( 0 ),
00032   fOutEventTree  ( 0 )
00033 { 
00034   Long64_t tsize = 150; // Set the limit of event tree to be large (GB)
00035   for (int i = 0; i < 3; ++i) tsize *= 1000; // Convert to bytes from GB
00036   TTree::SetMaxTreeSize(tsize);
00037 
00038   this->ClearLoadFlags();
00039 }
00040 
00041 //......................................................................
00042 
00043 EventHandle::~EventHandle() { }
00044 
00045 //......................................................................
00049 int EventHandle::Index() const { return fIndex; }
00050 
00051 //......................................................................
00058 int EventHandle::SetupInputFile(TFile* f) 
00059 {
00060   fInputFile = f;
00061 
00062   // In with the new...
00063   fEventTree = dynamic_cast<TTree*>(f->Get(gsEventTree));
00064   if (fEventTree==0) {
00065     std::cout << 
00066       "Failed to find " << gsEventTree << " in file '"
00067               << f->GetName() << "'" << std::endl;
00068     f->ls();
00069     return 0;
00070   }
00071 
00072   // Don't check for 0 on these until we try to use them. Its possible
00073   // and legal for the branches to have been dropped from a file.
00074   const char* branch[] = { 
00075     "fHeader", "fMC",     "fDetSim", "fDAQ",  "fRaw",
00076     "fRawAux", "fCal",    "fReco",   "fUser", "fSummary"
00077   };
00078   void* baddr[kNBranch];
00079   baddr[kHeaderID]  = &fEvent->fHeader;
00080   baddr[kMCID]      = &fEvent->fMC;
00081   baddr[kDetSimID]  = &fEvent->fDetSim;
00082   baddr[kDAQID]     = &fEvent->fDAQ;
00083   baddr[kRawID]     = &fEvent->fRaw;
00084   baddr[kRawAuxID]  = &fEvent->fRawAux;
00085   baddr[kCalID]     = &fEvent->fCal;
00086   baddr[kRecoID]    = &fEvent->fReco;
00087   baddr[kUserID]    = &fEvent->fUser;
00088   baddr[kSummaryID] = &fEvent->fSummary;
00089   for (int ibranch=0; ibranch<kNBranch; ++ibranch) {
00090     fBranch[ibranch] = fEventTree->GetBranch(branch[ibranch]);
00091     if (fBranch[ibranch]!=0) {
00092       fBranch[ibranch]->SetAddress(baddr[ibranch]);
00093       fBranch[ibranch]->SetAutoDelete(kTRUE);
00094     }
00095     else {
00096       std::cout << __FILE__ << ":" << __LINE__ 
00097                 << "\tFailed to make branch " << branch[ibranch] 
00098                 << std::endl;
00099       std::cout << "\tFile not formed correctly?" << std::endl;
00100       abort();
00101     }
00102   }
00103 
00104   // Flag all branches as unloaded
00105   this->ClearLoadFlags();
00106 
00107   // Reset to the start of the file
00108   fIndex = 0;
00109 
00110   return 1;
00111 }
00112 
00113 //......................................................................
00114 
00115 int EventHandle::SetupOutputFile(TFile* f) 
00116 {
00117 //======================================================================
00118 // Set up an output file for writing events
00119 //======================================================================
00120   fOutputFile = f;
00121   
00122   // Out with the old
00123   if (fOutEventTree) { 
00124     delete fOutEventTree; 
00125     fOutEventTree=0; 
00126   }
00127 
00128   // In with the new...  
00129   TDirectory* startDir = gDirectory;
00130 
00131   fOutputFile->cd();
00132   fOutEventTree = new TTree(gsEventTree, gsEventTree);
00133   assert(fOutEventTree);
00134 
00135   int bSz  = fBucketSize;
00136   int sLvl = fSplitLevel;
00137   fOutEventTree->Branch("fHeader", "edm::EventHeader",
00138                         &fEvent->fHeader, bSz, sLvl);
00139   fOutEventTree->Branch("fTop",    "TFolder", &fEvent->fTop,    bSz, sLvl);
00140   fOutEventTree->Branch("fMC",     "TFolder", &fEvent->fMC,     bSz, sLvl);
00141   fOutEventTree->Branch("fDetSim", "TFolder", &fEvent->fDetSim, bSz, sLvl);
00142   fOutEventTree->Branch("fDAQ",    "TFolder", &fEvent->fDAQ,    bSz, sLvl);
00143   fOutEventTree->Branch("fRaw",    "TFolder", &fEvent->fRaw,    bSz, sLvl);
00144   fOutEventTree->Branch("fRawAux", "TFolder", &fEvent->fRawAux, bSz, sLvl);
00145   fOutEventTree->Branch("fCal",    "TFolder", &fEvent->fCal,    bSz, sLvl);
00146   fOutEventTree->Branch("fReco",   "TFolder", &fEvent->fReco,   bSz, sLvl);
00147   fOutEventTree->Branch("fUser",   "TFolder", &fEvent->fUser,   bSz, sLvl);
00148   fOutEventTree->Branch("fSummary","TFolder", &fEvent->fSummary,bSz, sLvl);
00149   
00150   startDir->cd();
00151   
00152   return 1;
00153 }
00154 
00155 //......................................................................
00156 
00157 int EventHandle::Advance(int n) 
00158 {
00159 //======================================================================
00160 // Advance n places in the input stream. Return the difference between
00161 // the new position and the old.
00162 //======================================================================
00163   if (fEventTree==0 || n<1) return 0;
00164 
00165   int indexSave = fIndex;
00166   int nEntries  = (int)fEventTree->GetEntries();
00167 
00168   fIndex += n;
00169   if (fIndex>=nEntries) fIndex = nEntries-1;
00170 
00171   // Since we've moved on, mark all branches as unloaded
00172   this->ClearLoadFlags();
00173 
00174   return fIndex-indexSave;
00175 }
00176 
00177 //......................................................................
00178 
00179 int EventHandle::Rewind(int n) 
00180 {
00181 //======================================================================
00182 // Rewind n places in the input stream. Return the difference between
00183 // the new position and the old.
00184 //======================================================================
00185   if (fEventTree==0 || n<1) return 0;
00186 
00187   int indexSave = fIndex;
00188 
00189   fIndex -= n;
00190   if (fIndex<0) fIndex = 0;
00191 
00192   // Since we've moved on, mark all branches as unloaded
00193   this->ClearLoadFlags();
00194   
00195   return indexSave-fIndex;
00196 }
00197 
00198 //......................................................................
00203 int EventHandle::Load(int branchID) const
00204 {
00205   // Do not load anything if a ROOT input file is not specified
00206   if (!fInputFile) return 0;
00207   assert ((branchID >= 0) && (branchID <= kNBranch));
00208 
00209   // If branchID is maxed out, load all the branches. Load one-by-one
00210   // to avoid over-writing branched already loaded.
00211   if (branchID == kNBranch) {
00212     for (int i=0; i<kNBranch; ++i) this->Load(i);
00213     return 1;
00214   }
00215   
00216   // Check if branch is already loaded
00217   if (this->IsLoaded((BranchID_t) branchID)) return 0;
00218   
00219   // Make sure that this branch exists before actually loading it
00220   if (fBranch[branchID]) {
00221     fBranch[branchID]->GetEntry(fIndex);
00222     this->SetLoaded(branchID);
00223     return 1;
00224   }
00225   else {
00226     // Do this in case event was written with split level 0, when
00227     // event is written to one branch
00228     std::cout << "IoEventHandle::Load() Warning: Branch " << branchID
00229               << " is missing. Loading entire event" << std::endl;
00230     fEventTree->GetEntry(fIndex);
00231     this->SetLoaded(kNBranch);
00232   }
00233 
00234   return 0;
00235 }
00236 
00237 //......................................................................
00238 
00239 int EventHandle::Write()
00240 {
00241   if (fOutputFile && fOutEventTree) {
00242     // Make sure we load any branches which haven't been loaded yet so
00243     // they get written
00244     this->Load(kNBranch);
00245     fOutEventTree->Fill();
00246   }
00247   return 1;
00248 }
00249 
00250 //......................................................................
00251 
00252 void EventHandle::Close()
00253 {
00254   if (fOutputFile && fOutEventTree) {
00255     fOutEventTree->Write();
00256     delete fOutEventTree;
00257     fOutEventTree = 0;
00258   }
00259 }
00260 
00261 //......................................................................
00262 
00263 void EventHandle::Report()
00264 {
00265   if (fEventTree) {
00266     std::cout << "IoEventHandle EventTree: " << std::endl;
00267     fEventTree->Print();
00268   }
00269   if (fOutEventTree) {
00270     std::cout << "IoEventHandle OutEventTreee: " << std::endl;
00271     fOutEventTree->Print();
00272   }
00273 }
00274 

Generated on Mon Dec 1 02:35:17 2008 for NOvA Offline by  doxygen 1.3.9.1