00001
00002
00003
00004
00005
00006
00007
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;
00035 for (int i = 0; i < 3; ++i) tsize *= 1000;
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
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
00073
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
00105 this->ClearLoadFlags();
00106
00107
00108 fIndex = 0;
00109
00110 return 1;
00111 }
00112
00113
00114
00115 int EventHandle::SetupOutputFile(TFile* f)
00116 {
00117
00118
00119
00120 fOutputFile = f;
00121
00122
00123 if (fOutEventTree) {
00124 delete fOutEventTree;
00125 fOutEventTree=0;
00126 }
00127
00128
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
00161
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
00172 this->ClearLoadFlags();
00173
00174 return fIndex-indexSave;
00175 }
00176
00177
00178
00179 int EventHandle::Rewind(int n)
00180 {
00181
00182
00183
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
00193 this->ClearLoadFlags();
00194
00195 return indexSave-fIndex;
00196 }
00197
00198
00203 int EventHandle::Load(int branchID) const
00204 {
00205
00206 if (!fInputFile) return 0;
00207 assert ((branchID >= 0) && (branchID <= kNBranch));
00208
00209
00210
00211 if (branchID == kNBranch) {
00212 for (int i=0; i<kNBranch; ++i) this->Load(i);
00213 return 1;
00214 }
00215
00216
00217 if (this->IsLoaded((BranchID_t) branchID)) return 0;
00218
00219
00220 if (fBranch[branchID]) {
00221 fBranch[branchID]->GetEntry(fIndex);
00222 this->SetLoaded(branchID);
00223 return 1;
00224 }
00225 else {
00226
00227
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
00243
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