Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-12-03 09:53:19

0001 #include "DataAnalysis.h"
0002 #include <vector>
0003 #include "TROOT.h"
0004 #include <cmath>  
0005 #include <algorithm>
0006 //#include <unistd.h> // Add for use on Mac OS -> Same goes for Analyse.cc
0007 #include "TF1.h"
0008 #include "TFitResult.h"
0009 #include "TFitResultPtr.h"
0010 #include "TH1D.h"
0011 #include "TH2D.h"
0012 #include "TProfile.h"
0013 #include "TChain.h"
0014 #include "CommonHelperFunctions.h"
0015 #include "TileSpectra.h"
0016 #include "MultiCanvas.h"
0017 #include "PlotHelper.h"
0018 #include "TRandom3.h"
0019 #include "TMath.h"
0020 #include "Math/Minimizer.h"
0021 #include "Math/MinimizerOptions.h"
0022 
0023 // ****************************************************************************
0024 // Checking and opening input and output files
0025 // ****************************************************************************
0026 bool DataAnalysis::CheckAndOpenIO(void){
0027   int matchingbranch;
0028 
0029   //Need to check first input to get the setup...I do not think it is necessary
0030     std::cout<<"Input name set to: '"<<RootInputName.Data() <<std::endl;  
0031   if(!RootInputName.IsNull()){
0032     //File exist?
0033     RootInput=new TFile(RootInputName.Data(),"READ");
0034     if(RootInput->IsZombie()){
0035       std::cout<<"Error opening '"<<RootInputName<<"', does the file exist?"<<std::endl;
0036       return false;
0037     }
0038 
0039     //Retrieve info, start with setup
0040     TsetupIn = (TTree*) RootInput->Get("Setup");
0041     if(!TsetupIn){
0042       std::cout<<"Could not retrieve the Setup tree, leaving"<<std::endl;
0043       return false;
0044     }
0045     setup=Setup::GetInstance();
0046     std::cout<<"Setup add "<<setup<<std::endl;
0047     //matchingbranch=TsetupIn->SetBranchAddress("setup",&setup);
0048     matchingbranch=TsetupIn->SetBranchAddress("setup",&rswptr);
0049     if(matchingbranch<0){
0050       std::cout<<"Error retrieving Setup info from the tree"<<std::endl;
0051       return false;
0052     }
0053     std::cout<<"Entries "<<TsetupIn->GetEntries()<<std::endl;
0054     TsetupIn->GetEntry(0);
0055     setup->Initialize(*rswptr);
0056     std::cout<<"Reading "<<RootInput->GetName()<<std::endl;
0057     std::cout<<"Setup Info "<<setup->IsInit()<<"  and  "<<setup->GetCellID(0,0)<<std::endl;
0058     //std::cout<<"Setup add now "<<setup<<std::endl;
0059 
0060     //Retrieve info, existing data?
0061     TdataIn = (TTree*) RootInput->Get("Data");
0062     if(!TdataIn){
0063       std::cout<<"Could not retrieve the Data tree, leaving"<<std::endl;
0064       return false;
0065     }
0066     matchingbranch=TdataIn->SetBranchAddress("event",&eventptr);
0067     if(matchingbranch<0){
0068       std::cout<<"Error retrieving Event info from the tree"<<std::endl;
0069       return false;
0070     }
0071     
0072     TcalibIn = (TTree*) RootInput->Get("Calib");
0073     if(!TcalibIn){
0074       std::cout<<"Could not retrieve Calib tree, leaving"<<std::endl;
0075       //return false;
0076     }
0077     else {
0078       matchingbranch=TcalibIn->SetBranchAddress("calib",&calibptr);
0079       if(matchingbranch<0){
0080         std::cout<<"Error retrieving calibration info from the tree"<<std::endl;
0081         TcalibIn=nullptr;
0082       }
0083     }
0084   } else {
0085     std::cout<<"An input file is required, aborting"<<std::endl;
0086     return false;
0087   }  
0088   
0089   //Setup Output files
0090   if(!RootOutputName.IsNull()){    
0091     
0092     bool sCOF = CreateOutputRootFile();
0093     if (!sCOF) return false;
0094     
0095     TsetupOut = new TTree("Setup","Setup");
0096     setup=Setup::GetInstance();
0097     TsetupOut->Branch("setup",&rsw);
0098     TdataOut = new TTree("Data","Data");
0099     TdataOut->Branch("event",&event);
0100     TcalibOut = new TTree("Calib","Calib");
0101     TcalibOut->Branch("calib",&calib);
0102   } 
0103   
0104   //Setup Output files
0105   if(!RootOutputNameHist.IsNull()){    
0106     std::cout<<"Creating output file for hists: "<< RootOutputNameHist.Data() <<std::endl;
0107     bool sCOF = CreateOutputRootFileHist();
0108     if (!sCOF) return false;
0109   }
0110   
0111   if ( RootOutputName.IsNull() && RootOutputNameHist.IsNull()){
0112     std::cout<<"Output file name is missing, please add"<<std::endl;
0113     return false;
0114   }  
0115   
0116   return true;
0117 }
0118 
0119 // ****************************************************************************
0120 // Primary process function to start all calibrations
0121 // ****************************************************************************
0122 bool DataAnalysis::Process(void){
0123   bool status;
0124   ROOT::EnableImplicitMT();
0125   
0126   if(RunQA){
0127     status=QAData();
0128   }  
0129   
0130   if(RunSimpleQA){
0131     status=SimpleQAData();
0132   }  
0133   return status;
0134 }
0135 
0136 
0137 
0138 //***********************************************************************************************
0139 //*********************** Advanced QA routine ***************************************************
0140 //***********************************************************************************************
0141 bool DataAnalysis::QAData(void){
0142   std::cout<<"QA data"<<std::endl;
0143   if(debug==1000){
0144     std::cerr<<"Time Min: "<<timemin<<std::endl;
0145     std::cerr<<"Time Max: "<<timemax<<std::endl;
0146   }
0147   std::map<int,RunInfo> ri=readRunInfosFromFile(RunListInputName.Data(),debug,0);
0148   TdataIn->GetEntry(0);
0149   Int_t runNr           = event.GetRunNumber();
0150   ReadOut::Type typeRO  = event.GetROtype();
0151 
0152   // Get run info object
0153   std::map<int,RunInfo>::iterator it=ri.find(runNr);
0154 
0155   int species = -1;
0156   species = GetSpeciesIntFromRunInfo(it->second);
0157   float beamE = it->second.energy;
0158   std::cout << "Beam energy:" << beamE << std::endl;
0159   if (species == -1){
0160       std::cout << "WARNING: species unknown: " << it->second.species.Data() << "  aborting"<< std::endl;
0161       return false;
0162   }
0163   
0164   Int_t maxCellsPerLayer = setup->GetMaxChannelInLayerFull()+1;
0165   //=============================================================================================
0166   // Create output histos
0167   //=============================================================================================
0168   // create HG and LG histo's per channel
0169   TH2D* hspectraHGCorrvsCellID      = nullptr;
0170   TH2D* hspectraLGCorrvsCellID      = nullptr;
0171   TH2D* hspectraHGvsCellID          = nullptr;
0172   TH2D* hspectraLGvsCellID          = nullptr;
0173   TH2D* hspectraTOTvsCellID         = nullptr;
0174 
0175   if (typeRO == ReadOut::Type::Caen) {
0176     hspectraHGCorrvsCellID      = new TH2D( "hspectraHGCorr_vsCellID","ADC spectrum High Gain corrected vs CellID; cell ID; ADC_{HG} (arb. units)  ; counts ",
0177                                             setup->GetMaxCellID()+1, -0.5, setup->GetMaxCellID()+1-0.5, 4000,-200,3800);
0178     hspectraHGCorrvsCellID->SetDirectory(0);
0179     hspectraLGCorrvsCellID      = new TH2D( "hspectraLGCorr_vsCellID","ADC spectrum Low Gain corrected vs CellID; cell ID; ADC_{LG} (arb. units)  ; counts  ",
0180                                             setup->GetMaxCellID()+1, -0.5, setup->GetMaxCellID()+1-0.5, 4000,-200,3800);
0181     hspectraLGCorrvsCellID->SetDirectory(0);
0182     hspectraHGvsCellID      = new TH2D( "hspectraHG_vsCellID","ADC spectrum High Gain vs CellID; cell ID; ADC_{HG} (arb. units)   ; counts ",
0183                                             setup->GetMaxCellID()+1, -0.5, setup->GetMaxCellID()+1-0.5, 4000,0,4000);
0184     hspectraHGvsCellID->SetDirectory(0);
0185     hspectraLGvsCellID      = new TH2D( "hspectraLG_vsCellID","ADC spectrum Low Gain vs CellID; cell ID; ADC_{LG} (arb. units)  ; counts",
0186                                             setup->GetMaxCellID()+1, -0.5, setup->GetMaxCellID()+1-0.5, 4000,0,4000);
0187     hspectraLGvsCellID->SetDirectory(0);
0188   } else if (typeRO == ReadOut::Type::Hgcroc) {
0189     hspectraHGvsCellID      = new TH2D( "hspectraADCvsCellID","ADC vs CellID; cell ID; ADC (arb. units)  ; counts ",
0190                                             setup->GetMaxCellID()+1, -0.5, setup->GetMaxCellID()+1-0.5, 1128,-100,1028);
0191     hspectraHGvsCellID->SetDirectory(0);
0192     hspectraTOTvsCellID= new TH2D( "hspectraTOTvsCellID","TOT vs CellID; cell ID; TOT (arb. units)  ; counts ",
0193                                             setup->GetMaxCellID()+1, -0.5, setup->GetMaxCellID()+1-0.5, 4096,0,4096);
0194     hspectraTOTvsCellID->SetDirectory(0);
0195   }
0196   TH2D* hspectraEnergyvsCellID  = new TH2D( "hspectraEnergy_vsCellID","Energy vs CellID; cell ID; E (mip eq./tile)    ; counts",
0197                                             setup->GetMaxCellID()+1, -0.5, setup->GetMaxCellID()+1-0.5, 8000,0,250);
0198   hspectraEnergyvsCellID->SetDirectory(0);
0199 
0200   // beam species dependent binning for energy hists:
0201   TH2D* hspectraEnergyTotvsNCells     = nullptr;
0202   TH2D* hspectraEnergyTotvsNCellsMuon = nullptr;
0203   TH2D* hspectraEnergyVsLayer         = nullptr;
0204   TH2D* hspectraEnergyVsLayerMuon     = nullptr;
0205   // muons 
0206   if (species == 0 || species == 2 ){
0207     hspectraEnergyTotvsNCells       = new TH2D( "hspectraTotEnergy_vsNCells","Energy vs CellID; N_{Cells}; E_{tot} (mip eq./tile) ; counts",
0208                                                 setup->GetNActiveCells()+1, -0.5, setup->GetNActiveCells()+1-0.5, 4000,0,500);
0209     hspectraEnergyTotvsNCellsMuon   = new TH2D( "hspectraTotEnergy_vsNCellsMuon","Energy vs CellID; N_{Cells}; E_{tot} (mip eq./tile) ; counts",
0210                                                 setup->GetNActiveCells()+1, -0.5, setup->GetNActiveCells()+1-0.5, 4000,0,500);    
0211     hspectraEnergyVsLayer           = new TH2D( "hspectraTotLayerEnergy_vsLayer","Energy in layer vs Layer; Layer; E_{layer} (mip eq./tile) ; counts",
0212                                                 setup->GetNMaxLayer()+1, -0.5, setup->GetNMaxLayer()+1-0.5, 2000,0,400);
0213     hspectraEnergyVsLayerMuon       = new TH2D( "hspectraTotLayerEnergy_vsLayerMuon","Energy in layer vs Layer Muon triggers; Layer; E_{layer} (mip eq./tile) ; counts",
0214                                                 setup->GetNMaxLayer()+1, -0.5, setup->GetNMaxLayer()+1-0.5, 2000,0,400);
0215   // em-probes
0216   } else if ( species == 1 || species == 3 || species == 6 ){
0217     hspectraEnergyTotvsNCells       = new TH2D( "hspectraTotEnergy_vsNCells","Energy vs CellID; N_{Cells}; E_{tot} (mip eq./tile) ; counts",
0218                                                 setup->GetNActiveCells()+1, -0.5, setup->GetNActiveCells()+1-0.5, 8000,0,1000);
0219     hspectraEnergyTotvsNCellsMuon   = new TH2D( "hspectraTotEnergy_vsNCellsMuon","Energy vs CellID; N_{Cells}; E_{tot} (mip eq./tile) ; counts",
0220                                                 setup->GetNActiveCells()+1, -0.5, setup->GetNActiveCells()+1-0.5, 8000,0,1000);    
0221     hspectraEnergyVsLayer           = new TH2D( "hspectraTotLayerEnergy_vsLayer","Energy in layer vs Layer; Layer; E_{layer} (mip eq./tile) ; counts",
0222                                                 setup->GetNMaxLayer()+1, -0.5, setup->GetNMaxLayer()+1-0.5, 2000,0,400);
0223     hspectraEnergyVsLayerMuon       = new TH2D( "hspectraTotLayerEnergy_vsLayerMuon","Energy in layer vs Layer Muon triggers; Layer; E_{layer} (mip eq./tile) ; counts",
0224                                                 setup->GetNMaxLayer()+1, -0.5, setup->GetNMaxLayer()+1-0.5, 2000,0,400);
0225   // hadrons
0226   } else if ( species == 4 || species == 5  ){
0227     hspectraEnergyTotvsNCells       = new TH2D( "hspectraTotEnergy_vsNCells","Energy vs CellID; N_{Cells}; E_{tot} (mip eq./tile) ; counts",
0228                                                 setup->GetNActiveCells()+1, -0.5, setup->GetNActiveCells()+1-0.5, 16000,0,2000);
0229     hspectraEnergyTotvsNCellsMuon   = new TH2D( "hspectraTotEnergy_vsNCellsMuon","Energy vs CellID; N_{Cells}; E_{tot} (mip eq./tile) ; counts",
0230                                                 setup->GetNActiveCells()+1, -0.5, setup->GetNActiveCells()+1-0.5, 16000,0,2000);
0231     hspectraEnergyVsLayer           = new TH2D( "hspectraTotLayerEnergy_vsLayer","Energy in layer vs Layer; Layer; E_{layer} (mip eq./tile) ; counts",
0232                                                 setup->GetNMaxLayer()+1, -0.5, setup->GetNMaxLayer()+1-0.5, 4000,0,800);
0233     hspectraEnergyVsLayerMuon       = new TH2D( "hspectraTotLayerEnergy_vsLayerMuon","Energy in layer vs Layer Muon triggers; Layer; E_{layer} (mip eq./tile) ; counts",
0234                                                 setup->GetNMaxLayer()+1, -0.5, setup->GetNMaxLayer()+1-0.5, 4000,0,800);
0235   }
0236   
0237   hspectraEnergyTotvsNCells->SetDirectory(0);
0238   hspectraEnergyTotvsNCells->Sumw2();
0239   hspectraEnergyTotvsNCellsMuon->SetDirectory(0);
0240   hspectraEnergyTotvsNCellsMuon->Sumw2();
0241   hspectraEnergyVsLayer->SetDirectory(0);
0242   hspectraEnergyVsLayer->Sumw2();
0243   hspectraEnergyVsLayerMuon->SetDirectory(0);
0244   hspectraEnergyVsLayerMuon->Sumw2();
0245   
0246   Double_t minXPos = setup->GetMinX();
0247   Double_t maxXPos = setup->GetMaxX();
0248   Double_t minYPos = setup->GetMinY();
0249   Double_t maxYPos = setup->GetMaxY();
0250   Int_t nBinsXPos  = (Int_t)((maxXPos-minXPos)/0.1);
0251   Int_t nBinsYPos  = (Int_t)((maxXPos-minXPos)/0.1);
0252   
0253   TH2D* hAverageXVsLayer  = new TH2D( "hAverageX_vsLayer","Av. X pos in layer vs Layer; Layer; X_{pos} (cm) ; counts",
0254                                             setup->GetNMaxLayer()+1, -0.5, setup->GetNMaxLayer()+1-0.5, nBinsXPos,minXPos,maxXPos);
0255   hAverageXVsLayer->SetDirectory(0);
0256   hAverageXVsLayer->Sumw2();
0257   TH2D* hAverageXVsLayerMuon  = new TH2D( "hAverageX_vsLayerMuon","Av. X pos in layer vs Layer Muon triggers; Layer; X_{pos} (cm) ; counts",
0258                                             setup->GetNMaxLayer()+1, -0.5, setup->GetNMaxLayer()+1-0.5, nBinsXPos,minXPos,maxXPos);
0259   hAverageXVsLayerMuon->SetDirectory(0);
0260   hAverageXVsLayer->Sumw2();
0261   TH2D* hAverageYVsLayer  = new TH2D( "hAverageX_vsLayer","Av. Y pos in layer vs Layer; Layer; Y_{pos} (cm) ; counts",
0262                                             setup->GetNMaxLayer()+1, -0.5, setup->GetNMaxLayer()+1-0.5, nBinsYPos,minYPos,maxYPos);
0263   hAverageYVsLayer->SetDirectory(0);
0264   hAverageYVsLayer->Sumw2();
0265   TH2D* hAverageYVsLayerMuon  = new TH2D( "hAverageX_vsLayerMuon","Av. Y pos in layer vs Layer Muon triggers; Layer; Y_{pos} (cm) ; counts",
0266                                             setup->GetNMaxLayer()+1, -0.5, setup->GetNMaxLayer()+1-0.5, nBinsYPos,minYPos,maxYPos);
0267   hAverageYVsLayerMuon->SetDirectory(0);
0268   hAverageYVsLayerMuon->Sumw2();
0269   TH2D* hNCellsVsLayer  = new TH2D( "hnCells_vsLayer","NCells in layer vs Layer; Layer; N_{cells,layer} ; counts",
0270                                             setup->GetNMaxLayer()+1, -0.5, setup->GetNMaxLayer()+1-0.5, maxCellsPerLayer,-0.5,maxCellsPerLayer-0.5);
0271   hNCellsVsLayer->SetDirectory(0);
0272   hNCellsVsLayer->Sumw2();
0273   TH2D* hNCellsVsLayerMuon  = new TH2D( "hnCells_vsLayerMuon","NCells in layer vs Layer  Muon triggers; Layer; N_{cells,layer} ; counts",
0274                                             setup->GetNMaxLayer()+1, -0.5, setup->GetNMaxLayer()+1-0.5, maxCellsPerLayer,-0.5,maxCellsPerLayer-0.5);
0275   hNCellsVsLayerMuon->SetDirectory(0);
0276   hNCellsVsLayerMuon->Sumw2();
0277   
0278   //Create 1D Histos for Delta time
0279   TH1D* hDeltaTime = new TH1D("hDeltaTime", "Time Difference between Events; Delta Time (#mus); Counts", 2000, 0, 100000);
0280   
0281   std::map<int,TileSpectra> hSpectra;
0282   std::map<int,TileSpectra> hSpectraTrigg;
0283   std::map<int, TileSpectra>::iterator ithSpectra;
0284   std::map<int, TileSpectra>::iterator ithSpectraTrigg;
0285 
0286   RootOutputHist->mkdir("IndividualCells");
0287   RootOutputHist->mkdir("IndividualCellsTrigg");
0288   
0289   //=============================================================================================
0290   // Setup general variables for analysis
0291   //=============================================================================================
0292   std::cout << "starting to run QA"<< std::endl;
0293   // initialize calib
0294   TcalibIn->GetEntry(0);
0295   int actCh1st = 0;
0296   double averageScale = calib.GetAverageScaleHigh(actCh1st);
0297   std::cout << "average HG mip: " << averageScale << "\t active ch: "<< actCh1st<< std::endl;
0298 
0299   // get and set maximum event number
0300   int evts=TdataIn->GetEntries();
0301   if ((eventNumber > -1) && (eventNumber <= evts)){
0302     evts = eventNumber;
0303     std::cout<<"restricting number of events in QA to " << evts << std::endl;
0304   }
0305   int evtsMuon= 0;
0306   double last_time = 0;
0307   double DeltaTime = 1000000000;
0308   double minESave  = 0.;
0309   
0310   int nActiveLayers = setup->GetNActiveLayers();
0311   
0312   //=============================================================================================
0313   // Run over all events in tree
0314   //=============================================================================================  
0315   for(int i=0; i<evts; i++){
0316     TdataIn->GetEntry(i);
0317     if (i%5000 == 0 && debug > 0) std::cout << "Reading " <<  i << " / " << evts << " events" << std::endl;
0318     
0319     //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
0320     // Set up event variables
0321     //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
0322     double Etot = 0;
0323     int nCells  = 0;
0324     bool muontrigg = false;
0325     int locMuon = 0;
0326     std::map<int,Layer> layers;
0327     std::map<int, Layer>::iterator ithLayer;
0328     
0329     // Find and fill delta time
0330     double current_time = event.GetTimeStamp();
0331     if(last_time != 0){
0332       DeltaTime = current_time - last_time;
0333       if (debug == 1000){
0334         std::cerr<< "current timestamp: "<< current_time<<std::endl;
0335       }
0336     }
0337     if (DeltaTime == 0){
0338       if(debug == 1001){
0339         std::cerr<< "Run Number: " << runNr <<"      Event Number: "<< i << std::endl;
0340         std::cerr<< "Previous Timestamp (us): "<< last_time <<"      Current Timestamp: "<< current_time<<std::endl;
0341       }
0342     }
0343         //cut based on DeltaTime range
0344     if(DeltaTime < timemin || DeltaTime > timemax){
0345       //std::cout<<"event rejected:"<< i <<std::endl;
0346       last_time = current_time;
0347       continue;
0348     }
0349     hDeltaTime->Fill(DeltaTime);
0350     last_time = current_time;
0351         
0352     //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
0353     // Evaluate all cells in event
0354     //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
0355     for(int j=0; j<event.GetNTiles(); j++){
0356       if (typeRO == ReadOut::Type::Caen) {
0357         Caen* aTile=(Caen*)event.GetTile(j);
0358         double corrHG = aTile->GetADCHigh()-calib.GetPedestalMeanH(aTile->GetCellID());
0359         double corrLG = aTile->GetADCLow()-calib.GetPedestalMeanL(aTile->GetCellID());
0360         hspectraHGvsCellID->Fill(aTile->GetCellID(), aTile->GetADCHigh());
0361         hspectraLGvsCellID->Fill(aTile->GetCellID(), aTile->GetADCLow());
0362         hspectraHGCorrvsCellID->Fill(aTile->GetCellID(), corrHG);
0363         hspectraLGCorrvsCellID->Fill(aTile->GetCellID(), corrLG);
0364         if(aTile->GetE()> minESave ){ 
0365           nCells++;
0366           int currLayer = setup->GetLayer(aTile->GetCellID());
0367           ithLayer=layers.find(currLayer);
0368           if(ithLayer!=layers.end()){
0369             ithLayer->second.nCells+=1;
0370             ithLayer->second.energy+=aTile->GetE();
0371             ithLayer->second.avX+=setup->GetX(aTile->GetCellID())*aTile->GetE();
0372             ithLayer->second.avY+=setup->GetY(aTile->GetCellID())*aTile->GetE();
0373           } else {
0374             layers[currLayer]=Layer();
0375             layers[currLayer].nCells+=1;
0376             layers[currLayer].energy+=aTile->GetE();
0377             layers[currLayer].avX+=setup->GetX(aTile->GetCellID())*aTile->GetE();
0378             layers[currLayer].avY+=setup->GetY(aTile->GetCellID())*aTile->GetE();
0379           }
0380         }
0381         // read current tile
0382         ithSpectraTrigg=hSpectraTrigg.find(aTile->GetCellID());
0383         if (aTile->GetLocalTriggerBit() == 1){
0384           if(ithSpectraTrigg!=hSpectraTrigg.end()){
0385             ithSpectraTrigg->second.FillTrigger(aTile->GetLocalTriggerPrimitive());
0386             ithSpectraTrigg->second.FillSpectraCAEN(corrLG,corrHG);
0387           } else {
0388             RootOutputHist->cd("IndividualCellsTrigg");
0389             hSpectraTrigg[aTile->GetCellID()]=TileSpectra("muonTriggers",aTile->GetCellID(),calib.GetTileCalib(aTile->GetCellID()),event.GetROtype(), nSampleHGCROCInt, debug);
0390             hSpectraTrigg[aTile->GetCellID()].FillTrigger(aTile->GetLocalTriggerPrimitive());
0391             hSpectraTrigg[aTile->GetCellID()].FillSpectraCAEN(corrLG,corrHG);
0392           }
0393           locMuon++;        
0394         }      
0395         
0396         ithSpectra=hSpectra.find(aTile->GetCellID());
0397         if (ithSpectra!=hSpectra.end()){
0398           ithSpectra->second.FillSpectraCAEN(corrLG,corrHG);
0399           ithSpectra->second.FillTrigger(aTile->GetLocalTriggerPrimitive());;
0400           ithSpectra->second.FillCorrCAEN(corrLG,corrHG);
0401         } else {
0402           RootOutputHist->cd("IndividualCells");
0403           hSpectra[aTile->GetCellID()]=TileSpectra("AllTriggers",aTile->GetCellID(),calib.GetTileCalib(aTile->GetCellID()),event.GetROtype(), nSampleHGCROCInt, debug);
0404           hSpectra[aTile->GetCellID()].FillSpectraCAEN(corrLG,corrHG);;
0405           hSpectra[aTile->GetCellID()].FillTrigger(aTile->GetLocalTriggerPrimitive());;
0406           hSpectra[aTile->GetCellID()].FillCorrCAEN(corrLG,corrHG);
0407         }
0408       } else if (typeRO == ReadOut::Type::Hgcroc){
0409         Hgcroc* aTile=(Hgcroc*)event.GetTile(j);
0410         double adc = aTile->GetIntegratedADC();
0411         double tot = aTile->GetRawTOT();
0412         double toa = aTile->GetRawTOA();
0413         hspectraHGvsCellID->Fill(aTile->GetCellID(), adc);
0414         hspectraTOTvsCellID->Fill(aTile->GetCellID(), tot);
0415         if(aTile->GetE() > minESave ){ 
0416           nCells++;
0417           int currLayer = setup->GetLayer(aTile->GetCellID());
0418           ithLayer=layers.find(currLayer);
0419           if(ithLayer!=layers.end()){
0420             ithLayer->second.nCells+=1;
0421             ithLayer->second.energy+=aTile->GetE();
0422             ithLayer->second.avX+=setup->GetX(aTile->GetCellID())*aTile->GetE();
0423             ithLayer->second.avY+=setup->GetY(aTile->GetCellID())*aTile->GetE();
0424           } else {
0425             layers[currLayer]=Layer();
0426             layers[currLayer].nCells+=1;
0427             layers[currLayer].energy+=aTile->GetE();
0428             layers[currLayer].avX+=setup->GetX(aTile->GetCellID())*aTile->GetE();
0429             layers[currLayer].avY+=setup->GetY(aTile->GetCellID())*aTile->GetE();
0430           }
0431         }
0432         // read current tile
0433         ithSpectraTrigg=hSpectraTrigg.find(aTile->GetCellID());
0434         if (aTile->GetLocalTriggerBit() == 1){
0435           if(ithSpectraTrigg!=hSpectraTrigg.end()){
0436             ithSpectraTrigg->second.FillTrigger(aTile->GetLocalTriggerPrimitive());
0437             ithSpectraTrigg->second.FillHGCROC(adc,toa,tot);
0438           } else {
0439             RootOutputHist->cd("IndividualCellsTrigg");
0440             hSpectraTrigg[aTile->GetCellID()]=TileSpectra("muonTriggers",aTile->GetCellID(),calib.GetTileCalib(aTile->GetCellID()),event.GetROtype(), nSampleHGCROCInt, debug);
0441             hSpectraTrigg[aTile->GetCellID()].FillTrigger(aTile->GetLocalTriggerPrimitive());
0442             hSpectraTrigg[aTile->GetCellID()].FillHGCROC(adc,toa,tot);
0443           }
0444           locMuon++;        
0445         }      
0446         
0447         ithSpectra=hSpectra.find(aTile->GetCellID());
0448         if (ithSpectra!=hSpectra.end()){
0449           ithSpectra->second.FillTrigger(aTile->GetLocalTriggerPrimitive());;
0450           ithSpectra->second.FillHGCROC(adc,toa,tot);
0451         } else {
0452           RootOutputHist->cd("IndividualCells");
0453           hSpectra[aTile->GetCellID()]=TileSpectra("AllTriggers",aTile->GetCellID(),calib.GetTileCalib(aTile->GetCellID()),event.GetROtype(), nSampleHGCROCInt, debug);
0454           hSpectra[aTile->GetCellID()].FillTrigger(aTile->GetLocalTriggerPrimitive());;
0455           hSpectra[aTile->GetCellID()].FillHGCROC(adc,toa,tot);
0456         }
0457       }
0458     }
0459     if (nCells > 0) {
0460 
0461       int nLayerSingleCell = 0;
0462       for(ithLayer=layers.begin(); ithLayer!=layers.end(); ++ithLayer){
0463         if (ithLayer->second.nCells == 1) 
0464             nLayerSingleCell++;
0465       }
0466       double fracLayer1Cell = (double)nLayerSingleCell/(int)layers.size();
0467       // if ( fracLayer1Cell > 0.8){
0468       //   muontrigg = true;
0469       //   evtsMuon++;        
0470       // }
0471       double fracLocMuon = (double)locMuon/nCells;
0472       if (fracLocMuon > 0.6){ 
0473         muontrigg = true;
0474         evtsMuon++;
0475       }
0476       
0477       if (muontrigg && debug > 3){
0478           std::cout << "Muon triggered:\t" <<  fracLayer1Cell << "\t" << fracLocMuon << "\t" << nActiveLayers << "\t" << nCells<< std::endl;
0479       }
0480       
0481       for(int l = 0; l < setup->GetNMaxLayer()+1;l++){
0482         ithLayer=layers.find(l);
0483         if (ithLayer!=layers.end()){          
0484           double avx     = ithLayer->second.avX/ithLayer->second.energy;
0485           double avy     = ithLayer->second.avY/ithLayer->second.energy;
0486           hspectraEnergyVsLayer->Fill(l,ithLayer->second.energy);
0487           if (muontrigg){
0488             hspectraEnergyVsLayerMuon->Fill(l,ithLayer->second.energy);
0489             hAverageXVsLayerMuon->Fill(l,avx);
0490             hAverageYVsLayerMuon->Fill(l,avy);
0491             hNCellsVsLayerMuon->Fill(l,ithLayer->second.nCells);
0492           }
0493           hAverageXVsLayer->Fill(l,avx);
0494           hAverageYVsLayer->Fill(l,avy);
0495           hNCellsVsLayer->Fill(l,ithLayer->second.nCells);
0496           if (ithLayer->second.nCells == 1) 
0497             nLayerSingleCell++;
0498         } else {
0499           hspectraEnergyVsLayer->Fill(l,0.);
0500           if (muontrigg){
0501             hspectraEnergyVsLayerMuon->Fill(l,0.);
0502             hAverageXVsLayerMuon->Fill(l,-20.);
0503             hAverageYVsLayerMuon->Fill(l,-20.);
0504             hNCellsVsLayerMuon->Fill(l,0);            
0505           }
0506           hAverageXVsLayer->Fill(l,-20);
0507           hAverageYVsLayer->Fill(l,-20);
0508           hNCellsVsLayer->Fill(l,0);
0509         }
0510       }
0511 
0512       for(int j=0; j<event.GetNTiles(); j++){
0513         Caen* aTile=(Caen*)event.GetTile(j);
0514         // remove bad channels from output
0515         double energy = aTile->GetE();
0516         if(energy!=0){ 
0517           hspectraEnergyvsCellID->Fill(aTile->GetCellID(), energy);
0518           Etot=Etot+energy;
0519         } 
0520       }
0521 
0522       hspectraEnergyTotvsNCells->Fill(nCells,Etot);
0523       if(muontrigg) hspectraEnergyTotvsNCellsMuon->Fill(nCells,Etot);
0524     }
0525   }
0526   
0527   if (IsCalibSaveToFile()){
0528     TString fileCalibPrint = RootOutputName;
0529     fileCalibPrint         = fileCalibPrint.ReplaceAll(".root","_calib.txt");
0530     calib.PrintCalibToFile(fileCalibPrint);
0531   }
0532   
0533   RootInput->Close();      
0534   
0535   RootOutputHist->cd();
0536 
0537   hDeltaTime->Write();
0538 
0539   hspectraHGvsCellID->Write();
0540   if (typeRO == ReadOut::Type::Caen){
0541     hspectraLGvsCellID->Write();
0542     hspectraHGCorrvsCellID->Write();
0543     hspectraLGCorrvsCellID->Write();    
0544   } else if (typeRO == ReadOut::Type::Hgcroc){
0545     hspectraTOTvsCellID->Write();
0546   }
0547   
0548   hspectraEnergyvsCellID->Write();
0549   hspectraEnergyTotvsNCells->Write();
0550   hspectraEnergyTotvsNCellsMuon->Write();
0551   
0552   TH2D* hspectraEnergyTotvsNCells_WoMuon = (TH2D*)hspectraEnergyTotvsNCells->Clone("hspectraTotEnergy_vsNCells_woMuon");
0553   // hspectraEnergyTotvsNCells_WoMuon->Sumw2();
0554   hspectraEnergyTotvsNCells_WoMuon->Add(hspectraEnergyTotvsNCellsMuon, -1);
0555   hspectraEnergyTotvsNCells_WoMuon->Write();
0556   
0557   TH1D* hspectraEnergyTot = (TH1D*)hspectraEnergyTotvsNCells->ProjectionY();
0558   hspectraEnergyTot->SetDirectory(0);
0559   TH1D* hspectraNCells = (TH1D*)hspectraEnergyTotvsNCells->ProjectionX();
0560   hspectraNCells->SetDirectory(0);
0561   hspectraEnergyTot->Write("hTotEnergy");
0562   hspectraNCells->Write("hNCells");
0563 
0564   TH1D* hspectraEnergyTotMuon = (TH1D*)hspectraEnergyTotvsNCellsMuon->ProjectionY();
0565   hspectraEnergyTotMuon->SetDirectory(0);
0566   TH1D* hspectraNCellsMuon = (TH1D*)hspectraEnergyTotvsNCellsMuon->ProjectionX();
0567   hspectraNCellsMuon->SetDirectory(0);
0568   hspectraEnergyTotMuon->Write("hTotEnergyMuon");
0569   hspectraNCellsMuon->Write("hNCellsMuon");
0570   
0571   TH1D* hspectraEnergyTotNonMuon = (TH1D*)hspectraEnergyTotvsNCells_WoMuon->ProjectionY();
0572   hspectraEnergyTotNonMuon->SetDirectory(0);
0573   TH1D* hspectraNCellsNonMuon = (TH1D*)hspectraEnergyTotvsNCells_WoMuon->ProjectionX();
0574   hspectraNCellsNonMuon->SetDirectory(0);
0575   hspectraEnergyTotNonMuon->Write("hTotEnergyNonMuon");
0576   hspectraNCellsNonMuon->Write("hNCellsNonMuon");
0577   
0578   // position hists
0579   hAverageXVsLayer->Write();
0580   hAverageXVsLayerMuon->Write();
0581   TH2D* hAverageXVsLayer_WoMuon = (TH2D*)hAverageXVsLayer->Clone("hAverageX_vsLayer_woMuon");
0582   // hAverageXVsLayer_WoMuon->Sumw2();
0583   hAverageXVsLayer_WoMuon->Add(hAverageXVsLayerMuon, -1);
0584   hAverageXVsLayer_WoMuon->Write();
0585   
0586   hAverageYVsLayer->Write();
0587   hAverageYVsLayerMuon->Write();
0588   TH2D* hAverageYVsLayer_WoMuon = (TH2D*)hAverageYVsLayer->Clone("hAverageY_vsLayer_woMuon");
0589   // hAverageYVsLayer_WoMuon->Sumw2();
0590   hAverageYVsLayer_WoMuon->Add(hAverageYVsLayerMuon, -1);
0591   hAverageYVsLayer_WoMuon->Write();
0592   
0593   hNCellsVsLayer->Write();
0594   hNCellsVsLayerMuon->Write();
0595   TH2D* hNCellsVsLayer_WoMuon = (TH2D*)hNCellsVsLayer->Clone("hnCells_vsLayer_woMuon");
0596   // hNCellsVsLayer_WoMuon->Sumw2();
0597   hNCellsVsLayer_WoMuon->Add(hNCellsVsLayerMuon, -1);
0598   hNCellsVsLayer_WoMuon->Write();
0599   
0600   hspectraEnergyVsLayer->Write();
0601   // hspectraEnergyVsLayer->Sumw2();
0602   hspectraEnergyVsLayerMuon->Write();
0603   // hspectraEnergyVsLayerMuon->Sumw2();
0604   TH2D* hspectraEnergyVsLayer_WoMuon = (TH2D*)hspectraEnergyVsLayer->Clone("hspectraTotLayerEnergy_vsLayer_woMuon");
0605   // hspectraEnergyVsLayer_WoMuon->Sumw2();
0606   hspectraEnergyVsLayer_WoMuon->Add(hspectraEnergyVsLayerMuon, -1);
0607   hspectraEnergyVsLayer_WoMuon->Write();
0608 
0609   TH1D* histELayer_All[65];
0610   TH1D* histELayer_Muon[65];
0611   TH1D* histELayer_WoMuon[65];
0612   TH1D* histXLayer_All[65];
0613   TH1D* histXLayer_Muon[65];
0614   TH1D* histXLayer_WoMuon[65];
0615   TH1D* histYLayer_All[65];
0616   TH1D* histYLayer_Muon[65];
0617   TH1D* histYLayer_WoMuon[65];
0618   TH1D* histNCellsLayer_All[65];
0619   TH1D* histNCellsLayer_Muon[65];
0620   TH1D* histNCellsLayer_WoMuon[65];
0621   Float_t maxYLAll    = 0;
0622   Float_t maxYLMuon   = 0;
0623   Float_t maxYLWOMuon = 0;
0624   Float_t maxXLAll    = 0;
0625   Float_t maxXLMuon   = 0;
0626   Float_t maxXLWOMuon = 0;
0627   std::map<int,Layer> layersMeanAll;
0628   std::map<int, Layer>::iterator ithLayerMeanAll;
0629   std::map<int,Layer> layersMeanMuon;
0630   std::map<int, Layer>::iterator ithLayerMeanMuon;
0631   std::map<int,Layer> layersMeanWOMuon;
0632   std::map<int, Layer>::iterator ithLayerMeanWOMuon;
0633   
0634   TGraphErrors* graphMeanE_Layer        = new TGraphErrors();
0635   TGraphErrors* graphMeanE_Layer_muon   = new TGraphErrors();
0636   TGraphErrors* graphMeanE_Layer_woMuon = new TGraphErrors();
0637   
0638   for(int l = 0; l < setup->GetNMaxLayer()+1;l++){  
0639     
0640     bool layerOn = true;
0641     
0642     histNCellsLayer_All[l]     = (TH1D*)hNCellsVsLayer->ProjectionY(Form("histNCellsLayer_AllTrigg_Layer_%02d",l),l+1,l+1);
0643     histNCellsLayer_All[l]->Write();
0644     histNCellsLayer_Muon[l]    = (TH1D*)hNCellsVsLayerMuon->ProjectionY(Form("histNCellsLayer_MuonTrigg_Layer_%02d",l),l+1,l+1);
0645     histNCellsLayer_Muon[l]->Write();
0646     histNCellsLayer_WoMuon[l]  = (TH1D*)hNCellsVsLayer_WoMuon->ProjectionY(Form("histNCellsLayer_WOMuonTrigg_Layer_%02d",l),l+1,l+1);
0647     histNCellsLayer_WoMuon[l]->Write();
0648 
0649     
0650     if (histNCellsLayer_All[l]->GetMean() < 0.001) layerOn =false;
0651     
0652     histELayer_All[l]     = (TH1D*)hspectraEnergyVsLayer->ProjectionY(Form("histELayer_AllTrigg_Layer_%02d",l),l+1,l+1);
0653     // histELayer_All[l]->Sumw2();
0654     histELayer_All[l]->Write();
0655     histELayer_Muon[l]    = (TH1D*)hspectraEnergyVsLayerMuon->ProjectionY(Form("histELayer_MuonTrigg_Layer_%02d",l),l+1,l+1);
0656     // histELayer_Muon[l]->Sumw2();
0657     histELayer_Muon[l]->Write();
0658     histELayer_WoMuon[l]  = (TH1D*)hspectraEnergyVsLayer_WoMuon->ProjectionY(Form("histELayer_WOMuonTrigg_Layer_%02d",l),l+1,l+1);
0659     // histELayer_WoMuon[l]->Sumw2();
0660     histELayer_WoMuon[l]->Write();
0661     
0662     if (layerOn){
0663       if (maxYLAll < histELayer_All[l]->GetMaximum())maxYLAll =  histELayer_All[l]->GetMaximum();
0664       if (maxXLAll < FindLastBinXAboveMin(histELayer_All[l],2)) maxXLAll = FindLastBinXAboveMin(histELayer_All[l],2);
0665       if (maxYLMuon < histELayer_Muon[l]->GetMaximum() )maxYLMuon =  histELayer_Muon[l]->GetMaximum();
0666       if (maxXLMuon < FindLastBinXAboveMin(histELayer_Muon[l],2) ) maxXLMuon = FindLastBinXAboveMin(histELayer_Muon[l],2);
0667       if (maxYLWOMuon < histELayer_WoMuon[l]->GetMaximum() )maxYLWOMuon =  histELayer_WoMuon[l]->GetMaximum();
0668       if (maxXLWOMuon < FindLastBinXAboveMin(histELayer_WoMuon[l],2) ) maxXLWOMuon = FindLastBinXAboveMin(histELayer_WoMuon[l],2);
0669     }
0670 
0671     histXLayer_All[l]     = (TH1D*)hAverageXVsLayer->ProjectionY(Form("histXLayer_AllTrigg_Layer_%02d",l),l+1,l+1);
0672     histXLayer_All[l]->Write();
0673     histXLayer_Muon[l]    = (TH1D*)hAverageXVsLayerMuon->ProjectionY(Form("histXLayer_MuonTrigg_Layer_%02d",l),l+1,l+1);
0674     histXLayer_Muon[l]->Write();
0675     histXLayer_WoMuon[l]  = (TH1D*)hAverageXVsLayer_WoMuon->ProjectionY(Form("histXLayer_WOMuonTrigg_Layer_%02d",l),l+1,l+1);
0676     histXLayer_WoMuon[l]->Write();
0677 
0678     histYLayer_All[l]     = (TH1D*)hAverageYVsLayer->ProjectionY(Form("histYLayer_AllTrigg_Layer_%02d",l),l+1,l+1);
0679     histYLayer_All[l]->Write();
0680     histYLayer_Muon[l]    = (TH1D*)hAverageYVsLayerMuon->ProjectionY(Form("histYLayer_MuonTrigg_Layer_%02d",l),l+1,l+1);
0681     histYLayer_Muon[l]->Write();
0682     histYLayer_WoMuon[l]  = (TH1D*)hAverageYVsLayer_WoMuon->ProjectionY(Form("histYLayer_WOMuonTrigg_Layer_%02d",l),l+1,l+1);
0683     histYLayer_WoMuon[l]->Write();
0684 
0685     layersMeanAll[l]=Layer();
0686     layersMeanAll[l].energy+=histELayer_All[l]->GetMean();
0687     layersMeanAll[l].avX+=histXLayer_All[l]->GetMean();
0688     layersMeanAll[l].avY+=histYLayer_All[l]->GetMean();
0689     layersMeanAll[l].nCells+=histNCellsLayer_All[l]->GetMean();
0690     
0691     layersMeanMuon[l]=Layer();
0692     layersMeanMuon[l].energy+=histELayer_Muon[l]->GetMean();
0693     layersMeanMuon[l].avX+=histXLayer_Muon[l]->GetMean();
0694     layersMeanMuon[l].avY+=histYLayer_Muon[l]->GetMean();
0695     layersMeanMuon[l].nCells+=histNCellsLayer_Muon[l]->GetMean();
0696     
0697     layersMeanWOMuon[l]=Layer();
0698     layersMeanWOMuon[l].energy+=histELayer_WoMuon[l]->GetMean();
0699     layersMeanWOMuon[l].avX+=histXLayer_WoMuon[l]->GetMean();
0700     layersMeanWOMuon[l].avY+=histYLayer_WoMuon[l]->GetMean();
0701     layersMeanWOMuon[l].nCells+=histNCellsLayer_WoMuon[l]->GetMean();
0702     
0703     graphMeanE_Layer->SetPoint(graphMeanE_Layer->GetN(), l,histELayer_All[l]->GetMean());
0704     graphMeanE_Layer->SetPointError(graphMeanE_Layer->GetN()-1, 0,histELayer_All[l]->GetMeanError());
0705     // graphMeanE_Layer->SetPointError(graphMeanE_Layer->GetN()-1, 0,histELayer_All[l]->GetRMS());
0706     graphMeanE_Layer_muon->SetPoint(graphMeanE_Layer_muon->GetN(),l,histELayer_Muon[l]->GetMean());
0707     graphMeanE_Layer_muon->SetPointError(graphMeanE_Layer_muon->GetN()-1, 0,histELayer_Muon[l]->GetMeanError());
0708     // graphMeanE_Layer_muon->SetPointError(graphMeanE_Layer_muon->GetN()-1, 0,histELayer_Muon[l]->GetRMS());
0709     graphMeanE_Layer_woMuon->SetPoint(graphMeanE_Layer_woMuon->GetN(),l,histELayer_WoMuon[l]->GetMean());
0710     graphMeanE_Layer_woMuon->SetPointError(graphMeanE_Layer_woMuon->GetN()-1, 0,histELayer_WoMuon[l]->GetMeanError());
0711     // graphMeanE_Layer_woMuon->SetPointError(graphMeanE_Layer_woMuon->GetN()-1, 0,histELayer_WoMuon[l]->GetRMS());
0712   }
0713   
0714   std::cout<< "average per layer - All" << std::endl;
0715   graphMeanE_Layer->Write("graphMeanE_Layer");
0716   std::cout<< "average per layer - no Muon" << std::endl;
0717   graphMeanE_Layer_woMuon->Write("graphMeanE_Layer_woMuon");
0718   std::cout<< "average per layer - Muon" << std::endl;
0719   graphMeanE_Layer_muon->Write("graphMeanE_Layer_Muon");
0720   
0721   //**********************************************************************
0722   //********************* Plotting ***************************************
0723   //**********************************************************************  
0724   TString outputDirPlots = GetPlotOutputDir();
0725   gSystem->Exec("mkdir -p "+outputDirPlots);
0726   
0727   
0728   
0729   //**********************************************************************
0730   // Create canvases for channel overview plotting
0731   //**********************************************************************
0732   Double_t textSizeRel = 0.035;
0733   StyleSettingsBasics("pdf");
0734   SetPlotStyle();
0735   
0736   TCanvas* canvas2DCorr = new TCanvas("canvasCorrPlots","",0,0,1450,1300);  // gives the page size
0737   DefaultCanvasSettings( canvas2DCorr, 0.08, 0.13, 0.045, 0.07);
0738   canvas2DCorr->SetLogz(1);
0739   TCanvas* canvas2DCorrWOLine = new TCanvas("canvasCorrPlotsWoLine","",0,0,1450,1300);  // gives the page size
0740   DefaultCanvasSettings( canvas2DCorrWOLine, 0.08, 0.13, 0.01, 0.07);
0741   canvas2DCorrWOLine->SetLogz(1);
0742   
0743   TCanvas* canvasDeltaTime = new TCanvas("canvasDeltaTime","",0,0,1450,1300);  // gives the page size
0744   DefaultCanvasSettings( canvasDeltaTime, 0.08, 0.07, 0.01, 0.07);
0745   canvasDeltaTime->SetLogy(1);
0746   if(DeltaTimePlot>0){
0747     PlotSimple1D( canvasDeltaTime, hDeltaTime, -10000, timemax, textSizeRel, Form("%s/deltaTime.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1);
0748   } 
0749 
0750   PlotSimple2D( canvas2DCorr, hspectraHGvsCellID, -10000, -10000, textSizeRel, Form("%s/HG.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", true);
0751   if (typeRO == ReadOut::Type::Caen){
0752     PlotSimple2D( canvas2DCorr, hspectraLGvsCellID, -10000, -10000, textSizeRel, Form("%s/LG.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", true);
0753     PlotSimple2D( canvas2DCorr, hspectraHGCorrvsCellID, -10000, -10000, textSizeRel, Form("%s/HGCorr.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", true);
0754     PlotSimple2D( canvas2DCorr, hspectraLGCorrvsCellID, -10000, -10000, textSizeRel, Form("%s/LGCorr.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", true);
0755   } else if (typeRO == ReadOut::Type::Hgcroc){
0756     PlotSimple2D( canvas2DCorr, hspectraTOTvsCellID, -10000, -10000, textSizeRel, Form("%s/TOT.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", true);    
0757   }
0758   PlotSimple2D( canvas2DCorr, hspectraEnergyvsCellID, -10000, -10000, textSizeRel, Form("%s/EnergyVsCellID.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", true);
0759   PlotSimple2D( canvas2DCorr, hspectraEnergyTotvsNCells, -10000, -10000, textSizeRel, Form("%s/EnergyTotalVsNCells.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", true, Form("evts = %d",evts));
0760   PlotSimple2D( canvas2DCorr, hspectraEnergyTotvsNCellsMuon, -10000, -10000, textSizeRel, Form("%s/EnergyTotalVsNCells_MuonTrigg.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", true, Form("Muon evts = %d",evtsMuon));
0761   PlotSimple2D( canvas2DCorr, hspectraEnergyTotvsNCells_WoMuon, -10000, -10000, textSizeRel, Form("%s/EnergyTotalVsNCells_WoMuonTrigg.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", true, Form("Non Muon evts = %d",evts-evtsMuon));
0762 
0763   PlotSimple2D( canvas2DCorr, hspectraEnergyVsLayer, -10000, -10000, textSizeRel, Form("%s/EnergyVsLayer.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", true);
0764   PlotSimple2D( canvas2DCorr, hspectraEnergyVsLayerMuon, -10000, -10000, textSizeRel, Form("%s/EnergyVsLayer_MuonTrigg.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", true, "Muon triggers");
0765   PlotSimple2D( canvas2DCorr, hspectraEnergyVsLayer_WoMuon, -10000, -10000, textSizeRel, Form("%s/EnergyVsLayer_WOMuonTrigg.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", true, "Non Muon triggers");
0766   PlotSimple2D( canvas2DCorr, hAverageXVsLayer, -10000, -10000, textSizeRel, Form("%s/XPosVsLayer.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", true);
0767   PlotSimple2D( canvas2DCorr, hAverageXVsLayerMuon, -10000, -10000, textSizeRel, Form("%s/XPosVsLayer_MuonTrigg.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", true, "Muon triggers");
0768   PlotSimple2D( canvas2DCorr, hAverageXVsLayer_WoMuon, -10000, -10000, textSizeRel, Form("%s/XPosVsLayer_WOMuonTrigg.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", true, "Non Muon triggers");
0769   PlotSimple2D( canvas2DCorr, hAverageYVsLayer, -10000, -10000, textSizeRel, Form("%s/YPosVsLayer.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", true);
0770   PlotSimple2D( canvas2DCorr, hAverageYVsLayerMuon, -10000, -10000, textSizeRel, Form("%s/YPosVsLayer_MuonTrigg.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", true, "Muon triggers");
0771   PlotSimple2D( canvas2DCorr, hAverageYVsLayer_WoMuon, -10000, -10000, textSizeRel, Form("%s/YPosVsLayer_WOMuonTrigg.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", true, "Non Muon triggers");
0772   PlotSimple2D( canvas2DCorr, hNCellsVsLayer, -10000, -10000, textSizeRel, Form("%s/NcellsLayerVsLayer.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", true);
0773   PlotSimple2D( canvas2DCorr, hNCellsVsLayerMuon, -10000, -10000, textSizeRel, Form("%s/NcellsLayerVsLayer_MuonTrigg.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", true, "Muon triggers");
0774   PlotSimple2D( canvas2DCorr, hNCellsVsLayer_WoMuon, -10000, -10000, textSizeRel, Form("%s/NcellsLayerVsLayer_WOMuonTrigg.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", true, "Non Muon triggers");
0775   
0776   double maxEProp = 400.;
0777   if (TMath::Abs(beamE -5.0) < 0.01) {
0778       maxEProp = 300;
0779       std::cout << "reset max for beam E" << beamE << "\t" << maxEProp << std::endl;
0780   }
0781   if (species == 0 || species == 2 ){
0782     Plot2DWithGraph( canvas2DCorrWOLine, hspectraEnergyVsLayerMuon, nullptr, maxEProp, -10000,  textSizeRel, Form("%s/EnergyVsLayer_Propaganda.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", false, "");
0783     Plot2DWithGraph( canvas2DCorrWOLine, hspectraEnergyVsLayerMuon, graphMeanE_Layer_muon, maxEProp, -10000,  textSizeRel, Form("%s/EnergyVsLayer_PropagandaWGraph.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", false, "");
0784   } else {
0785     Plot2DWithGraph( canvas2DCorrWOLine, hspectraEnergyVsLayer_WoMuon,  nullptr, maxEProp, -10000, textSizeRel, Form("%s/EnergyVsLayer_Propaganda.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", false, "");
0786     Plot2DWithGraph( canvas2DCorrWOLine, hspectraEnergyVsLayer_WoMuon,  graphMeanE_Layer_woMuon, maxEProp, -10000, textSizeRel, Form("%s/EnergyVsLayer_PropagandaWGraph.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", false, "");
0787   }
0788   canvas2DCorrWOLine->SetLogy();
0789   if (species == 0 || species == 2 ){
0790     Plot2DWithGraph( canvas2DCorrWOLine, hspectraEnergyVsLayerMuon, nullptr, 400, -10000,  textSizeRel, Form("%s/EnergyVsLayer_Propaganda_LogY.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", false, "");
0791     Plot2DWithGraph( canvas2DCorrWOLine, hspectraEnergyVsLayerMuon, graphMeanE_Layer_muon, 400, -10000,  textSizeRel, Form("%s/EnergyVsLayer_PropagandaWGraph_LogY.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", false, "");
0792   } else {
0793     Plot2DWithGraph( canvas2DCorrWOLine, hspectraEnergyVsLayer_WoMuon,  nullptr, 400, -10000, textSizeRel, Form("%s/EnergyVsLayer_Propaganda_LogY.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", false, "");
0794     Plot2DWithGraph( canvas2DCorrWOLine, hspectraEnergyVsLayer_WoMuon,  graphMeanE_Layer_woMuon, 400, -10000, textSizeRel, Form("%s/EnergyVsLayer_PropagandaWGraph_LogY.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, kFALSE, "colz", false, "");
0795   }
0796   canvas2DCorrWOLine->SetLogy(kFALSE);
0797   
0798 
0799   TCanvas* canvas1DSimple = new TCanvas("canvas1DSimple","",0,0,1450,1300);  // gives the page size
0800   DefaultCanvasSettings( canvas1DSimple, 0.08, 0.01, 0.03, 0.07);
0801   hspectraEnergyTot->Rebin(4);
0802   hspectraEnergyTot->Scale(1./evts);
0803   hspectraEnergyTot->GetYaxis()->SetTitle("counts/event");
0804   
0805   TF1* fitGaussETot = new TF1("fitEtot","gaus",hspectraEnergyTot->GetMean()-1.5*hspectraEnergyTot->GetRMS(),hspectraEnergyTot->GetMean()+1.5*hspectraEnergyTot->GetRMS());
0806   hspectraEnergyTot->Fit(fitGaussETot,"QRMEN0S");
0807   std::cout<< "All trigg: "<<fitGaussETot->GetParameter(1) << "\t" << fitGaussETot->GetParameter(2) << std::endl;
0808   
0809   Double_t maxEPlot =  FindLastBinXAboveMin(hspectraEnergyTot,1./evts);
0810   std::cout << maxEPlot << std::endl;
0811   
0812   PlotSimple1D(canvas1DSimple, hspectraEnergyTot, -10000, maxEPlot, textSizeRel, Form("%s/EnergyTot.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, Form("#LT E_{Tot} #GT = %.1f #pm %.1f (mip/tile eq.)", hspectraEnergyTot->GetMean(), hspectraEnergyTot->GetRMS() ));
0813 
0814   
0815   hspectraEnergyTotMuon->Rebin(4);
0816   hspectraEnergyTotMuon->Scale(1./evts);
0817   hspectraEnergyTotNonMuon->Rebin(4);
0818   hspectraEnergyTotNonMuon->Scale(1./evts);
0819 
0820   TF1* fitGaussETotNonMuon = new TF1("fitEtot","gaus",hspectraEnergyTotNonMuon->GetMean()-1.5*hspectraEnergyTotNonMuon->GetRMS(),hspectraEnergyTotNonMuon->GetMean()+1.5*hspectraEnergyTotNonMuon->GetRMS());
0821   hspectraEnergyTotNonMuon->Fit(fitGaussETotNonMuon,"QRMEN0S");
0822   std::cout<< "Non muon trigg: "<<fitGaussETotNonMuon->GetParameter(1) << "\t" << fitGaussETotNonMuon->GetParameter(2) << std::endl;
0823   hspectraEnergyTotNonMuon->Fit(fitGaussETotNonMuon,"QRMEN0S","",fitGaussETotNonMuon->GetParameter(1)-1.*fitGaussETotNonMuon->GetParameter(2), fitGaussETotNonMuon->GetParameter(1)+1.*fitGaussETotNonMuon->GetParameter(2));
0824   std::cout<< "Non muon trigg refit: "<<fitGaussETotNonMuon->GetParameter(1) << "\t" << fitGaussETotNonMuon->GetParameter(2) << std::endl;
0825   
0826   
0827   PlotContamination1D(canvas1DSimple, hspectraEnergyTot,hspectraEnergyTotMuon, hspectraEnergyTotNonMuon, -10000, maxEPlot, textSizeRel, Form("%s/EnergyTotSplit.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, Form("#LT E_{Tot,non muon} #GT = %.1f#pm %.1f (mip/tile eq.)", hspectraEnergyTotNonMuon->GetMean(), hspectraEnergyTotNonMuon->GetRMS() ));
0828 
0829   PlotSimpleWithFit1D(canvas1DSimple, hspectraEnergyTotNonMuon, fitGaussETotNonMuon , -10000, maxEPlot, textSizeRel, Form("%s/EnergyTotNonMuonWithFit.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, Form("#LT E_{Tot,non muon} #GT = %.1f #pm %.1f (mip/tile eq.)", fitGaussETotNonMuon->GetParameter(1), fitGaussETotNonMuon->GetParameter(2) ));
0830 
0831   hspectraNCells->Scale(1./evts);
0832   hspectraNCells->GetYaxis()->SetTitle("counts/event");
0833   PlotSimple1D(canvas1DSimple, hspectraNCells, -10000, -10000, textSizeRel, Form("%s/NCells.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, Form("#LT N_{Cells} #GT = %.1f",hspectraNCells->GetMean() ));
0834   hspectraNCellsMuon->Scale(1./evts);
0835   hspectraNCellsNonMuon->Scale(1./evts);  
0836   PlotContamination1D( canvas1DSimple, hspectraNCells, hspectraNCellsMuon, hspectraNCellsNonMuon, -10000, -10000, textSizeRel, Form("%s/NCellsSplit.%s", outputDirPlots.Data(), plotSuffix.Data()), it->second, 1, Form("#LT N_{Cells,non muon} #GT = %.1f",hspectraNCellsNonMuon->GetMean() ));
0837   
0838   // canvas1DSimple->SetLogy();
0839   PlotLayerOverlay(canvas1DSimple, histELayer_All, evts*10, maxXLAll ,GetAverageLayer(layersMeanAll), GetMaxLayer(layersMeanAll), textSizeRel, Form("%s/ELayerOverlay_AllTrigg.%s", outputDirPlots.Data(), plotSuffix.Data()),it->second, 1, "All triggers");
0840   PlotLayerOverlay(canvas1DSimple, histELayer_Muon, evtsMuon*10, maxXLMuon, GetAverageLayer(layersMeanMuon), GetMaxLayer(layersMeanMuon),textSizeRel, Form("%s/ELayerOverlay_MuonTrigg.%s", outputDirPlots.Data(), plotSuffix.Data()),it->second, 1, "Muon triggers");
0841   PlotLayerOverlay(canvas1DSimple, histELayer_WoMuon, (evts-evtsMuon)*10, maxXLAll, GetAverageLayer(layersMeanWOMuon), GetMaxLayer(layersMeanWOMuon),textSizeRel, Form("%s/ELayerOverlay_NonMuonTrigg.%s", outputDirPlots.Data(), plotSuffix.Data()),it->second, 1, "Non muon triggers");
0842   
0843   if (species == 0 || species == 2 ){
0844     PlotLayerOverlay(canvas1DSimple, histELayer_Muon, evtsMuon*10, maxXLMuon, GetAverageLayer(layersMeanMuon), GetMaxLayer(layersMeanMuon),textSizeRel, Form("%s/ELayerOverlay_Propaganda.%s", outputDirPlots.Data(), plotSuffix.Data()),it->second, 1, "", false);
0845   } else {
0846     PlotLayerOverlay(canvas1DSimple, histELayer_WoMuon, (evts-evtsMuon)*10, maxXLAll, GetAverageLayer(layersMeanWOMuon), GetMaxLayer(layersMeanWOMuon),textSizeRel, Form("%s/ELayerOverlay_Propaganda.%s", outputDirPlots.Data(), plotSuffix.Data()),it->second, 1, "", false);
0847   }
0848 
0849   
0850   PlotLayerOverlay(canvas1DSimple, histXLayer_All, evts*100, maxXPos-2. ,GetAverageLayer(layersMeanAll), GetMaxLayer(layersMeanAll), textSizeRel, Form("%s/XPosLayerOverlay_AllTrigg.%s", outputDirPlots.Data(), plotSuffix.Data()),it->second, 1, "All triggers");
0851   PlotLayerOverlay(canvas1DSimple, histXLayer_Muon, evtsMuon*100, maxXPos-2., GetAverageLayer(layersMeanMuon), GetMaxLayer(layersMeanMuon),textSizeRel, Form("%s/XPosLayerOverlay_MuonTrigg.%s", outputDirPlots.Data(), plotSuffix.Data()),it->second, 1, "Muon triggers");
0852   PlotLayerOverlay(canvas1DSimple, histXLayer_WoMuon, (evts-evtsMuon)*100, maxXPos-2., GetAverageLayer(layersMeanWOMuon), GetMaxLayer(layersMeanWOMuon),textSizeRel, Form("%s/XPosLayerOverlay_NonMuonTrigg.%s", outputDirPlots.Data(), plotSuffix.Data()),it->second, 1, "Non muon triggers");
0853 
0854   PlotLayerOverlay(canvas1DSimple, histYLayer_All, evts*100, maxYPos-2. ,GetAverageLayer(layersMeanAll), GetMaxLayer(layersMeanAll), textSizeRel, Form("%s/YPosLayerOverlay_AllTrigg.%s", outputDirPlots.Data(), plotSuffix.Data()),it->second, 1, "All triggers");
0855   PlotLayerOverlay(canvas1DSimple, histYLayer_Muon, evtsMuon*100, maxYPos-2., GetAverageLayer(layersMeanMuon), GetMaxLayer(layersMeanMuon),textSizeRel, Form("%s/YPosLayerOverlay_MuonTrigg.%s", outputDirPlots.Data(), plotSuffix.Data()),it->second, 1, "Muon triggers");
0856   PlotLayerOverlay(canvas1DSimple, histYLayer_WoMuon, (evts-evtsMuon)*100, maxYPos-2., GetAverageLayer(layersMeanWOMuon), GetMaxLayer(layersMeanWOMuon),textSizeRel, Form("%s/YPosLayerOverlay_NonMuonTrigg.%s", outputDirPlots.Data(), plotSuffix.Data()),it->second, 1, "Non muon triggers");
0857 
0858   PlotLayerOverlay(canvas1DSimple, histNCellsLayer_All, evts*100, maxCellsPerLayer-0.5 ,GetAverageLayer(layersMeanAll), GetMaxLayer(layersMeanAll), textSizeRel, Form("%s/NCellsLayerOverlay_AllTrigg.%s", outputDirPlots.Data(), plotSuffix.Data()),it->second, 1, "All triggers");
0859   PlotLayerOverlay(canvas1DSimple, histNCellsLayer_Muon, evtsMuon*100, maxCellsPerLayer-0.5, GetAverageLayer(layersMeanMuon), GetMaxLayer(layersMeanMuon),textSizeRel, Form("%s/NCellsLayerOverlay_MuonTrigg.%s", outputDirPlots.Data(), plotSuffix.Data()),it->second, 1, "Muon triggers");
0860   PlotLayerOverlay(canvas1DSimple, histNCellsLayer_WoMuon, (evts-evtsMuon)*100, maxCellsPerLayer-0.5, GetAverageLayer(layersMeanWOMuon), GetMaxLayer(layersMeanWOMuon),textSizeRel, Form("%s/NCellsLayerOverlay_NonMuonTrigg.%s", outputDirPlots.Data(), plotSuffix.Data()),it->second, 1, "Non muon triggers");
0861 
0862   if (ExtPlot > 0){
0863     gSystem->Exec("mkdir -p "+outputDirPlots+"/detailed");    
0864     calib.PrintGlobalInfo();  
0865     Double_t maxHG = ReturnMipPlotRangeDepVov(calib.GetVov(),true, ReadOut::Type::Caen);
0866     Double_t maxLG = 3800;
0867     
0868     //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
0869     // Single layer plotting
0870     //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
0871     DetConf::Type detConf = setup->GetDetectorConfig();
0872     MultiCanvas panelPlot(detConf, "QAData");
0873     bool init1D = panelPlot.Initialize(1);
0874     MultiCanvas panelPlot2D(detConf, "QAData2D");
0875     bool init2D = panelPlot2D.Initialize(2);
0876 
0877     std::cout << "plotting single layers" << std::endl;
0878     panelPlot.PlotMipWithFits( hSpectra, hSpectraTrigg, 1, -100, 3800, 1.2, 
0879                                 Form("%s/detailed/LocTriggerMip_HG_%.0f_%.0f" ,outputDirPlots.Data(),timemin, timemax), plotSuffix.Data(), it->second, &calib);
0880     panelPlot.PlotMipWithFits( hSpectra, hSpectraTrigg, 1, -100, maxHG, 1.2, 
0881                                 Form("%s/detailed/LocTriggerMip_Zoomed_HG_%.0f_%.0f" ,outputDirPlots.Data(),timemin, timemax), plotSuffix.Data(), it->second, &calib);    
0882     panelPlot.PlotTriggerPrim( hSpectra, averageScale, 0.8, 2., 0, 6000, 1.2,
0883                               Form("%s/detailed/All_TriggPrimitive_%.0f_%.0f" ,outputDirPlots.Data(),timemin, timemax), plotSuffix.Data(), it->second, &calib);
0884     if (ExtPlot > 1){
0885       panelPlot.PlotMipWithFits( hSpectra, hSpectraTrigg, 0, -20, maxLG, 1.2, 
0886                                 Form("%s/detailed/LocTriggerMip_LG_%.0f_%.0f" ,outputDirPlots.Data(),timemin, timemax),plotSuffix.Data(), it->second, &calib);
0887       panelPlot.PlotTriggerPrim( hSpectraTrigg, averageScale, 0.8, 2., 0, maxHG*2, 1.2,
0888                               Form("%s/detailed/LocalMuon_TriggPrimitive_%.0f_%.0f" ,outputDirPlots.Data(),timemin, timemax), plotSuffix.Data(), it->second, &calib);
0889       panelPlot2D.PlotCorrWithFits( hSpectra, 0, -20, 800, 0., 3400,
0890                                     Form("%s/detailed/LGHG_Corr_%.0f_%.0f" ,outputDirPlots.Data(),timemin, timemax), plotSuffix.Data(), it->second, &calib);
0891     }
0892     std::cout << "done plotting" << std::endl;
0893   }
0894   RootOutputHist->Close();
0895   return true;
0896 } // end QAData()
0897 
0898 
0899 //***********************************************************************************************
0900 //*********************** Simple QA routine ***************************************************
0901 //***********************************************************************************************
0902 bool DataAnalysis::SimpleQAData(void){
0903   std::cout<<"QA data"<<std::endl;
0904 
0905   std::map<int,RunInfo> ri=readRunInfosFromFile(RunListInputName.Data(),debug,0);
0906   TdataIn->GetEntry(0);
0907   Int_t runNr = event.GetRunNumber();
0908   // Get run info object
0909   std::map<int,RunInfo>::iterator it=ri.find(runNr);
0910 
0911   int species = -1;
0912   species = GetSpeciesIntFromRunInfo(it->second);
0913   float beamE = it->second.energy;
0914   std::cout << "Beam energy:" << beamE << std::endl;
0915   if (species == -1){
0916       std::cout << "WARNING: species unknown: " << it->second.species.Data() << "  aborting"<< std::endl;
0917       return false;
0918   }
0919   
0920   // create HG and LG histo's per channel
0921   TH1D* hDeltaTime = new TH1D("hDeltaTime", "Time Difference between Events; Delta Time (#mus); Counts", 2000, 0, 100000);
0922   TH2D* hspectraHGCorrvsCellID      = new TH2D( "hspectraHGCorr_vsCellID","ADC spectrum High Gain corrected vs CellID; cell ID; ADC_{HG} (arb. units)  ; counts ",
0923                                             setup->GetMaxCellID()+1, -0.5, setup->GetMaxCellID()+1-0.5, 4000,-200,3800);
0924   hspectraHGCorrvsCellID->SetDirectory(0);
0925   TH2D* hspectraLGCorrvsCellID      = new TH2D( "hspectraLGCorr_vsCellID","ADC spectrum Low Gain corrected vs CellID; cell ID; ADC_{LG} (arb. units)  ; counts  ",
0926                                             setup->GetMaxCellID()+1, -0.5, setup->GetMaxCellID()+1-0.5, 4000,-200,3800);
0927   hspectraLGCorrvsCellID->SetDirectory(0);
0928   TH2D* hspectraEnergyvsCellID  = new TH2D( "hspectraEnergy_vsCellID","Energy vs CellID; cell ID; E (mip eq./tile)    ; counts",
0929                                             setup->GetMaxCellID()+1, -0.5, setup->GetMaxCellID()+1-0.5, 8000,0,250);
0930   hspectraEnergyvsCellID->SetDirectory(0);
0931   
0932   std::map<int,TileSpectra> hSpectra;
0933   std::map<int, TileSpectra>::iterator ithSpectra;
0934 
0935   RootOutputHist->mkdir("IndividualCells");
0936   
0937   std::cout << "starting to run QA"<< std::endl;
0938   TcalibIn->GetEntry(0);
0939   int actCh1st = 0;
0940   double averageScale = calib.GetAverageScaleHigh(actCh1st);
0941   std::cout << "average HG mip: " << averageScale << "\t active ch: "<< actCh1st<< std::endl;
0942   
0943   int evts=TdataIn->GetEntries();
0944   double last_time = 0;
0945   double DeltaTime = 1000000000;
0946   
0947   
0948   if(percentmax!=100 || percentmin!=0){
0949     std::vector<double> deltat;
0950     deltat.reserve(evts);
0951     // Percentile calculations
0952     for (int i=0; i<evts; i++){
0953       TdataIn->GetEntry(i);
0954       
0955       double current_time = event.GetTimeStamp();
0956       if(last_time != 0){
0957         DeltaTime = current_time - last_time;
0958       }
0959       if (DeltaTime != 1000000000){
0960         deltat.push_back(DeltaTime);
0961       }
0962       last_time = current_time;
0963     }
0964     std::sort(deltat.begin(), deltat.end());
0965     int eventmax = static_cast<int>(std::round(percentmax*evts/100.0));
0966     int eventmin = static_cast<int>(std::round(percentmin*evts/100.0));
0967     std::cout << "eventmax: " << eventmax <<std::endl;
0968     std::cout <<"eventmin: " << eventmin << std::endl;
0969     if (percentmax != 100) timemax = deltat[eventmax-2];
0970     if (percentmin != 0) timemin = deltat[eventmin-1];
0971     std::cout << "timemax: " << timemax <<std::endl;
0972     std::cout <<"timemin: " << timemin << std::endl;
0973   }
0974 
0975   int rejected = 0;
0976   std::cerr<<"Run Number: " << runNr<< std::endl;
0977   std::cerr<< "Number of total evts: " << evts <<std::endl;
0978   // Event loop
0979   for(int i=0; i<evts; i++){
0980     TdataIn->GetEntry(i);
0981     
0982     // Find and fill delta time
0983     double current_time = event.GetTimeStamp();
0984     if(last_time != 0){
0985       DeltaTime = current_time - last_time;
0986       if (debug == 1000){
0987         std::cerr<< "current timestamp: "<< current_time<<std::endl;
0988       }
0989     }
0990     if (DeltaTime == 0){
0991       if(debug == 1001){
0992         std::cerr<< "Run Number: " << runNr <<"      Event Number: "<< i << std::endl;
0993         std::cerr<< "Previous Timestamp (us): "<< last_time <<"      Current Timestamp: "<< current_time<<std::endl;
0994       }
0995     }
0996         //cut based on DeltaTime range
0997     if(DeltaTime < timemin || DeltaTime > timemax){
0998       rejected++;
0999       last_time = current_time;
1000       continue;
1001     }
1002     hDeltaTime->Fill(DeltaTime);
1003     last_time = current_time;
1004     
1005     if (i%5000 == 0 && debug > 0) std::cout << "Reading " <<  i << " / " << evts << " events" << std::endl;
1006     for(int j=0; j<event.GetNTiles(); j++){
1007       Caen* aTile=(Caen*)event.GetTile(j);
1008       double corrHG = aTile->GetADCHigh()-calib.GetPedestalMeanH(aTile->GetCellID());
1009       double corrLG = aTile->GetADCLow()-calib.GetPedestalMeanL(aTile->GetCellID());
1010       hspectraHGCorrvsCellID->Fill(aTile->GetCellID(), corrHG);
1011       hspectraLGCorrvsCellID->Fill(aTile->GetCellID(), corrLG);
1012       if(aTile->GetE()!=0 ){ 
1013         hspectraEnergyvsCellID->Fill(aTile->GetCellID(), aTile->GetE());
1014       }      
1015       ithSpectra=hSpectra.find(aTile->GetCellID());
1016       if (ithSpectra!=hSpectra.end()){
1017         ithSpectra->second.FillSpectraCAEN(corrLG,corrHG);
1018         ithSpectra->second.FillTrigger(aTile->GetLocalTriggerPrimitive());;
1019         ithSpectra->second.FillCorrCAEN(corrLG,corrHG);
1020       } else {
1021         RootOutputHist->cd("IndividualCells");
1022         hSpectra[aTile->GetCellID()]=TileSpectra("AllTriggers",aTile->GetCellID(),calib.GetTileCalib(aTile->GetCellID()),event.GetROtype(),  nSampleHGCROCInt, debug);
1023         hSpectra[aTile->GetCellID()].FillSpectraCAEN(corrLG,corrHG);;
1024         hSpectra[aTile->GetCellID()].FillTrigger(aTile->GetLocalTriggerPrimitive());;
1025         hSpectra[aTile->GetCellID()].FillCorrCAEN(corrLG,corrHG);
1026       }
1027     }
1028   }
1029   if (debug == 1000){
1030     std::cerr<< "Number rejected: " << rejected <<std::endl;
1031     std::cerr<< "Number accepted: " << evts - rejected << std::endl;
1032   }
1033   if (IsCalibSaveToFile()){
1034     TString fileCalibPrint = RootOutputName;
1035     fileCalibPrint         = fileCalibPrint.ReplaceAll(".root","_calib.txt");
1036     calib.PrintCalibToFile(fileCalibPrint);
1037   }
1038   
1039   RootInput->Close();      
1040   
1041   RootOutputHist->cd();
1042 
1043   hspectraHGCorrvsCellID->Write();
1044   hspectraLGCorrvsCellID->Write();
1045   hspectraEnergyvsCellID->Write();
1046 
1047   hDeltaTime->Write();
1048 
1049   RootOutputHist->cd("IndividualCells");
1050   for(ithSpectra=hSpectra.begin(); ithSpectra!=hSpectra.end(); ++ithSpectra){
1051     ithSpectra->second.Write(false);
1052   }
1053   
1054   //**********************************************************************
1055   //********************* Plotting ***************************************
1056   //**********************************************************************  
1057   TString outputDirPlots = GetPlotOutputDir();
1058   gSystem->Exec("mkdir -p "+outputDirPlots);
1059   
1060   //**********************************************************************
1061   // Create canvases for channel overview plotting
1062   //**********************************************************************
1063   Double_t textSizeRel = 0.035;
1064   StyleSettingsBasics("pdf");
1065   SetPlotStyle();
1066   
1067   TCanvas* canvasDeltaTime = new TCanvas("canvasDeltaTime","",0,0,1450,1300);  // gives the page size
1068   DefaultCanvasSettings( canvasDeltaTime, 0.08, 0.07, 0.01, 0.07);
1069   canvasDeltaTime->SetLogy(1);
1070   
1071   if(DeltaTimePlot>0){
1072     PlotSimple1D( canvasDeltaTime, hDeltaTime, -10000, timemax, textSizeRel, Form("%s/deltaTime_%.0f_%.0f.%s", outputDirPlots.Data(),timemin, timemax, plotSuffix.Data()), it->second, 1);
1073   } 
1074   
1075   TCanvas* canvas2DCorr = new TCanvas("canvasCorrPlots","",0,0,1450,1300);  // gives the page size
1076   DefaultCanvasSettings( canvas2DCorr, 0.08, 0.13, 0.045, 0.07);
1077   canvas2DCorr->SetLogz(1);
1078   TCanvas* canvas2DCorrWOLine = new TCanvas("canvasCorrPlotsWoLine","",0,0,1450,1300);  // gives the page size
1079   DefaultCanvasSettings( canvas2DCorrWOLine, 0.08, 0.13, 0.01, 0.07);
1080   canvas2DCorrWOLine->SetLogz(1);
1081   PlotSimple2D( canvas2DCorr, hspectraHGCorrvsCellID, -10000, -10000, textSizeRel, Form("%s/HGCorr_%.0f_%.0f.%s", outputDirPlots.Data(),timemin, timemax, plotSuffix.Data()), it->second, 1, kFALSE, "colz", true);
1082   PlotSimple2D( canvas2DCorr, hspectraLGCorrvsCellID, -10000, -10000, textSizeRel, Form("%s/LGCorr_%.0f_%.0f.%s", outputDirPlots.Data(),timemin, timemax, plotSuffix.Data()), it->second, 1, kFALSE, "colz", true);
1083   PlotSimple2D( canvas2DCorr, hspectraEnergyvsCellID, -10000, -10000, textSizeRel, Form("%s/EnergyVsCellID_%.0f_%.0f.%s", outputDirPlots.Data(),timemin, timemax, plotSuffix.Data()), it->second, 1, kFALSE, "colz", true);
1084     
1085   if (ExtPlot > 0){
1086     gSystem->Exec("mkdir -p "+outputDirPlots+"/detailed");
1087     //***********************************************************************************************************
1088     //********************************* 8 Panel overview plot  **************************************************
1089     //***********************************************************************************************************
1090     //*****************************************************************
1091       // Test beam geometry (beam coming from viewer)
1092       //===========================================================
1093       //||    8 (4)    ||    7 (5)   ||    6 (6)   ||    5 (7)   ||  row 0
1094       //===========================================================
1095       //||    1 (0)    ||    2 (1)   ||    3 (2)   ||    4 (3)   ||  row 1
1096       //===========================================================
1097       //    col 0     col 1       col 2     col  3
1098       // rebuild pad geom in similar way (numbering -1)
1099     //*****************************************************************
1100     TCanvas* canvas8Panel;
1101     TPad* pad8Panel[8];
1102     Double_t topRCornerX[8];
1103     Double_t topRCornerY[8];
1104     Int_t textSizePixel = 30;
1105     Double_t relSize8P[8];
1106     CreateCanvasAndPadsFor8PannelTBPlot(canvas8Panel, pad8Panel,  topRCornerX, topRCornerY, relSize8P, textSizePixel);
1107   
1108     TCanvas* canvas8PanelProf;
1109     TPad* pad8PanelProf[8];
1110     Double_t topRCornerXProf[8];
1111     Double_t topRCornerYProf[8];
1112     Double_t relSize8PProf[8];
1113     CreateCanvasAndPadsFor8PannelTBPlot(canvas8PanelProf, pad8PanelProf,  topRCornerXProf, topRCornerYProf, relSize8PProf, textSizePixel, 0.045, "Prof", false);
1114 
1115     
1116     calib.PrintGlobalInfo();  
1117     Double_t maxHG = ReturnMipPlotRangeDepVov(calib.GetVov(),true, ReadOut::Type::Caen);
1118     Double_t maxLG = 3800;
1119     
1120         //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
1121     // Single layer plotting
1122     //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++  
1123     DetConf::Type detConf = setup->GetDetectorConfig();
1124     MultiCanvas panelPlot(detConf, "QAData");
1125     bool init1D = panelPlot.Initialize(1);
1126     MultiCanvas panelPlot2D(detConf, "QAData2D");
1127     bool init2D = panelPlot2D.Initialize(2);
1128 
1129     std::cout << "plotting single layers" << std::endl;
1130     panelPlot.PlotNoiseWithFits( hSpectra,  0, 0, maxHG, 1.2, 
1131                                 Form("%s/detailed/Spectra_HG_%.0f_%.0f" ,outputDirPlots.Data(),timemin, timemax), plotSuffix.Data(), it->second, &calib);
1132     panelPlot.PlotNoiseWithFits( hSpectra,  1, 0, maxLG, 1.2, 
1133                                 Form("%s/detailed/Spectra_LG_%.0f_%.0f" ,outputDirPlots.Data(),timemin, timemax), plotSuffix.Data(), it->second, &calib);
1134     panelPlot2D.PlotCorrWithFits( hSpectra, 0, -20, 340, 0., 3800,
1135                                   Form("%s/detailed/LGHG_Corr_%.0f_%.0f" ,outputDirPlots.Data(),timemin, timemax), plotSuffix.Data(), it->second, &calib);
1136     std::cout << "done plotting" << std::endl;
1137   }
1138   RootOutputHist->Close();
1139   return true;
1140 }
1141 
1142 //***********************************************************************************************
1143 //*********************** Create output files ***************************************************
1144 //***********************************************************************************************
1145 bool DataAnalysis::CreateOutputRootFile(void){
1146   if(Overwrite){
1147     RootOutput=new TFile(RootOutputName.Data(),"RECREATE");
1148   } else{
1149     RootOutput = new TFile(RootOutputName.Data(),"CREATE");
1150   }
1151   if(RootOutput->IsZombie()){
1152     std::cout<<"Error opening '"<<RootOutput<<"'no reachable path? Exist without force mode to overwrite?..."<<std::endl;
1153     return false;
1154   }
1155   return true;
1156 }
1157 
1158 //***********************************************************************************************
1159 //*********************** Create output files ***************************************************
1160 //***********************************************************************************************
1161 bool DataAnalysis::CreateOutputRootFileHist(void){
1162   if(Overwrite){
1163     RootOutputHist=new TFile(RootOutputNameHist.Data(),"RECREATE");
1164   } else{
1165     RootOutputHist = new TFile(RootOutputNameHist.Data(),"CREATE");
1166   }
1167   if(RootOutputHist->IsZombie()){
1168     std::cout<<"Error opening '"<<RootOutputHist<<"'no reachable path? Exist without force mode to overwrite?..."<<std::endl;
1169     return false;
1170   }
1171   return true;
1172 }