00001
00002
00003
00004
00005
00006
00007
00009
00010 #include "AlephROOTDbManager.h"
00011 #include "AlphaBanks.h"
00012 #include "AlephCardsReader.h"
00013 #include "AlephIoManager.h"
00014 #include "AlephExManager.h"
00015 #include "TChain.h"
00016 #include "TTree.h"
00017 #include "TFile.h"
00018 #include <stdlib.h>
00019 #include <iostream>
00020 #include <fstream>
00021 #include <vector>
00022 #include <list>
00023 #include <algorithm>
00024 #include <string>
00025
00026 extern ofstream fout;
00027
00028 AlephROOTDbManager::AlephROOTDbManager()
00029 {
00030 ROOTDbFile = NULL;
00031 ROOTDbTree = NULL;
00032 entry = -1;
00033 classbitpattern = 0;
00034 }
00035
00036 AlephROOTDbManager::~AlephROOTDbManager()
00037 {
00038
00039 if(ROOTDbTree) delete ROOTDbTree;
00040 if(ROOTDbFile) delete ROOTDbFile;
00041 }
00042
00043 AlephRC AlephROOTDbManager::initialize()
00044 {
00045 fout << " AlephROOTDbManager : initializing .... " << endl;
00046 AlephManager* ioManager = (AlephSession::TheAlephSession())->aAlephManager("IoManager");
00047 if (ioManager != NULL)
00048 {
00049 AlephStatus ioManStatus = ioManager->status();
00050 if (ioManStatus == INITIALIZED)
00051 {
00052
00053 AlphaBanks::Class()->IgnoreTObjectStreamer();
00054 QLIN::Class()->IgnoreTObjectStreamer();
00055 QMTL::Class()->IgnoreTObjectStreamer();
00056 QMTS::Class()->IgnoreTObjectStreamer();
00057 FRFT::Class()->IgnoreTObjectStreamer();
00058 FRTL::Class()->IgnoreTObjectStreamer();
00059 TEXS::Class()->IgnoreTObjectStreamer();
00060 HMAD::Class()->IgnoreTObjectStreamer();
00061 MCAD::Class()->IgnoreTObjectStreamer();
00062 PECO::Class()->IgnoreTObjectStreamer();
00063 EIDT::Class()->IgnoreTObjectStreamer();
00064 PHCO::Class()->IgnoreTObjectStreamer();
00065 PEPT::Class()->IgnoreTObjectStreamer();
00066 FRID::Class()->IgnoreTObjectStreamer();
00067 EFOL::Class()->IgnoreTObjectStreamer();
00068 PCQA::Class()->IgnoreTObjectStreamer();
00069 EGPC::Class()->IgnoreTObjectStreamer();
00070 MUID::Class()->IgnoreTObjectStreamer();
00071 PGPC::Class()->IgnoreTObjectStreamer();
00072 PGAC::Class()->IgnoreTObjectStreamer();
00073 PDLT::Class()->IgnoreTObjectStreamer();
00074 PMLT::Class()->IgnoreTObjectStreamer();
00075 QDET::Class()->IgnoreTObjectStreamer();
00076 QVEC::Class()->IgnoreTObjectStreamer();
00077 YV0V::Class()->IgnoreTObjectStreamer();
00078 FKIN::Class()->IgnoreTObjectStreamer();
00079 FVER::Class()->IgnoreTObjectStreamer();
00080 QVRT::Class()->IgnoreTObjectStreamer();
00081 ALPB::Class()->IgnoreTObjectStreamer();
00082 ALRP::Class()->IgnoreTObjectStreamer();
00083 RNL2::Class()->IgnoreTObjectStreamer();
00084 RNF2::Class()->IgnoreTObjectStreamer();
00085 RNR2::Class()->IgnoreTObjectStreamer();
00086 RLEP::Class()->IgnoreTObjectStreamer();
00087 LFIL::Class()->IgnoreTObjectStreamer();
00088 EVEH::Class()->IgnoreTObjectStreamer();
00089 KEVH::Class()->IgnoreTObjectStreamer();
00090 REVH::Class()->IgnoreTObjectStreamer();
00091 LOLE::Class()->IgnoreTObjectStreamer();
00092 X1RG::Class()->IgnoreTObjectStreamer();
00093 BOMB::Class()->IgnoreTObjectStreamer();
00094 RUNH::Class()->IgnoreTObjectStreamer();
00095 ASIM::Class()->IgnoreTObjectStreamer();
00096 ADBR::Class()->IgnoreTObjectStreamer();
00097 XTBN::Class()->IgnoreTObjectStreamer();
00098 XTCN::Class()->IgnoreTObjectStreamer();
00099 XTOP::Class()->IgnoreTObjectStreamer();
00100 LUPA::Class()->IgnoreTObjectStreamer();
00101 SILH::Class()->IgnoreTObjectStreamer();
00102 XHVB::Class()->IgnoreTObjectStreamer();
00103 XTEB::Class()->IgnoreTObjectStreamer();
00104 XTRB::Class()->IgnoreTObjectStreamer();
00105 DTBP::Class()->IgnoreTObjectStreamer();
00106 VHBV::Class()->IgnoreTObjectStreamer();
00107 VHPV::Class()->IgnoreTObjectStreamer();
00108 EAUX::Class()->IgnoreTObjectStreamer();
00109 SIX2::Class()->IgnoreTObjectStreamer();
00110 X1TV::Class()->IgnoreTObjectStreamer();
00111 KWGT::Class()->IgnoreTObjectStreamer();
00112 X1IP::Class()->IgnoreTObjectStreamer();
00113 SIXA::Class()->IgnoreTObjectStreamer();
00114 SITC::Class()->IgnoreTObjectStreamer();
00115 SRCO::Class()->IgnoreTObjectStreamer();
00116 X1TH::Class()->IgnoreTObjectStreamer();
00117 X1AD::Class()->IgnoreTObjectStreamer();
00118 QEXT::Class()->IgnoreTObjectStreamer();
00119 QHAC::Class()->IgnoreTObjectStreamer();
00120 AlephIoManager* IoManager = (AlephIoManager*)ioManager;
00121 AlephCardsReader& cardsReader = IoManager->theCardsReader();
00122
00123 vector<int> edirclass = cardsReader.getClass();
00124 vector<long> asrucard = cardsReader.getAlphaSrun();
00125
00126 classbitpattern = 0;
00127 for(vector<int>::iterator theclass = edirclass.begin();theclass<edirclass.end();theclass++)
00128 classbitpattern |= (1<<((*theclass)-1));
00129
00130 asrucardlist.clear();
00131 for(vector<long>::iterator thecard = asrucard.begin();thecard<asrucard.end();thecard++)
00132 asrucardlist.push_back(*thecard);
00133 switch(cardsReader.getTaType()=="write")
00134 {
00135 case true:
00136 {
00137 openNewDb(cardsReader.getAlphaFilo());
00138 break;
00139 }
00140 case false:
00141 {
00142 openExistingDb(cardsReader.getAlphaFili());
00143
00144 vector<long> nevtcard = cardsReader.getNevt();
00145
00146 if (ROOTDbTree == 0)
00147 {
00148 fout << "AlephROOTDbManager::Initialize ERROR: ROOTDbTree not initialized!" << endl;
00149 return AlephERROR;
00150 }
00151 int nentries = int(((TChain*)ROOTDbTree)->GetEntries());
00152
00153 if(nevtcard.size()==1)
00154 {
00155 lowentry = 0;
00156 highentry = nevtcard[0]<nentries ? nevtcard[0] : nentries;
00157 }
00158 else if (nevtcard.size()==2)
00159 {
00160 lowentry = nevtcard[0];
00161 highentry = nevtcard[1]<nentries ? nevtcard[1] : nentries;
00162 }
00163 else
00164 {
00165 fout << "AlephROOTDbManager::Initialize WARNING: bad NEVT format. Ignored" << endl;
00166 lowentry = 0;
00167 highentry = nentries;
00168 }
00169 entry = lowentry-1;
00170 break;
00171 }
00172 }
00173 }
00174 else
00175 {
00176 fout << "AlephDbManager Warning : AlephIoManager not initialized... can't start" << endl;
00177 exit(1);
00178 }
00179 }
00180 else
00181 {
00182 fout << "AlephDbManager Warning : AlephIoManager not persent... can't start" << endl;
00183 exit(1);
00184 }
00185 setStatus(INITIALIZED);
00186 return AlephOK;
00187 }
00188
00189 AlephRC AlephROOTDbManager::openExistingDb(const vector<string>& aDbName,const string& mode = "TRANSTY")
00190 {
00191 ROOTDbTree = new TChain("AlephDbTree","AlephEvents");
00192 for(vector<string>::const_iterator filename=aDbName.begin();filename<aDbName.end();filename++)
00193 {
00194 ((TChain*)ROOTDbTree)->Add(filename->c_str());
00195 }
00196 setDbName("TChain");
00197 return AlephOK;
00198 }
00199
00200 AlephRC AlephROOTDbManager::openNewDb(const string& aDbName)
00201 {
00202 ROOTDbFile = new TFile(aDbName.c_str(),"RECREATE");
00203
00204 ROOTDbTree = new TTree("AlephDbTree","AlephEvents");
00205 ROOTDbTree->Branch("AlphaBanks","AlphaBanks", &mylocalevent,1600,0);
00206 ROOTDbTree->Branch("Event_number",&eventnumber,"Event_number/I");
00207 ROOTDbTree->Branch("Run_number",&runnumber,"Run_number/I");
00208 ROOTDbTree->Branch("EDIR_class",&edirclass,"EDIR_class/I");
00209 setDbName(aDbName);
00210 return AlephOK;
00211 }
00212
00213 AlephRC AlephROOTDbManager::StoreEvent(AlphaBanks* event)
00214 {
00215 mylocalevent = event;
00216 eventnumber = event->Event().number();
00217 runnumber = event->Run().number();
00218 edirclass = event->Event().EdirClass();
00219 ROOTDbFile->cd();
00220 ROOTDbTree->Fill();
00221 return AlephOK;
00222 }
00223
00224 AlephRC AlephROOTDbManager::GetEvent(AlphaBanks* theoutput)
00225 {
00226 AlephExManager* exManager = (AlephExManager*)(AlephSession::TheAlephSession())->aAlephManager("ExManager");
00227
00228 for(entry++;entry<highentry;entry++)
00229 {
00230 if(entry>=highentry) return AlephERROR;
00231
00232 Int_t ientry = ((TChain*)ROOTDbTree)->LoadTree(entry);
00233
00234
00235 TBranch* bRun_number = ((TChain*)ROOTDbTree)->GetBranch("Run_number");
00236 TBranch* bEDIR_class = ((TChain*)ROOTDbTree)->GetBranch("EDIR_class");
00237 TBranch* bEvent_number = ((TChain*)ROOTDbTree)->GetBranch("Event_number");
00238 TBranch* bAlphaBanks = ((TChain*)ROOTDbTree)->GetBranch("AlphaBanks");
00239 bRun_number->SetAddress(&runnumber);
00240 bEDIR_class->SetAddress(&edirclass);
00241 bEvent_number->SetAddress(&eventnumber);
00242 bAlphaBanks->SetAddress(&theoutput);
00243 bEDIR_class->GetEntry(ientry);
00244 bRun_number->GetEntry(ientry);
00245
00246 if((classbitpattern)&&(!(edirclass&classbitpattern))) continue;
00247
00248 if((asrucardlist.size())&&(find(asrucardlist.begin(),asrucardlist.end(),runnumber)==asrucardlist.end())) continue;
00249
00250 ((TChain*)ROOTDbTree)->GetEntry(ientry);
00251 exManager->setCurrentRunEvt(runnumber,eventnumber);
00252 return AlephOK;
00253 }
00254 return AlephERROR;
00255 }
00256
00257 AlephRC AlephROOTDbManager::terminate()
00258 {
00259 if(ROOTDbFile)
00260 {
00261 ROOTDbFile->cd();
00262 ROOTDbFile->Write();
00263 ROOTDbFile->Close();
00264 setDbName("");
00265 ROOTDbTree = NULL;
00266 }
00267 setStatus(TERMINATED);
00268 return AlephOK;
00269 }
00270