Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:28:17

0001 #include <vector>
0002 #include "TROOT.h"
0003 #ifdef __APPLE__
0004 #include <unistd.h>
0005 #endif
0006 #include "TF1.h"
0007 #include "TFitResult.h"
0008 #include "TFitResultPtr.h"
0009 #include "TH1D.h"
0010 #include "TH2D.h"
0011 #include "TProfile.h"
0012 #include "TChain.h"
0013 #include "TileSpectra.h"
0014 #include "TileTrend.h"
0015 #include "AnaSummary.h"
0016 #include "CalibSummary.h"
0017 #include "CommonHelperFunctions.h"
0018 #include "PlotHelper.h"
0019 #include "ComparisonAna.h"
0020 
0021 
0022 //Input Setup
0023 
0024 bool ComparisonAna::CheckAndOpenIO(void){
0025   
0026   int matchingbranch;
0027   
0028   // *****************************************************************************************
0029   // Reading files from a text file
0030   // *****************************************************************************************
0031   if(!InputListName.IsNull()){
0032     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0033     // text file with only 1 calib-file per line 
0034     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0035     if (expandedList == 0){
0036       std::cout << "*********************************************************" << std::endl;
0037       std::cout << "Simple reading from file list" << std::endl;
0038       std::cout << "*********************************************************" << std::endl;
0039       // File exist?
0040       std::cout << "reading from file list: " << InputListName.Data() << std::endl;
0041       std::fstream dummyTxt;
0042       dummyTxt.open(InputListName.Data(),std::ios::in);
0043       if(!dummyTxt.is_open()){
0044         std::cout<<"Error opening "<<InputListName.Data()<<", does the file exist?"<<std::endl;
0045       }
0046     
0047       // initialize root file name
0048       std::string dummyRootName;
0049       // set first root file name
0050       dummyTxt>>dummyRootName;
0051       // read files from text file
0052       int goodsetup;
0053       int goodcalib;
0054       while(dummyTxt.good()){
0055         std::cout << dummyRootName.data() << std::endl;
0056         // check that file exists and can be opened
0057         TFile dummyRoot=TFile(dummyRootName.c_str(),"READ");
0058         if(dummyRoot.IsZombie()){
0059           std::cout<<"Error opening '"<<dummyRootName<<", does the file exist?"<<std::endl;
0060           return false;
0061         }
0062         dummyRoot.Close();
0063         // Add file-name to setup and calib chain
0064         goodsetup=TsetupIn->AddFile(dummyRootName.c_str());
0065         goodcalib=TcalibIn->AddFile(dummyRootName.c_str());
0066         if(goodcalib==0){
0067           std::cout<<"Issues retrieving Calib tree from "<<dummyRootName<<", file is ignored"<<std::endl;
0068         }
0069         if(goodsetup==0){
0070           std::cout<<"Issues retrieving Setup tree from "<<dummyRootName<<", file is ignored"<<std::endl;
0071         }
0072         // set next root file name
0073         dummyTxt>>dummyRootName;
0074       }
0075     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0076     // text file with 2 files per line 1 calib file & 1 histo file
0077     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0078     } else {  // read more complex files
0079       std::cout << "You need to provide a calib file and an output of the improved muon calib in the textfile in each line" << std::endl;
0080       std::fstream dummyTxt;
0081       dummyTxt.open(InputListName.Data(),std::ios::in);
0082       if(!dummyTxt.is_open()){
0083         std::cout<<"Error opening "<<InputListName.Data()<<", does the file exist?"<<std::endl;
0084       }
0085       std::string dummyRootCalibName;
0086       std::string dummyRootHistName;
0087       // set first root file names
0088       dummyTxt>>dummyRootCalibName >> dummyRootHistName;
0089       
0090       int goodsetup;
0091       int goodcalib;
0092       while(dummyTxt.good()){
0093         std::cout << dummyRootCalibName.data() << "\t" << dummyRootHistName.data() << std::endl;
0094         
0095         // check that files exist and can be opened
0096         TFile dummyRootCalib=TFile(dummyRootCalibName.c_str(),"READ");
0097         if(dummyRootCalib.IsZombie()){
0098           std::cout<<"Error opening '"<<dummyRootCalibName<<", does the file exist?"<<std::endl;
0099           return false;
0100         }
0101         dummyRootCalib.Close();
0102         TFile dummyRootHist=TFile(dummyRootHistName.c_str(),"READ");
0103         if(dummyRootHist.IsZombie()){
0104           std::cout<<"Error opening '"<<dummyRootHistName<<", does the file exist?"<<std::endl;
0105           return false;
0106         }
0107         dummyRootHist.Close();
0108         
0109         // Add file-name to setup and calib chain as well string-vector
0110         AddInputFile(dummyRootHistName);
0111         goodsetup=TsetupIn->AddFile(dummyRootCalibName.c_str());
0112         goodcalib=TcalibIn->AddFile(dummyRootCalibName.c_str());
0113         if(goodcalib==0){
0114           std::cout<<"Issues retrieving Calib tree from "<<dummyRootCalibName<<", file is ignored"<<std::endl;
0115         }
0116         if(goodsetup==0){
0117           std::cout<<"Issues retrieving Setup tree from "<<dummyRootCalibName<<", file is ignored"<<std::endl;
0118         }
0119         // set next root file names
0120         dummyTxt>>dummyRootCalibName >> dummyRootHistName;
0121       }
0122     }
0123   }
0124   // *****************************************************************************************
0125   // Reading files by auto-expansion
0126   // - we did not provide a txt file with a list of root file we want to process but directly 
0127   //   the list of files to process
0128   // *****************************************************************************************
0129   else {
0130     std::cout << "*********************************************************" << std::endl;
0131     std::cout << "Simple reading from vector" << std::endl;
0132     std::cout << "*********************************************************" << std::endl;
0133     
0134     // check if file vector is empty
0135     if(RootInputNames.empty()){
0136       std::cout<<"No root files, neither text list file provided"<<std::endl;
0137       return false;
0138     }
0139     
0140     // read input files from string vector
0141     std::vector<TString>::iterator it;
0142     int goodsetup;
0143     int goodcalib;
0144     for(it=RootInputNames.begin(); it!=RootInputNames.end(); ++it){
0145       // check if file exists
0146       TFile dummy=TFile(it->Data(),"READ");
0147       if(dummy.IsZombie()){
0148         std::cout<<"Error opening '"<<it->Data()<<", does the file exist?"<<std::endl;
0149         return false;
0150       }
0151       dummy.Close();
0152       // Add file-name to setup and calib chain
0153       goodsetup=TsetupIn->AddFile(it->Data());
0154       goodcalib=TcalibIn->AddFile(it->Data());
0155       if(goodcalib==0){
0156         std::cout<<"Issues retrieving Calib tree from "<<it->Data()<<", file is ignored"<<std::endl;
0157       }
0158       if(goodsetup==0){
0159         std::cout<<"Issues retrieving Setup tree from "<<it->Data()<<", file is ignored"<<std::endl;
0160       }
0161     }
0162   }
0163   // *****************************************************************************************
0164   // Setup Output files
0165   // *****************************************************************************************
0166   if(RootOutputName.IsNull()){
0167     return false;
0168   } else {
0169     if(!CreateOutputRootFile()){
0170       return false;
0171     }
0172   }
0173 
0174   // *****************************************************************************************
0175   // Setup TChain of setups and calibrations
0176   // *****************************************************************************************
0177   // intialize global variable setup
0178   setup=Setup::GetInstance();
0179   std::cout<<"Setup add "<<setup<<std::endl;
0180   matchingbranch=TsetupIn->SetBranchAddress("setup",&rswptr);
0181   if(matchingbranch<0){
0182     std::cout<<"Error retrieving Setup info from the tree"<<std::endl;
0183     return false;
0184   }
0185   std::cout<<"Entries "<<TsetupIn->GetEntries()<<std::endl;
0186   TsetupIn->GetEntry(0);
0187   setup->Initialize(*rswptr);
0188   // initialize calib with the correct branch
0189   matchingbranch=TcalibIn->SetBranchAddress("calib",&calibptr);
0190   if(matchingbranch<0){
0191     std::cout<<"Error retrieving calibration info from the tree"<<std::endl;
0192     return false;
0193   }
0194   
0195   return true;    
0196 }
0197 
0198 
0199 
0200 
0201 // Main Loop
0202 bool ComparisonAna::ProcessAna(void){
0203     if(expandedList==4){
0204       std::cout<<"in process"<<std::endl;
0205         // *****************************************************************************************
0206         // plotting settings
0207         // *****************************************************************************************
0208         gSystem->Exec("mkdir -p "+OutputNameDirPlots);
0209         if (ExtPlot > 0) gSystem->Exec("mkdir -p "+OutputNameDirPlots+"/SingleLayer");
0210         StyleSettingsBasics("pdf");
0211         SetPlotStyle();  
0212 
0213         // *****************************************************************************************
0214         // Some general setup
0215         // *****************************************************************************************
0216         bool status=true;
0217         // enbale implitcit root multithreading
0218         ROOT::EnableImplicitMT();
0219         // get nuber of entires from Calib tree (how many runs do we have)
0220         int entries=TcalibIn->GetEntries();
0221 
0222         // *****************************************************************************************
0223         // global variable setup, common iterators and ranges
0224         // ******************************************************************************************
0225 
0226         std::map<int, AnaSummary> sumCalibs;
0227         std::map<int, AnaSummary>::iterator isumCalibs;
0228         
0229         double Xvalue;
0230         double Xmin= 9999.;
0231         double Xmax=-9999.;
0232         int nRun = 0;
0233         
0234         // ******************************************************************************************
0235         // ************* Get run data base to potentially obtain more information from file *********
0236         // ******************************************************************************************
0237         std::map<int,RunInfo> ri=readRunInfosFromFile(RunListInputName.Data(),debug,0);
0238         std::map<int,RunInfo>::iterator it; // basic infos
0239         
0240         // ******************************************************************************************
0241         // Iterate over all entries (runs) in the calib tree
0242         // ******************************************************************************************
0243         for(int ientry=0; ientry<entries;ientry++){
0244             TcalibIn->GetEntry(ientry);
0245             TsetupIn->GetEntry(ientry);
0246             
0247             // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0248             // set global iterator for runs to first run number in list to obtain beam-line, dates...
0249 
0250             if (ientry==0) it = ri.find(calib.GetRunNumber());
0251                 Xvalue=calib.GetRunNumber();
0252               // TODO: Change id from run number, look at constructor ()
0253                 AnaSummary aSum = AnaSummary(nRun, calib.GetRunNumber(),calib.GetVop());
0254 
0255                 TH1D* hTimeDiff = nullptr;
0256                 TFile* tempFile = nullptr;
0257             if (nRun < (int)RootInputNames.size()){
0258                 //std::cerr << "names: " << RootInputNames[nRun].Data() << std::endl;
0259                 tempFile      = new TFile(RootInputNames[nRun].Data(),"READ");   
0260                 TH1D* hTimeDiff = (TH1D*)tempFile->Get("hDeltaTime");
0261                 aSum.SetDeltaTimeHist(hTimeDiff);
0262                 std::cout<<"Run "<<Xvalue<<" mean: "<< hTimeDiff->GetMean()<<std::endl;
0263             }
0264             // append AnaSummary object to map
0265             std::cout<<"filling for " << nRun <<std::endl; 
0266             sumCalibs[nRun]=aSum;
0267             nRun++;
0268         }
0269         Int_t textSizePixel   = 30;
0270         Float_t textSizeRel   = 0.04;  
0271         TCanvas* canvasDeltaTime = new TCanvas("canvasDeltaTime","",0,0,1450,1300);  // gives the page size
0272         DefaultCancasSettings( canvasDeltaTime,  0.08, 0.062, 0.025, 0.09);
0273         canvasDeltaTime->SetLogy(1);
0274         PlotAnalysisComparison( canvasDeltaTime, 0, sumCalibs, textSizeRel, 
0275                             Form("%s/TimeDiff_RunOverlay.%s",OutputNameDirPlots.Data(),plotSuffix.Data()), it->second,"", debug);
0276         return true;
0277     }
0278     else{
0279         return false;
0280     }
0281 }
0282 
0283 
0284 // Make Output File
0285 bool ComparisonAna::CreateOutputRootFile(void){
0286   if(Overwrite){
0287     RootOutput=new TFile(RootOutputName.Data(),"RECREATE");
0288   } else{
0289     RootOutput = new TFile(RootOutputName.Data(),"CREATE");
0290   }
0291   if(RootOutput->IsZombie()){
0292     std::cout<<"Error opening '"<<RootOutput<<"'no reachable path? Exist without force mode to overwrite?..."<<std::endl;
0293     return false;
0294   }
0295   return true;
0296 }