00001
00002
00003
00004
00005
00007
00008 #include "TChain.h"
00009 #include "TClonesArray.h"
00010 #include "TFile.h"
00011 #include "TH1F.h"
00012 #include "TObjString.h"
00013 #include "TMath.h"
00014 #include "TRegexp.h"
00015 #include "TROOT.h"
00016 #include "TStyle.h"
00017 #include "TSystem.h"
00018
00019 #include "MessageService/MsgService.h"
00020 #include "BeamDataNtuple/NtpBDLiteRecord.h"
00021 #include "CandNtupleSR/NtpSREvent.h"
00022 #include "CandNtupleSR/NtpSRTrack.h"
00023 #include "CandNtupleSR/NtpSRShower.h"
00024 #include "CandNtupleSR/NtpSRSlice.h"
00025 #include "CandNtupleSR/NtpSRStrip.h"
00026 #include "StandardNtuple/NtpStRecord.h"
00027
00028 #include "NuMuBar/NuBase.h"
00029 #include "NuMuBar/NuConfig.h"
00030
00031 using std::endl;
00032 using std::cout;
00033 using std::map;
00034 using std::vector;
00035
00036 string NuBase::fInputFileName="";
00037 Bool_t NuBase::fLoadTrees=true;
00038
00039
00040
00041 CVSID("$Id: NuBase.cxx,v 1.5 2007/12/14 23:36:49 ahimmel Exp $");
00042
00043
00044
00045 NuBase::NuBase()
00046 {
00047 MSG("NuBase",Msg::kInfo)
00048 <<"Running NuBase Constructor..."<<endl;
00049
00050 fChain=0;
00051 fChainBD=0;
00052 fOutFile=0;
00053 fRec=0;
00054 fRecBD=0;
00055
00056 fEntries=0;
00057 fEntriesBD=0;
00058 fRun=100;
00059 fSubrun=0;
00060
00061 fNumFiles=0;
00062
00063 fFirstFileName="";
00064 fS="";
00065
00066
00067 if (fLoadTrees) {
00068 MSG("NuBase",Msg::kInfo)<<"Loading trees"<<endl;
00069 this->MakeChain();
00070 this->SetRootFileObjects();
00071
00072
00073
00074 if (fRec && fChain){
00075
00076 fChain->GetEvent(0);
00077 NtpStRecord& ntp=(*fRec);
00078 const RecCandHeader& rec=ntp.GetHeader();
00079 MSG("NuBase",Msg::kInfo)
00080 <<"Found: run="<<rec.GetRun()
00081 <<", subrun="<<rec.GetSubRun()<<endl;
00082 fRun=rec.GetRun();
00083 fSubrun=rec.GetSubRun();
00084 }
00085 else cout<<"Ahhh, no root files"<<endl;
00086 }
00087 else MSG("NuBase",Msg::kInfo)<<"Not loading trees"<<endl;
00088
00089
00090
00091 gStyle->SetOptStat(1111111);
00092 gStyle->SetOptFit(1111);
00093 gStyle->SetPalette(1);
00094
00095 MSG("NuBase",Msg::kInfo)
00096 <<"Finished NuBase Constructor"<<endl;
00097 }
00098
00099
00100
00101 NuBase::~NuBase()
00102 {
00103 MSG("NuBase",Msg::kInfo)
00104 <<"Running NuBase Destructor..."<<endl;
00105
00106 if (fOutFile){
00107
00108
00109 fOutFile->Close();
00110 }
00111
00112 MSG("NuBase",Msg::kDebug)
00113 <<"Finished NuBase Destructor"<<endl;
00114 }
00115
00116
00117
00118 void NuBase::InputFileName(string f)
00119 {
00120 if (f!=""){
00121 MAXMSG("NuBase",Msg::kInfo,1)
00122 <<"Running with input file name="<<f<<endl;
00123 fInputFileName=f;
00124 }
00125 }
00126
00127
00128
00129 void NuBase::LoadTrees(Bool_t loadTrees)
00130 {
00131 fLoadTrees=loadTrees;
00132 }
00133
00134
00135
00136 void NuBase::WriteOutHistos()
00137 {
00138
00139 if (fOutFile){
00140 if (fOutFile->IsWritable()) {
00141 fOutFile->cd();
00142
00143
00144 Int_t nf=this->GetNumFilesAddedToChain();
00145 TH1F* hNumFiles=new TH1F("hNumFiles","hNumFiles",15,-5,10);
00146 hNumFiles->Fill(1,nf);
00147
00148
00149 TH1F* hRun=dynamic_cast<TH1F*>(gROOT->FindObject("hRun"));
00150 Float_t nr=-1;
00151 if (hRun) nr=hRun->GetEntries();
00152 MSG("NuAnalysis",Msg::kInfo)
00153 <<"Files added to chain="<<hNumFiles->GetMaximum()
00154 <<", runs found="<<nr<<endl;
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171 MSG("NuBase",Msg::kInfo)
00172 <<"Writing histos to: "<<fOutFile->GetName()<<" ..."<<endl;
00173 fOutFile->Write();
00174
00175
00176 }
00177 else {
00178 MSG("NuBase",Msg::kWarning)<<"File not writable!"<<endl;
00179 }
00180 }
00181 }
00182
00183
00184
00185 void NuBase::OpenOutFile(TString outName, TString opt)
00186 {
00187 TH1::AddDirectory(true);
00188 if (fOutFile) {
00189 fOutFile->Close();
00190 delete fOutFile;
00191 }
00192 fOutFile = new TFile(outName, opt);
00193 if (!fOutFile->IsOpen()) {
00194 MSG("NuBase",Msg::kError)<< "File " << outName << " did not open!"<<endl;
00195 }
00196 }
00197
00198
00199
00200 void NuBase::CloseOutFile()
00201 {
00202 if (fOutFile) fOutFile->Close();
00203 }
00204
00205
00206
00207 vector<string> NuBase::MakeFileList()
00208 {
00210
00211 vector<string> fileList;
00212
00213 if (fInputFileName!=""){
00214
00215 Int_t findGives=fInputFileName.find(".root");
00216 MSG("NuBase",Msg::kInfo)
00217 <<"Checking input string for \"*.root\", position in string="
00218 <<findGives<<endl;
00219
00220 if (findGives>0){
00221
00222 fileList.push_back(fInputFileName);
00223 }
00224 else{
00225 ifstream inputFile(fInputFileName.c_str());
00226
00227
00228 if (inputFile){
00229
00230 string file="";
00231
00232
00233 while(inputFile>>file) {
00234 MSG("NuBase",Msg::kDebug)
00235 <<"Found input file name="<<file<<endl;
00236 fileList.push_back(file);
00237 }
00238 MSG("NuBase",Msg::kDebug)
00239 <<"Files names found in txt file="<<fileList.size()<<endl;
00240 }
00241 else{
00242 MSG("NuBase",Msg::kFatal)
00243 <<endl<<endl
00244 <<"**********************************************************"
00245 <<endl<<"Input txt file of file names does not exist!"<<endl
00246 <<"InputFileName="<<fInputFileName<<endl
00247 <<"**********************************************************"
00248 <<endl<<endl<<"Program will exit here"<<endl;
00249 exit(0);
00250 }
00251 }
00252 }
00253
00254 if (fileList.size()>0) return fileList;
00255
00256
00257 char* envVariable=getenv("NUDATA");
00258 if (envVariable==NULL){
00259 MSG("NuBase",Msg::kFatal)
00260 <<endl<<endl
00261 <<"*************************************************************"
00262 <<endl<<"Environmental variable NUDATA not set!"<<endl
00263 <<"Please set NUDATA to the directory containing the"
00264 <<" ntuple root files"<<endl
00265 <<"Note: If more than one file is found they will be"
00266 <<" concatenated and treated as one"<<endl
00267 <<"*************************************************************"
00268 <<endl<<endl<<"Program will exit here"<<endl;
00269 exit(0);
00270 }
00271
00272 string sEnv="";
00273 sEnv=envVariable;
00274 sEnv+="/*.root";
00275 MSG("NuBase",Msg::kInfo)
00276 <<"Looking for *.root files using the env variable"<<endl
00277 <<"NUDATA="<<sEnv<<endl;
00278 fileList.push_back(sEnv);
00279
00280 return fileList;
00281 }
00282
00283
00284
00285
00286 Bool_t NuBase::ObjectExistsInFile(TFile* file,
00287 std::string objectName) const
00288 {
00289 if (!file) {
00290 MSG("NuBase",Msg::kWarning)
00291 <<"File does not exist so can't test if object exists!"<<endl;
00292 return false;
00293 }
00294
00295 TList* listOfKeys=file->GetListOfKeys();
00296
00297 static TFile* lastFile=0;
00298 if (lastFile!=file){
00299 MSG("NuBase",Msg::kInfo)
00300 <<"File contains these keys:"<<endl;
00301 listOfKeys->Print();
00302 }
00303 lastFile=file;
00304
00305 TObject* ob=listOfKeys->FindObject(objectName.c_str());
00306 if (ob) {
00307 MSG("NuBase",Msg::kInfo)
00308 <<"Requested object with name="<<objectName
00309 <<" exists in file:"<<endl;
00310 ob->Print();
00311 return true;
00312 }
00313 else {
00314 MSG("NuBase",Msg::kInfo)
00315 <<"Requested object with name="<<objectName
00316 <<" does NOT exist in file"<<endl;
00317 return false;
00318 }
00319 }
00320
00321
00322
00323 std::string NuBase::GetFirstFileName(std::string wildcardString) const
00324 {
00325
00326 if (!TString(wildcardString.c_str()).MaybeWildcard()) {
00327 return wildcardString;
00328 }
00329
00330
00331
00332 vector<string> fileList;
00333
00334
00335 TString basename(wildcardString.c_str());
00336
00337 Int_t dotslashpos = basename.Index(".root/");
00338 TString behind_dot_root;
00339 if (dotslashpos>=0) {
00340
00341 behind_dot_root = basename(dotslashpos+6,basename.Length()-dotslashpos+6);
00342
00343 basename.Remove(dotslashpos+5);
00344 }
00345
00346 Int_t slashpos = basename.Last('/');
00347 TString directory;
00348 if (slashpos>=0) {
00349 directory = basename(0,slashpos);
00350 basename.Remove(0,slashpos+1);
00351 } else {
00352 directory = gSystem->WorkingDirectory();
00353 }
00354
00355 const char *file;
00356 void *dir = gSystem->OpenDirectory(gSystem->ExpandPathName(directory.Data()));
00357
00358 if (dir) {
00359
00360 TList l;
00361 TRegexp re(basename,kTRUE);
00362 while ((file = gSystem->GetDirEntry(dir))) {
00363 if (!strcmp(file,".") || !strcmp(file,"..")) continue;
00364 TString s = file;
00365 if ( (basename!=file) && s.Index(re) == kNPOS) continue;
00366 l.Add(new TObjString(file));
00367 }
00368 gSystem->FreeDirectory(dir);
00369
00370 l.Sort();
00371 TIter next(&l);
00372 TObjString *obj;
00373 while ((obj = (TObjString*)next())) {
00374 file = obj->GetName();
00375 if (behind_dot_root.Length()!=0){
00376 string fileName=Form("%s/%s/%s",directory.Data(),
00377 file,behind_dot_root.Data());
00378 fileList.push_back(fileName);
00379
00380 }
00381 else {
00382 string fileName=Form("%s/%s",directory.Data(),file);
00383 fileList.push_back(fileName);
00384
00385 }
00386 }
00387 l.Delete();
00388 }
00389
00390
00391 if (fileList.begin()!=fileList.end()){
00392 cout<<"Used wildcard expansion to find first file name="
00393 <<*fileList.begin()<<endl;
00394 return *fileList.begin();
00395 }
00396 else {
00397 return "";
00398 }
00399 }
00400
00401
00402
00403 void NuBase::MakeChain()
00404 {
00405
00406 vector<string> fileList=this->MakeFileList();
00407
00408 TFile* firstFile=0;
00409
00410 if (!TString(*fileList.begin()).MaybeWildcard()) {
00411 fFirstFileName=(*fileList.begin());
00412 firstFile=TFile::Open((*fileList.begin()).c_str(),"READ");
00413 }
00414 else {
00415 MSG("NuBase",Msg::kInfo)
00416 <<"Found a wildcard present in list of file names: "
00417 <<*fileList.begin()
00418 <<endl<<"Looking for first filename using wildcard..."<<endl;
00419 string fileName=this->GetFirstFileName(*fileList.begin());
00420 fFirstFileName=fileName;
00421 if (fileName!="") {
00422 firstFile=TFile::Open(fileName.c_str(),"READ");
00423 }
00424 else {
00425 MSG("NuBase",Msg::kInfo)
00426 <<"No files found matching:"<<endl
00427 <<*fileList.begin()<<", will exit here..."<<endl;
00428 exit(1);
00429 }
00430 }
00431
00432
00433
00434 if (this->ObjectExistsInFile(firstFile,"NtpSt")) {
00435 fChain=new TChain("NtpSt");
00436 }
00437 else {
00438 MSG("NuBase",Msg::kInfo)
00439 <<"Not creating NtpSt TChain because it does not exist in file"
00440 <<endl;
00441 }
00442
00443
00444 if (this->ObjectExistsInFile(firstFile,"NtpBDLite")) {
00445 fChainBD=new TChain("NtpBDLite");
00446 }
00447 else {
00448 MSG("NuBase",Msg::kInfo)
00449 <<"Not creating NtpBDLite TChain because it does not exist"
00450 <<" in file"<<endl;
00451 }
00452
00453 firstFile->Close();
00454 if (firstFile) delete firstFile;
00455 firstFile=0;
00456
00457 Int_t nf=0;
00458 Int_t nfBD=0;
00459
00460 for (vector<string>::iterator file=fileList.begin();
00461 file!=fileList.end();++file){
00462
00463
00464 Bool_t openOk=false;
00465
00466 if ((*file).find("dcache")!=string::npos) {
00467 MAXMSG("NuBase",Msg::kInfo,3)
00468 <<"Filename contains \"dcache\" in path"
00469 <<" so not testing its existance"<<endl;
00470 openOk=true;
00471 }
00472 else {
00473 ifstream fileOpenOk((*file).c_str());
00474 openOk=fileOpenOk;
00475 }
00476
00477
00478 Int_t stars=(*file).find("*");
00479 Int_t quest=(*file).find("?");
00480
00481
00482 if (!openOk && !(quest>=0 || stars>=0)){
00483 MSG("NuBase",Msg::kInfo)
00484 <<endl<<endl
00485 <<"***********************************************************"
00486 <<endl<<"Can't find file="<<*file<<endl
00487 <<"Note: you can't use '~/'. It has to be the full path"<<endl
00488 <<"***********************************************************"
00489 <<endl<<endl
00490 <<"Exiting here!"<<endl;
00491 exit(0);
00492 }
00493
00494 MSG("NuBase",Msg::kInfo)<<"Adding file(s)="<<*file<<endl;
00495 nf+=fChain->Add((*file).c_str());
00496 if (fChainBD) nfBD+=fChainBD->Add((*file).c_str());
00497
00498 }
00499
00500 if(nf==0){
00501 MSG("NuBase",Msg::kFatal)
00502 <<endl<<endl
00503 <<"*************************************************************"
00504 <<endl<<"No *.root files found"<<endl
00505 <<"Please set NUDATA to the directory containing the"
00506 <<" *.root files"<<endl
00507 <<"Or give the txt file containing the files to be input"<<endl
00508 <<"Note: If more than one file is found they will be"
00509 <<" concatenated in a TChain and treated as one"<<endl
00510 <<"*************************************************************"
00511 <<endl<<endl<<"Program will exit here"<<endl;
00512 exit(0);
00513 }
00514
00515 MSG("NuBase",Msg::kInfo)
00516 <<endl<<"Added "<<nf<<" file(s) to TChain"<<endl;
00517 MSG("NuBase",Msg::kInfo)
00518 <<endl<<"Added "<<nfBD<<" beam data (BD) file(s) to TChain"
00519 <<endl;
00520
00521 if (fChain) {
00522 MSG("NuBase",Msg::kInfo)
00523 <<"Ntuple information:"<<endl;
00524 fChain->Show(0);
00525
00526 if (fChainBD) {
00527 MSG("NuBase",Msg::kInfo)
00528 <<"NtpBDLite information:"<<endl;
00529 fChainBD->Show(0);
00530 }
00531 }
00532
00533 MSG("NuBase",Msg::kInfo)
00534 <<endl<<"Added "<<nfBD<<" beam data (BD) file(s) to TChain."
00535 <<endl;
00536 MSG("NuBase",Msg::kInfo)
00537 <<endl<<"Analysing "<<nf<<" file(s). Reading from disk..."<<endl;
00538
00539
00540 fNumFiles=nf;
00541 }
00542
00543
00544
00545 void NuBase::SetRootFileObjects()
00546 {
00547 MSG("NuBase",Msg::kInfo)
00548 <<"Running the SetRootFileObjects method..."<<endl;
00549
00550
00551 fChain->SetBranchAddress("NtpStRecord",&fRec);
00552 if (fChainBD) fChainBD->SetBranchAddress("NtpBDLiteRecord",&fRecBD);
00553
00554
00555
00556 fEntries=static_cast<Int_t>(fChain->GetEntries());
00557 if (fChainBD) fEntriesBD=static_cast<Int_t>(fChainBD->GetEntries());
00558 MSG("NuBase",Msg::kInfo)
00559 <<"NtpSt tree has "<<fEntries<<" entries"<<endl;
00560 MSG("NuBase",Msg::kInfo)
00561 <<"NtpBDLite tree has "<<fEntriesBD<<" entries"<<endl;
00562
00563 if (fEntries>0){
00564
00565 fChain->GetEvent(0);
00566
00567
00568 MSG("NuBase",Msg::kInfo)
00569 <<"SetRootFileObjects: first snarl nslice="
00570 <<fRec->evthdr.nslice<<endl;
00571 }
00572
00573 if (fEntriesBD>0){
00574
00575 fChainBD->GetEvent(0);
00576 MSG("NuBase",Msg::kInfo)
00577 <<"First tortgt="<<fRecBD->tortgt<<" E12 POT"<<endl;
00578 }
00579 MSG("NuBase",Msg::kDebug)
00580 <<"Finished the SetRootFileObjects method"<<endl;
00581 }
00582
00583
00584
00585 TFile* NuBase::OpenFile(const NuConfig& config,std::string prefix) const
00586 {
00587
00588
00589 TH1::AddDirectory(true);
00590
00591
00592 TFile* outputFile=0;
00593
00594
00595 char *anaDir=getenv("NUANA_DIR");
00596
00597
00598 string sAnaDir="";
00599
00600 if (anaDir!=NULL) {
00601 sAnaDir=anaDir;
00602 }
00603 else {
00604 MSG("NuBase",Msg::kInfo)
00605 <<"Environmental variable $NUANA_DIR not set."
00606 <<" Writing file(s) to current directory"<<endl;
00607 sAnaDir=".";
00608 }
00609
00610
00611 string sRunNumber=Form("%d",config.run);
00612 string sZeros="";
00613 if (config.run>=0 && config.run<10) sZeros="00000000";
00614 else if (config.run>=10 && config.run<100) sZeros="000000";
00615 else if (config.run>=100 && config.run<1000) sZeros="00000";
00616 else if (config.run>=1000 && config.run<10000) sZeros="0000";
00617 else if (config.run>=10000 && config.run<100000) sZeros="000";
00618 else if (config.run>=100000 && config.run<1000000) sZeros="00";
00619 else if (config.run>=1000000 && config.run<10000000) sZeros="0";
00620 else if (config.run>=10000000 && config.run<100000000) sZeros="";
00621 sRunNumber=sZeros+sRunNumber;
00622
00623 string sDetector="UnknownDet";
00624 if (config.detector==Detector::kNear) {
00625 sDetector="N";
00626 if (config.simFlag==SimFlag::kMC) sDetector="n";
00627 }
00628 else if (config.detector==Detector::kFar) {
00629 sDetector="F";
00630 if (config.simFlag==SimFlag::kMC) sDetector="f";
00631 }
00632 else if (config.detector==Detector::kCalDet) {
00633 sDetector="C";
00634 if (config.simFlag==SimFlag::kMC) sDetector="c";
00635 }
00636 else cout<<"Ahhh, don't know detector="<<config.detector<<endl;
00637
00638 string sPrefix="";
00639 if (prefix!="") sPrefix+=prefix;
00640 string sBase=sAnaDir+"/"+sPrefix+sDetector+sRunNumber;
00641 string sFileName=sBase+".root";
00642
00643
00644 ifstream Test(sFileName.c_str());
00645
00646
00647 if(!Test){
00648 outputFile=new TFile(sFileName.c_str(),"RECREATE");
00649 }
00650 else {
00651
00652 Int_t fred=1;
00653 while(Test) {
00654 Test.close();
00655 string sAppendage=Form("%d",fred);
00656 sFileName=sBase+"_"+sAppendage+".root";
00657 Test.open(sFileName.c_str());
00658 fred++;
00659 }
00660 outputFile=new TFile(sFileName.c_str(),"NEW");
00661 outputFile->SetCompressionLevel(9);
00662 }
00663
00664 string sTmp="No File!";
00665 if (outputFile) sTmp=outputFile->GetName();
00666
00667
00668
00669
00670
00671 TH1F* hError=new TH1F("hError","hError",110,-10,100);
00672 hError->GetXaxis()->SetTitle("Error code");
00673 hError->GetXaxis()->CenterTitle();
00674
00675 MSG("NuBase",Msg::kInfo)
00676 <<"Output file opened: "<<sTmp<<endl;
00677 return outputFile;
00678 }
00679
00680
00681
00682 TFile* NuBase::OpenFile(Int_t runNumber,std::string prefix) const
00683 {
00684
00685
00686 TH1::AddDirectory(true);
00687
00688
00689 TFile* outputFile=0;
00690
00691
00692 char *anaDir=getenv("NUANA_DIR");
00693
00694
00695 string sAnaDir="";
00696
00697 if (anaDir!=NULL) {
00698 sAnaDir=anaDir;
00699 }
00700 else {
00701 MSG("NuBase",Msg::kInfo)
00702 <<"Environmental variable $NUANA_DIR not set."
00703 <<" Writing file(s) to current directory"<<endl;
00704 sAnaDir=".";
00705 }
00706
00707
00708 string sRunNumber=Form("%d",runNumber);
00709 string sZeros="";
00710 if (runNumber>=0 && runNumber<10) sZeros="00000000";
00711 else if (runNumber>=10 && runNumber<100) sZeros="000000";
00712 else if (runNumber>=100 && runNumber<1000) sZeros="00000";
00713 else if (runNumber>=1000 && runNumber<10000) sZeros="0000";
00714 else if (runNumber>=10000 && runNumber<100000) sZeros="000";
00715 else if (runNumber>=100000 && runNumber<1000000) sZeros="00";
00716 else if (runNumber>=1000000 && runNumber<10000000) sZeros="0";
00717 else if (runNumber>=10000000 && runNumber<100000000) sZeros="";
00718 sRunNumber=sZeros+sRunNumber;
00719
00720
00721 string sDetector="";
00722
00723 string sPrefix="";
00724 if (prefix!="") sPrefix+=prefix;
00725 string sBase=sAnaDir+"/"+sPrefix+sDetector+sRunNumber;
00726 string sFileName=sBase+".root";
00727
00728
00729 ifstream Test(sFileName.c_str());
00730
00731
00732 if(!Test){
00733 outputFile=new TFile(sFileName.c_str(),"RECREATE");
00734 }
00735 else {
00736
00737 Int_t fred=1;
00738 while(Test) {
00739 Test.close();
00740 string sAppendage=Form("%d",fred);
00741 sFileName=sBase+"_"+sAppendage+".root";
00742 Test.open(sFileName.c_str());
00743 fred++;
00744 }
00745 outputFile=new TFile(sFileName.c_str(),"NEW");
00746 outputFile->SetCompressionLevel(9);
00747 }
00748
00749 string sTmp="No File!";
00750 if (outputFile) sTmp=outputFile->GetName();
00751
00752 MSG("NuBase",Msg::kInfo)
00753 <<"Output file opened: "<<sTmp<<endl;
00754 return outputFile;
00755 }
00756
00757
00758
00759 TFile* NuBase::OpenFileRECREATE(std::string sFileName) const
00760 {
00761
00762
00763 TH1::AddDirectory(true);
00764
00765
00766 TFile* outputFile=0;
00767
00768 outputFile=new TFile(sFileName.c_str(),"RECREATE");
00769
00770 MSG("NuBase",Msg::kInfo)
00771 <<"Output file opened: "<<outputFile->GetName()<<endl;
00772 return outputFile;
00773 }
00774
00775
00776
00777 ofstream* NuBase::OpenTxtFile(const NuConfig& config,
00778 std::string prefix) const
00779 {
00780
00781 ofstream* outputFile=0;
00782
00783
00784 string sRunNumber=Form("%d",config.run);
00785 string sZeros="";
00786 if (config.run>=0 && config.run<10) sZeros="00000000";
00787 else if (config.run>=10 && config.run<100) sZeros="000000";
00788 else if (config.run>=100 && config.run<1000) sZeros="00000";
00789 else if (config.run>=1000 && config.run<10000) sZeros="0000";
00790 else if (config.run>=10000 && config.run<100000) sZeros="000";
00791 else if (config.run>=100000 && config.run<1000000) sZeros="00";
00792 else if (config.run>=1000000 && config.run<10000000) sZeros="0";
00793 else if (config.run>=10000000 && config.run<100000000) sZeros="";
00794 sRunNumber=sZeros+sRunNumber;
00795
00796 string sDetector="UnknownDet";
00797 if (config.detector==Detector::kNear) {
00798 sDetector="N";
00799 if (config.simFlag==SimFlag::kMC) sDetector="n";
00800 }
00801 else if (config.detector==Detector::kFar) {
00802 sDetector="F";
00803 if (config.simFlag==SimFlag::kMC) sDetector="f";
00804 }
00805 else if (config.detector==Detector::kCalDet) {
00806 sDetector="C";
00807 if (config.simFlag==SimFlag::kMC) sDetector="c";
00808 }
00809 else cout<<"Ahhh, don't know detector="<<config.detector<<endl;
00810
00811 string sPrefix="";
00812 string sAnaDir=".";
00813 if (prefix!="") sPrefix+=prefix;
00814 string sBase=sAnaDir+"/"+sPrefix+sDetector+sRunNumber;
00815 string sFileName=sBase+".txt";
00816
00817 outputFile=new ofstream(sFileName.c_str());
00818
00819 if (outputFile){
00820 MSG("NuBase",Msg::kInfo)
00821 <<"Output txt file opened: "<<sFileName<<endl;
00822 }
00823 else{
00824 MSG("NuBase",Msg::kInfo)
00825 <<"Txt file NOT opened: "<<sFileName<<endl;
00826 }
00827
00828 return outputFile;
00829 }
00830
00831
00832
00833 ofstream* NuBase::OpenTxtFile(Int_t runNumber,
00834 std::string prefix) const
00835 {
00836
00837 ofstream* outputFile=0;
00838
00839
00840 string sRunNumber=Form("%d",runNumber);
00841 string sZeros="";
00842 if (runNumber>=0 && runNumber<10) sZeros="00000000";
00843 else if (runNumber>=10 && runNumber<100) sZeros="000000";
00844 else if (runNumber>=100 && runNumber<1000) sZeros="00000";
00845 else if (runNumber>=1000 && runNumber<10000) sZeros="0000";
00846 else if (runNumber>=10000 && runNumber<100000) sZeros="000";
00847 else if (runNumber>=100000 && runNumber<1000000) sZeros="00";
00848 else if (runNumber>=1000000 && runNumber<10000000) sZeros="0";
00849 else if (runNumber>=10000000 && runNumber<100000000) sZeros="";
00850 sRunNumber=sZeros+sRunNumber;
00851
00852 string sDetectorType="Run";
00853 string sPrefix="";
00854 string sAnaDir=".";
00855 if (prefix!="") sPrefix+=prefix;
00856 string sBase=sAnaDir+"/"+sPrefix+sDetectorType+sRunNumber;
00857 string sFileName=sBase+".txt";
00858
00859 outputFile=new ofstream(sFileName.c_str());
00860
00861 if (outputFile){
00862 MSG("NuBase",Msg::kInfo)
00863 <<"Output txt file opened: "<<sFileName<<endl;
00864 }
00865 else{
00866 MSG("NuBase",Msg::kInfo)
00867 <<"Txt file NOT opened: "<<sFileName<<endl;
00868 }
00869
00870 return outputFile;
00871 }
00872
00873
00874
00875 void NuBase::SetLoopVariables(Int_t entry,Int_t printMode)
00876 {
00877 this->PrintLoopProgress(entry,fEntries,printMode);
00878
00879
00880 fChain->GetEvent(entry);
00881 if (fChainBD) fChainBD->GetEvent(entry);
00882 }
00883
00884
00885
00886 void NuBase::PrintLoopProgress(Int_t entry,Float_t nEntries,
00887 Int_t printMode) const
00888 {
00889 if (printMode==1){
00890 Float_t fract=ceil(nEntries/20.);
00891 if (ceil(((Float_t)entry)/fract)==((Float_t)entry)/fract){
00892 MSG("STAnalysis",Msg::kInfo)
00893 <<"Fraction of loop complete: "<<entry
00894 <<"/"<<nEntries<<" ("
00895 <<(Int_t)(100.*entry/nEntries)<<"%)"<<endl;
00896 }
00897 }
00898 }
00899
00900
00901
00902 void NuBase::InitialiseLoopVariables()
00903 {
00904 MSG("NuBase",Msg::kDebug)<<"Initialising loop variables..."<<endl;
00905
00906 MSG("NuBase",Msg::kDebug)<<"Initialisation complete"<<endl;
00907 }
00908
00909
00910
00911 void NuBase::Test()
00912 {
00913 MSG("NuBase",Msg::kInfo)
00914 <<" ** Running Test method... **"<<endl;
00915
00919
00920
00921
00922
00923
00924
00925
00926 for (int i=0;i<5;i++) {
00927 fChain->GetEntry(i);
00928 NtpStRecord& ntpst=(*fRec);
00929
00930 TClonesArray& tcaTk=(*ntpst.trk);
00931 Int_t numTrks=tcaTk.GetEntries();
00932
00933 MSG("NuBase",Msg::kInfo)
00934 <<"numTrks="<<numTrks
00935 <<", ndigit="<<fRec->evthdr.ndigit
00936 <<", nTrack="<<fRec->evthdr.ntrack<<endl;
00937
00938
00939 for (Int_t itrk=0;itrk<numTrks;itrk++){
00940 const NtpSRTrack* ptrk=
00941 dynamic_cast<NtpSRTrack*>(tcaTk[itrk]);
00942 const NtpSRTrack& trk=(*ptrk);
00943 cout<<"range="<<trk.range<<" nstrip="<<trk.nstrip<<" ds="<<trk.ds
00944 <<" fidend.dr,dz="<<trk.fidend.dr<<" "<<trk.fidend.dz<<" end-vtx.z:"
00945 <<trk.end.z-trk.vtx.z<<" "<<trk.stpz[1]-trk.stpz[0]<<endl;
00946
00947
00948 TClonesArray& tcaStp=(*ntpst.stp);
00949 Int_t numStps=tcaStp.GetEntries();
00950 MSG("NuBase",Msg::kInfo)
00951 <<"numStps="<<numStps<<endl;
00952
00953 for (Int_t i=0;i<trk.nstrip;i++){
00954
00955
00956 const NtpSRStrip* pstp=dynamic_cast<NtpSRStrip*>(tcaStp[trk.stp[i]]);
00957
00958 const NtpSRStrip& stp=(*pstp);
00959 MSG("NuBase",Msg::kInfo)
00960 <<"Strip="<<stp.strip<<", pl="<<stp.plane
00961 <<", sigCor="<<stp.ph1.sigcor<<", tpos="<<stp.tpos<<endl;
00962 }
00963
00964 }
00965
00966
00967 TClonesArray& tcaStp=(*ntpst.stp);
00968 Int_t numStps=tcaStp.GetEntries();
00969 MSG("NuBase",Msg::kInfo)
00970 <<endl<<"2nd: numStps="<<numStps<<endl;
00971
00972 for (Int_t i=0;i<numStps;i++){
00973 const NtpSRStrip* pstp=dynamic_cast<NtpSRStrip*>(tcaStp[i]);
00974 const NtpSRStrip& stp=(*pstp);
00975 MSG("NuBase",Msg::kInfo)
00976 <<"Strip="<<stp.strip<<", pl="<<stp.plane<<endl;
00977 }
00978
00979
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007 }
01008
01012
01013 MSG("NuBase",Msg::kInfo)<<"Finished main loop"<<endl;
01014
01015 MSG("NuBase",Msg::kInfo)
01016 <<" ** Finished Test method **"<<endl;
01017 }
01018
01019
01020
01021 void NuBase::TestEventLoop()
01022 {
01023 MSG("NuBase",Msg::kInfo)
01024 <<" ** Running TestEventLoop method... **"<<endl;
01025
01029
01030 this->InitialiseLoopVariables();
01031
01032 for(Int_t entry=0;entry<fEntries;entry++) {
01033
01034
01035 this->SetLoopVariables(entry);
01036
01037 NtpStRecord& ntp=(*fRec);
01038
01039 TClonesArray& evtTca=(*ntp.evt);
01040 const Int_t numEvts=evtTca.GetEntriesFast();
01041
01042 TClonesArray& trkTca=(*ntp.trk);
01043 const Int_t numTrks=trkTca.GetEntriesFast();
01044
01045 TClonesArray& shwTca=(*ntp.shw);
01046 const Int_t numShws=shwTca.GetEntriesFast();
01047
01048 TClonesArray& stpTca=(*ntp.stp);
01049 const Int_t numStps=stpTca.GetEntries();
01050
01051 TClonesArray& slcTca=(*ntp.slc);
01052 const Int_t numSlcs=slcTca.GetEntries();
01053
01054 Float_t totalSnarlAdc=0;
01055 Float_t totalEvtAdc=0;
01056 Float_t totalSlcAdc=0;
01057 Float_t thisSlcAdc=0;
01058
01059 for (Int_t i=0;i<numStps;i++) {
01060 const NtpSRStrip* pstp=
01061 dynamic_cast<NtpSRStrip*>(stpTca[i]);
01062 const NtpSRStrip& stp=(*pstp);
01063 totalSnarlAdc+=stp.ph0.raw+stp.ph1.raw;
01064 }
01065
01066 if (numEvts==0 && numTrks>0) {
01067 cout<<"Ahhh, num events in tca="<<numEvts
01068 <<", trks="<<numTrks<<endl;
01069 }
01070
01071 if (numEvts>1) {
01072 MAXMSG("NuBase",Msg::kInfo,10)
01073 <<"High num evts="<<numEvts<<", trks="<<numTrks<<endl;
01074 }
01075
01076 if (numSlcs!=1) {
01077 MAXMSG("NuBase",Msg::kInfo,10)
01078 <<"High num slices in tca="<<numSlcs
01079 <<", trks="<<numTrks<<endl;
01080
01081 }
01082
01083 MAXMSG("NuBase",Msg::kInfo,100)
01084 <<"Num events in tca="<<numEvts
01085 <<", slcs="<<numSlcs<<", trks="<<numTrks<<endl;
01086
01087 Int_t slcStps=0;
01088
01089
01090 for (Int_t islc=0;islc<numSlcs;++islc) {
01091 const NtpSRSlice* pslc=
01092 dynamic_cast<NtpSRSlice*>(slcTca[islc]);
01093 const NtpSRSlice& slc=(*pslc);
01094
01095 MAXMSG("NuBase",Msg::kDebug,200)
01096 <<" slice "<<islc+1<<" of "<<numSlcs<<endl;
01097
01098 slcStps+=slc.nstrip;
01099
01100
01101 for (Int_t i=0;i<slc.nstrip;++i) {
01102 const NtpSRStrip* pstp=
01103 dynamic_cast<NtpSRStrip*>(stpTca[slc.stp[i]]);
01104 const NtpSRStrip& stp=(*pstp);
01105 totalSlcAdc+=stp.ph0.raw+stp.ph1.raw;
01106 }
01107
01108
01109
01110
01111
01112
01113
01114
01115
01116
01117
01118
01119 }
01120
01121 map<Int_t,Int_t> evtPerSlc;
01122
01123
01124 for (Int_t ntpevt=0;ntpevt<numEvts;++ntpevt) {
01125 const NtpSREvent* pevt=
01126 dynamic_cast<NtpSREvent*>(evtTca[ntpevt]);
01127 const NtpSREvent& evt=(*pevt);
01128
01129 MAXMSG("NuBase",Msg::kInfo,100)
01130 <<"Entry "<<entry<<" has tracks="<<evt.ntrack
01131 <<", shws="<<numShws<<", slcs="<<numSlcs<<endl;
01132
01133
01134 const NtpSRSlice* pslc=
01135 dynamic_cast<NtpSRSlice*>(slcTca[evt.slc]);
01136 const NtpSRSlice& slc=(*pslc);
01137
01138 evtPerSlc[evt.slc]++;
01139
01140
01141 Int_t evts=-1;
01142 Bool_t goodEvt=true;
01143 if (goodEvt){
01144 MAXMSG("NuBase",Msg::kInfo,100)
01145 <<"TEST::entry="<<entry
01146 <<", evt="<<ntpevt<<" is good, evtsPerSlc="<<evts
01147 <<" (slc="<<evt.slc<<")"<<endl;
01148 }
01149 else{
01150 MAXMSG("NuBase",Msg::kInfo,100)
01151 <<"TEST::entry="<<entry
01152 <<", evt="<<ntpevt<<" is bad, evtsPerSlc="<<evts
01153 <<" (slc="<<evt.slc<<")"<<endl;
01154 }
01155
01156 if (evt.ntrack!=1 || evt.nshower>1) continue;
01157
01158 Int_t trkStps=0;
01159 Int_t shwStps=0;
01160
01161
01162 for (Int_t i=0;i<slc.nstrip;++i) {
01163 const NtpSRStrip* pstp=
01164 dynamic_cast<NtpSRStrip*>(stpTca[slc.stp[i]]);
01165 const NtpSRStrip& stp=(*pstp);
01166 thisSlcAdc+=stp.ph0.raw+stp.ph1.raw;
01167 }
01168
01169
01170 for(int i=0;i<evt.ntrack;i++) {
01171 const NtpSRTrack* ptrk=
01172 dynamic_cast<NtpSRTrack*>(trkTca[evt.trk[i]]);
01173 const NtpSRTrack& trk=(*ptrk);
01174
01175 trkStps=trk.nstrip;
01176
01177 for (Int_t i=0;i<trk.nstrip;i++) {
01178 const NtpSRStrip* pstp=
01179 dynamic_cast<NtpSRStrip*>(stpTca[trk.stp[i]]);
01180 const NtpSRStrip& stp=(*pstp);
01181 MAXMSG("NuBase",Msg::kDebug,100)
01182 <<"strip time="<<stp.time0<<endl;
01183 }
01184 }
01185
01186
01187 for(int i=0;i<evt.nshower;i++) {
01188 const NtpSRShower* pshw=
01189 dynamic_cast<NtpSRShower*>(shwTca[evt.shw[i]]);
01190 const NtpSRShower& shw=(*pshw);
01191
01192 shwStps=shw.nstrip;
01193 }
01194
01195
01196 for (Int_t i=0;i<evt.nstrip;i++) {
01197 const NtpSRStrip* pstp=
01198 dynamic_cast<NtpSRStrip*>(stpTca[evt.stp[i]]);
01199 const NtpSRStrip& stp=(*pstp);
01200 totalEvtAdc+=stp.ph0.raw+stp.ph1.raw;
01201 }
01202 MAXMSG("NuBase",Msg::kInfo,100)
01203 <<"e="<<entry<<", evt="<<ntpevt
01204 <<", ADCs: snl="<<totalSnarlAdc
01205 <<", evt="<<totalEvtAdc
01206 <<", slc="<<totalSlcAdc
01207 <<", tslc="<<thisSlcAdc
01208 <<", #st="<<numStps<<","<<evt.nstrip<<","<<slcStps
01209
01210 <<endl;
01211 }
01212
01213
01214
01215 typedef map<Int_t,Int_t>::const_iterator slcIt;
01216 MAXMSG("NuBase",Msg::kInfo,30)
01217 <<"MAIN::Events per slice for entry="<<entry<<endl;
01218 for (slcIt it=evtPerSlc.begin();it!=evtPerSlc.end();++it) {
01219 MAXMSG("NuBase",Msg::kInfo,200)
01220 <<" slice "<<it->first<<" has "<<it->second<<" event(s)"<<endl;
01221 }
01222
01223 }
01224
01228
01229 MSG("NuBase",Msg::kInfo)<<"Finished main loop"<<endl;
01230
01231 MSG("NuBase",Msg::kInfo)
01232 <<" ** Finished TestEventLoop method **"<<endl;
01233 }
01234
01235
01236