00001
00002
00003
00004
00005
00007
00008 #include <cstdlib>
00009 #include <fstream>
00010
00011 #include "TFile.h"
00012 #include "TH1.h"
00013 #include "TTree.h"
00014
00015 #include "Conventions/Detector.h"
00016 #include "Conventions/ReleaseType.h"
00017 #include "Conventions/SimFlag.h"
00018 #include "MessageService/MsgService.h"
00019
00020 #include "NtupleUtils/NuEvent.h"
00021 #include "NtupleUtils/NuMCEvent.h"
00022
00023 #include "NuMuBar/NuConfig.h"
00024 #include "NuMuBar/NuOutputWriter.h"
00025
00026 using std::endl;
00027 using std::cout;
00028
00029 CVSID("$Id: NuOutputWriter.cxx,v 1.7 2009/11/26 05:43:38 evansj Exp $");
00030
00031
00032
00033 NuOutputWriter::NuOutputWriter()
00034 {
00035 MSG("NuOutputWriter",Msg::kDebug)
00036 <<"Running NuOutputWriter Constructor..."<<endl;
00037
00038
00039 fFile=0;
00040 fNuEvent=0;
00041 fNuMCEvent=0;
00042 fNuEventTree=0;
00043 fNuMCEventTree=0;
00044 fDirectory=0;
00045
00046 MSG("NuOutputWriter",Msg::kDebug)
00047 <<"Finished NuOutputWriter Constructor"<<endl;
00048 }
00049
00050
00051
00052 NuOutputWriter::~NuOutputWriter()
00053 {
00054 MSG("NuOutputWriter",Msg::kDebug)
00055 <<"Running NuOutputWriter Destructor..."<<endl;
00056
00057
00058 MSG("NuOutputWriter",Msg::kDebug)
00059 <<"Finished NuOutputWriter Destructor"<<endl;
00060 }
00061
00062
00063
00064 void NuOutputWriter::SetupFile(Int_t run,Int_t subrun)
00065 {
00066
00067 if (fDirectory!=0) cout<<"Ahhhh, fDirectory already exists"<<endl;
00068
00069
00070 fFile=this->OpenFile(run,subrun,"NuEvent");
00071
00072
00073 fDirectory=gDirectory;
00074 }
00075
00076
00077
00078 void NuOutputWriter::SetupFile(const NuConfig& config,
00079 std::string prefix)
00080 {
00081
00082 if (fDirectory!=0) cout<<"Ahhhh, fDirectory already exists"<<endl;
00083
00084
00085 fFile=this->OpenFile(config,prefix);
00086
00087
00088 fDirectory=gDirectory;
00089 }
00090
00091
00092
00093 void NuOutputWriter::SetupNuEventTree()
00094 {
00095 MSG("NuOutputWriter",Msg::kInfo)
00096 <<"gDirectory is:"<<endl;
00097 gDirectory->pwd();
00098 MSG("NuOutputWriter",Msg::kInfo)
00099 <<"Local stored fDirectory is:"<<endl;
00100 this->fDirectory->pwd();
00101 MSG("NuOutputWriter",Msg::kInfo)
00102 <<"Setting gDirectory=fDirectory"<<endl;
00103 gDirectory=fDirectory;
00104 fNuEventTree=new TTree("s","s");
00105 cout<<"Creating new NuEvent for output to tree"<<endl;
00106 fNuEvent=new NuEvent();
00107 fNuEventTree->Branch("s","NuEvent",&fNuEvent,32000,2);
00108 }
00109
00110
00111
00112 void NuOutputWriter::SetupNuMCEventTree()
00113 {
00114 MSG("NuOutputWriter",Msg::kInfo)
00115 <<"gDirectory is:"<<endl;
00116 gDirectory->pwd();
00117 MSG("NuOutputWriter",Msg::kInfo)
00118 <<"Local stored fDirectory is:"<<endl;
00119 this->fDirectory->pwd();
00120 MSG("NuOutputWriter",Msg::kInfo)
00121 <<"Setting gDirectory=fDirectory"<<endl;
00122 gDirectory=fDirectory;
00123 fNuMCEventTree=new TTree("mc","mc");
00124 cout<<"Creating new NuMCEvent for output to tree"<<endl;
00125 fNuMCEvent=new NuMCEvent();
00126 fNuMCEventTree->Branch("mc","NuMCEvent",&fNuMCEvent,32000,2);
00127 }
00128
00129
00130
00131 void NuOutputWriter::Finish()
00132 {
00133 MSG("NuOutputWriter",Msg::kInfo)
00134 <<"Running NuOutputWriter::Finish..."<<endl;
00135
00136
00137 TDirectory* tmpd=gDirectory;
00138 MSG("NuOutputWriter",Msg::kDebug)
00139 <<"gDirectory is:"<<endl;
00140
00141
00142
00143
00144
00145 gDirectory=this->fDirectory;
00146
00147 MSG("NuOutputWriter",Msg::kInfo)
00148 <<"Changing directory to fFile..."<<endl;
00149 fFile->cd();
00150
00151 MSG("NuOutputWriter",Msg::kInfo)<<"Writing tree..."<<endl;
00152 fFile->Write();
00153
00154 MSG("NuOutputWriter",Msg::kInfo)
00155 <<"Closing file "<<fFile->GetName()<<" ..."<<endl;
00156 fFile->Close();
00157
00158
00159 gDirectory=tmpd;
00160
00161 MSG("NuOutputWriter",Msg::kInfo)
00162 <<"Completed NuOutputWriter::Finish"<<endl;
00163 }
00164
00165
00166
00167 NuEvent& NuOutputWriter::GetNuEventToFill()
00168 {
00169 if (!fNuEvent) this->SetupNuEventTree();
00170 fNuEvent->Reset();
00171 return *fNuEvent;
00172 }
00173
00174
00175
00176 NuMCEvent& NuOutputWriter::GetNuMCEventToFill()
00177 {
00178 if (!fNuMCEvent) this->SetupNuMCEventTree();
00179 fNuMCEvent->Reset();
00180 return *fNuMCEvent;
00181 }
00182
00183
00184
00185 TFile* NuOutputWriter::OpenFile(const NuConfig& config,
00186 std::string prefix) const
00187 {
00188
00189
00190 TH1::AddDirectory(true);
00191
00192
00193 TFile* outputFile=0;
00194
00195
00196 char *anaDir=getenv("NUANA_DIR");
00197
00198
00199 string sAnaDir="";
00200
00201 if (anaDir!=NULL) {
00202 sAnaDir=anaDir;
00203 }
00204 else {
00205 MSG("NuOutputWriter",Msg::kInfo)
00206 <<"Environmental variable $NUANA_DIR not set."
00207 <<" Writing file(s) to current directory"<<endl;
00208 sAnaDir=".";
00209 }
00210
00211
00212 string sRunNumber=Form("%d",config.run);
00213 string sZeros="";
00214 if (config.run>=0 && config.run<10) sZeros="00000000";
00215 else if (config.run>=10 && config.run<100) sZeros="000000";
00216 else if (config.run>=100 && config.run<1000) sZeros="00000";
00217 else if (config.run>=1000 && config.run<10000) sZeros="0000";
00218 else if (config.run>=10000 && config.run<100000) sZeros="000";
00219 else if (config.run>=100000 && config.run<1000000) sZeros="00";
00220 else if (config.run>=1000000 && config.run<10000000) sZeros="0";
00221 else if (config.run>=10000000 && config.run<100000000) sZeros="";
00222 sRunNumber=sZeros+sRunNumber;
00223
00224 string sDetector="UnknownDet";
00225 if (config.detector==Detector::kNear) {
00226 sDetector="N";
00227 if (config.simFlag==SimFlag::kMC) sDetector="n";
00228 }
00229 else if (config.detector==Detector::kFar) {
00230 sDetector="F";
00231 if (config.simFlag==SimFlag::kMC) sDetector="f";
00232 }
00233 else if (config.detector==Detector::kCalDet) {
00234 sDetector="C";
00235 if (config.simFlag==SimFlag::kMC) sDetector="c";
00236 }
00237 else cout<<"Ahhh, don't know detector="<<config.detector<<endl;
00238
00239 string sPrefix="";
00240 if (prefix!="") sPrefix+=prefix;
00241 string sBase=sAnaDir+"/"+sPrefix;
00242
00243 if (ReleaseType::IsDaikon(config.releaseType)
00244 && ReleaseType::GetMCSubVersion(config.releaseType)>=7){
00245 sBase += Form("r%d",config.runPeriod);
00246 }
00247 if (SimFlag::kMC == config.simFlag
00248 && Detector::kNear == config.detector
00249 && ReleaseType::IsDogwood(config.releaseType)
00250 && ReleaseType::GetRecoSubVersion(config.releaseType)>=1){
00251 sBase += Form("i%d",config.intensity);
00252 }
00253
00254 sBase += sDetector;
00255 sBase += sRunNumber;
00256 string sFileName=sBase+".root";
00257
00258
00259 ifstream Test(sFileName.c_str());
00260
00261
00262 if(!Test){
00263 outputFile=new TFile(sFileName.c_str(),"RECREATE");
00264 }
00265 else {
00266
00267 Int_t fred=1;
00268 while(Test) {
00269 Test.close();
00270 string sAppendage=Form("%d",fred);
00271 sFileName=sBase+"_"+sAppendage+".root";
00272 Test.open(sFileName.c_str());
00273 fred++;
00274 }
00275 outputFile=new TFile(sFileName.c_str(),"NEW");
00276 outputFile->SetCompressionLevel(9);
00277 }
00278
00279 string sTmp="No File!";
00280 if (outputFile) sTmp=outputFile->GetName();
00281
00282 MSG("NuOutputWriter",Msg::kInfo)
00283 <<"Output file opened: "<<sTmp<<endl;
00284 return outputFile;
00285 }
00286
00287
00288
00289 TFile* NuOutputWriter::OpenFile(Int_t run,Int_t subrun,
00290 std::string prefix) const
00291 {
00292
00293
00294 TH1::AddDirectory(true);
00295
00296
00297 TFile* outputFile=0;
00298
00299
00300 char *anaDir=getenv("MEUANA_DIR");
00301
00302
00303 string sAnaDir="";
00304
00305 if (anaDir!=NULL) {
00306 sAnaDir=anaDir;
00307 }
00308 else {
00309 MSG("NuOutputWriter",Msg::kInfo)
00310 <<"Environmental variable $MEUANA_DIR not set."
00311 <<" Writing file(s) to current directory"<<endl;
00312 sAnaDir=".";
00313 }
00314
00315
00316 string sRunNumber=Form("%d",run);
00317 string sSubrunNumber=Form("%d",subrun);
00318
00319 string sDetector="";
00320
00321 string sPrefix="";
00322 if (prefix!="") sPrefix+=prefix;
00323 string sBase=sAnaDir+"/"+sPrefix+sDetector+sRunNumber;
00324 sBase+="_"+sSubrunNumber;
00325 string sFileName=sBase+".root";
00326
00327
00328 ifstream Test(sFileName.c_str());
00329
00330
00331 if(!Test){
00332 outputFile=new TFile(sFileName.c_str(),"RECREATE");
00333 }
00334 else {
00335
00336 Int_t fred=1;
00337 while(Test) {
00338 Test.close();
00339 string sAppendage=Form("%d",fred);
00340 sFileName=sBase+"_"+sAppendage+".root";
00341 Test.open(sFileName.c_str());
00342 fred++;
00343 }
00344 outputFile=new TFile(sFileName.c_str(),"NEW");
00345 outputFile->SetCompressionLevel(9);
00346 }
00347
00348 string sTmp="No File!";
00349 if (outputFile) sTmp=outputFile->GetName();
00350
00351 MSG("NuOutputWriter",Msg::kInfo)
00352 <<"Output file opened: "<<sTmp<<endl;
00353 return outputFile;
00354 }
00355
00356
00357
00358 void NuOutputWriter::FillNuEventTree()
00359 {
00360 if (!fNuEvent) {
00361 MSG("NuOutputWriter",Msg::kError)
00362 <<"No fNuEvent object to put in tree"<<endl;
00363 }
00364
00365 MSG("NuOutputWriter",Msg::kDebug)<<"Filling NuEvent Tree..."<<endl;
00366
00367 static Int_t counter=0;
00368 fNuEvent->index=counter;
00369 counter++;
00370
00371
00372 fNuEventTree->Fill();
00373 }
00374
00375
00376
00377 void NuOutputWriter::FillNuMCEventTree()
00378 {
00379 if (!fNuMCEvent) {
00380 MSG("NuOutputWriter",Msg::kError)
00381 <<"No fNuMCEvent object to put in tree"<<endl;
00382 }
00383
00384 MSG("NuOutputWriter",Msg::kDebug)<<"Filling NuMCEvent Tree..."<<endl;
00385
00386 static Int_t counter=0;
00387 fNuMCEvent->index=counter;
00388 counter++;
00389
00390
00391 fNuMCEventTree->Fill();
00392 }
00393
00394