Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:15:48

0001 #include <TROOT.h>
0002 #include <TString.h>
0003 #include <TObject.h>
0004 #include <TObjString.h>
0005 #include <TSystem.h>
0006 #include <TChain.h>
0007 #include <TMath.h>
0008 #include <TVector3.h>
0009 #include <iostream>
0010 #include <fstream>
0011 #include <TParticlePDG.h>
0012 #include <TDatabasePDG.h>
0013 #include <TRandom3.h>
0014 
0015 #include <TCanvas.h>
0016 #include <TPad.h>
0017 #include <TH1.h>
0018 #include <TH1D.h>
0019 #include <TH1F.h>
0020 #include <TH2.h>
0021 #include <TH3.h>
0022 #include <TFile.h>
0023 #include <TH2D.h>
0024 #include <TH2F.h>
0025 #include <TString.h>
0026 #include <TDatime.h>
0027 #include <TF1.h>
0028 #include <TF2.h>
0029 #include <THStack.h>
0030 #include <TGraph.h>
0031 #include <TStyle.h>
0032 #include <TGraphAsymmErrors.h>
0033 #include <TLine.h>
0034 #include <TLatex.h>
0035 #include <TArrow.h>
0036 #include <TGraphErrors.h>
0037 #include <TGaxis.h>
0038 #include <TLegend.h>
0039 #include <TFrame.h>
0040 #include <TLorentzVector.h>
0041 
0042 #include "PlottingHeader.h"
0043 #include "FittingHeader.h"
0044 
0045 const int gMaxChannels = 64;
0046 const int gMaxBoard    = 1;
0047 const int gMaxLayers   = 16;
0048 // Define tree variables
0049 Long64_t gTrID;
0050 Double_t gTRtimeStamp;
0051 Long64_t* gBoard          = new Long64_t[gMaxChannels];
0052 Long64_t* gChannel        = new Long64_t[gMaxChannels];
0053 Long64_t* gLG             = new Long64_t[gMaxChannels];
0054 Long64_t* gHG             = new Long64_t[gMaxChannels];
0055 
0056 // Definition of color/marker arrays for plotting
0057 Color_t colorLayer[16]    = { kRed-7, kBlue+1, kCyan-3, kMagenta+1, kPink-1, kViolet-9, kOrange, kGreen+1,  
0058                               kRed+1, kBlue-9, kCyan+1, kMagenta-7, kOrange+7, kSpring+5, kPink+5, kViolet+8 };
0059 Color_t colorReadBoard[8] = { kRed+1, kBlue+1, kCyan+1, kMagenta+1, kOrange, kGreen+1, kPink+5, kViolet-9};
0060 Style_t markerLayer[16]     = { 20, 25, 33, 28, 29, 37, 41, 46,
0061                                 24, 21, 27, 34, 30, 39, 40, 47};
0062 Style_t markerReadBoard[8]  = { 20, 21, 33, 34, 29, 39, 40, 46};                             
0063 
0064 #include "makeSimplePlotsFromJanusTree.h"
0065 
0066 
0067 
0068 //__________________________________________________________________________________________________________
0069 //_____________________MAIN function !!! ___________________________________________________________________
0070 //__________________________________________________________________________________________________________
0071 void makeSimplePlotsFromJanusTree( TString fileName     = "", 
0072                                    TString outputDir    = "ProcessedData/",
0073                                    Int_t runnumber      = -1,
0074                                    ULong_t minNEvent    = 0,
0075                                    ULong_t maxNEvent    = 0,
0076                                    Int_t verbosity      = 0,
0077                                    Int_t isTBdata       = 0,
0078                                    TString mappingFile  = "",
0079                                    Bool_t bDetPlot      = kTRUE,
0080                                    TString runListFileName = "configs/SPS_RunNumbers.txt"
0081                                   ){
0082                                
0083     //*******************************************************
0084     //********************Running modes:*********************
0085     // isTBdata = 0       // local testing and parsing
0086     // isTBdata = 1       // PS test beam 2023
0087     // isTBdata = 2       // local test setups in small stack
0088   
0089     StyleSettingsThesis("pdf");
0090     SetPlotStyle();
0091     Double_t textSizeRel = 0.04;
0092     
0093       // make output directory
0094     TString dateForOutput = ReturnDateStr();
0095     if (runnumber > -1)
0096       outputDir = Form("%s/Run%05d",outputDir.Data(), runnumber );
0097     TString outputDirPlots = Form("%s/Plots",outputDir.Data());
0098     TString outputDirPlotsDet = Form("%s/Plots/Detailed",outputDir.Data());
0099     gSystem->Exec("mkdir -p "+outputDir);
0100     gSystem->Exec("mkdir -p "+outputDirPlots);
0101     gSystem->Exec("mkdir -p "+outputDirPlotsDet);
0102 
0103     // load tree
0104     TChain *const tt_event = new TChain("tree");
0105     if (fileName.EndsWith(".root")) {                     // are we loading a single root tree?
0106         std::cout << "loading a single root file" << std::endl;
0107         tt_event->AddFile(fileName);
0108         TFile testFile(fileName.Data());
0109         if (testFile.IsZombie()){
0110           std::cout << Form("The file %s is not a root file or doesn't exit, please fix the file path", fileName.Data()) << std::endl;
0111           return;
0112         }
0113 
0114     } else {
0115         std::cout << "please try again this isn't a root file" << std::endl;
0116     } 
0117     if(!tt_event){ std::cout << "tree not found... returning!"<< std::endl; return;}
0118 
0119     // load all branches (see header)
0120     SetBranchAddressesTree(tt_event);
0121 
0122     
0123     Long64_t nEntriesTree                 = tt_event->GetEntries();
0124     std::cout << "Number of events in tree: " << nEntriesTree << std::endl;
0125     if(maxNEvent>0 && maxNEvent<(ULong_t)nEntriesTree){
0126       nEntriesTree = maxNEvent;
0127       std::cout << "Will only analyze first " << maxNEvent << " events in the tree..." << std::endl;
0128     }
0129 
0130     // ********************************************************************************************************
0131     // read run list and corresponding settings
0132     // ********************************************************************************************************
0133     std::vector<runInfo> runs = readRunInfosFromFile(runListFileName, 0 );
0134     Int_t indexCRun = findCurrentRun(runs, runnumber);
0135     runInfo currentRunInfo;
0136     if (indexCRun < 0){
0137       std::cout << "run not in current list of runs, provided" << std::endl;
0138       return;
0139     } else {
0140       std::cout << "Run "<< runnumber << "\t found at index " << indexCRun << std::endl;
0141       currentRunInfo = GetRunInfoObject(runs,indexCRun);
0142       std::cout << "Run " << currentRunInfo.runNr << "\t species: " << currentRunInfo.species << "\t energy: "  << currentRunInfo.energy << "\t Vop: " << currentRunInfo.vop << std::endl;
0143     }
0144     
0145     // ********************************************************************************************************
0146     // Read mapping
0147     //  * association of CAEN board channels with actuall layers in the detectors
0148     //  * CAEN board Nr. 0-2
0149     //  * CAEN board channels 0-64
0150     //  * Layers during TB (0-14)
0151     //  * Readout board channel (1-8)
0152     //  * Readout board column (0-3)
0153     //  * Readout board row (0-1)
0154     //  * Assembly Nr TB (0-19)
0155     // ********************************************************************************************************
0156     Int_t mapping[128][4]                     = {{-1}};      // linear mapping 
0157     Int_t backwardMapping[9][gMaxLayers]      = {{-1}};     // backward associations channels
0158     Int_t backwardMappingBoard[9][gMaxLayers] = {{-1}};     // backward associations board
0159     Bool_t lActive[gMaxLayers]                = {0};        // boolean to check whether layer is active
0160     Bool_t lActiveCh[9]                       = {0};        // boolean to check whether channel on board is active
0161     for (Int_t l = 0; l < gMaxLayers; l++){
0162       for (Int_t c = 0; c < 9 ; c++){
0163         backwardMapping[c][l]       = -1;
0164         backwardMappingBoard[c][l]  = -1;
0165         lActiveCh[c]                = kFALSE;
0166       }
0167       lActive[l] = kFALSE;
0168     }
0169     for (Int_t i = 0; i < 128; i++){
0170       for (Int_t k = 0; k < 4; k++){
0171         mapping[i][k] = -1;
0172       }
0173     }  
0174     
0175     Int_t nChmapped = 0;
0176     cout << "INFO: You have chosen the given the following config file: " << mappingFile.Data() << endl;
0177     ifstream fileMapping;
0178     fileMapping.open(mappingFile,ios_base::in);
0179     if (!fileMapping) {
0180         cout << "ERROR: settings " << mappingFile.Data() << " not found!" << endl;
0181         return;
0182     }
0183 
0184     // read settings from file
0185     for( TString tempLine; tempLine.ReadLine(fileMapping, kTRUE); ) {
0186         // check if line should be considered
0187         if (tempLine.BeginsWith("%") || tempLine.BeginsWith("#")){
0188             continue;
0189         }
0190         if (verbosity > 0) cout << tempLine.Data() << endl;
0191 
0192         // Separate the string according to tabulators
0193         TObjArray *tempArr  = tempLine.Tokenize("\t");
0194         if(tempArr->GetEntries()<1){
0195             cout << "nothing to be done" << endl;
0196             delete tempArr;
0197             continue;
0198         } else if (tempArr->GetEntries() == 1 ){
0199             // Separate the string according to space
0200             tempArr       = tempLine.Tokenize(" ");
0201             if(tempArr->GetEntries()<1){
0202                 cout << "nothing to be done" << endl;
0203                 delete tempArr;
0204                 continue;
0205             } else if (tempArr->GetEntries() == 1 ) {
0206                 cout << ((TString)((TObjString*)tempArr->At(0))->GetString()).Data() << " has not be reset, no value given!" << endl;
0207                 delete tempArr;
0208                 continue;
0209             }
0210         }
0211         Int_t chCAEN    = ((TString)((TObjString*)tempArr->At(0))->GetString()).Atoi()*64 + ((TString)((TObjString*)tempArr->At(1))->GetString()).Atoi();
0212         Int_t layerMod  = ((TString)((TObjString*)tempArr->At(2))->GetString()).Atoi();
0213         Int_t assemblyMod  = ((TString)((TObjString*)tempArr->At(3))->GetString()).Atoi();
0214         Int_t chBoard   = ((TString)((TObjString*)tempArr->At(4))->GetString()).Atoi();
0215         Int_t rowBoard  = ((TString)((TObjString*)tempArr->At(5))->GetString()).Atoi();
0216         Int_t colBoard  = ((TString)((TObjString*)tempArr->At(6))->GetString()).Atoi();
0217         
0218         if (verbosity > 0) std::cout << "-->" << chCAEN << "\t" << layerMod << "\t"<< chBoard << "\t" << rowBoard << "\t" << colBoard << std::endl;
0219         mapping[chCAEN][0]    = layerMod; 
0220         mapping[chCAEN][1]    = chBoard; 
0221         mapping[chCAEN][2]    = rowBoard; 
0222         mapping[chCAEN][3]    = colBoard; 
0223         nChmapped++;
0224         backwardMapping[chBoard][layerMod] = chCAEN;
0225         backwardMappingBoard[chBoard][layerMod] = ((TString)((TObjString*)tempArr->At(0))->GetString()).Atoi();
0226         if (verbosity > 0) std::cout << backwardMapping[chBoard][layerMod] << std::endl;
0227         delete tempArr;
0228     }
0229     
0230     for (Int_t ch = 0; ch< 64; ch++){
0231       if (verbosity > 0) std::cout  << "channel: " << ch << " location plane: " << mapping[ch][0] << "\t tile: " << mapping[ch][1] << std::endl;
0232     }
0233   
0234   
0235     Int_t nActiveLayers   = 0;
0236     Int_t minActiveLayer  = -1;
0237     Int_t maxActiveLayer  = -1;
0238     Int_t nActiveRBCh     = 0;
0239     Int_t minActiveRBCh   = -1;
0240     Int_t maxActiveRBCh   = -1;
0241 //     if (isTBdata > 0){
0242       std::cout << "===================================================================" << std::endl;
0243       std::cout << "================ Visualization of layers ==========================" << std::endl;
0244       std::cout << "===================================================================" << std::endl;
0245       for (Int_t l = 0; l < gMaxLayers; l++){
0246           for (Int_t c = 1; c < 9; c++){
0247               std::cout << backwardMapping[c][l] << "\t || \t" ;
0248               if (lActive[l] == kFALSE ){
0249                   if ( backwardMapping[c][l] != -1 ) lActive[l] = kTRUE;
0250               }
0251               if (lActiveCh[c] == kFALSE ){
0252                 if ( backwardMapping[c][l] != -1 ) lActiveCh[c] = kTRUE;
0253               }
0254           }
0255           std::cout << std::endl;
0256       }
0257       std::cout << "===================================================================" << std::endl;
0258       
0259       // count total number of active layers according to mapping file and determine maximum layer
0260       for (Int_t l = 0; l<gMaxLayers; l++){
0261         if (lActive[l]){
0262           nActiveLayers++; 
0263           maxActiveLayer = l;
0264           if (minActiveLayer == -1) minActiveLayer = l;
0265         }
0266       }
0267       // count total number of active channel according to mapping file and determine maximum RBchannel
0268       for (Int_t c = 0; c<9; c++){
0269         if (lActiveCh[c]){
0270           nActiveRBCh++; 
0271           maxActiveRBCh = c;
0272           if (minActiveRBCh == -1) minActiveRBCh = c;
0273         }
0274       }
0275       std::cout << "There are " << nActiveLayers << " active layers. The lowest layer is " << minActiveLayer << "  and the highest layer is " << maxActiveLayer << std::endl;
0276       std::cout << "There are " << nActiveRBCh << " active read-outboard channels. The lowest channel is " << minActiveRBCh << "  and the highest channel is " << maxActiveRBCh << std::endl;
0277 //     }
0278     
0279     // ********************************************************************************************************
0280     // RAW data monitoring histos per CAEN board and channel
0281     // ********************************************************************************************************    
0282     TH1D* histLG[gMaxBoard][gMaxChannels];                      // low gain all triggers
0283     TH1D* histHG[gMaxBoard][gMaxChannels];                      // high gain all triggers
0284     TH2D* hist_T_HG[gMaxBoard][gMaxChannels];                   // time dependent high gain all triggers
0285     TH2D* histLGHG[gMaxBoard][gMaxChannels];                    // LG - HG correlation
0286     TH2D* histHGLG[gMaxBoard][gMaxChannels];                    // HG - LG correlation
0287     TH1D* histLGTrig[gMaxBoard][gMaxChannels];                  // low gain straight line triggers
0288     TH1D* histHGTrig[gMaxBoard][gMaxChannels];                  // high gain straight line triggers
0289     TH1D* histLGNoise[gMaxBoard][gMaxChannels];                 // low gain !(straight line triggers)
0290     TH1D* histHGNoise[gMaxBoard][gMaxChannels];                 // high gain !(straight line triggers)
0291     TH1D* histLGTriggNoise[gMaxBoard][gMaxChannels];            // low gain noise triggers
0292     TH1D* histHGTriggNoise[gMaxBoard][gMaxChannels];            // high gain noise triggers
0293     
0294     //***************************************************
0295     // mapped channels - layer & read-out board channels
0296     //***************************************************
0297     // channel counting in layer starts with 1, easier acces make array go to 8
0298     TH1D* histLG_mapped[gMaxLayers][9];                         // low gain all triggers
0299     TH1D* histHG_mapped[gMaxLayers][9];                         // high gain all triggers
0300     TH1D* histLGTrig_mapped[gMaxLayers][9];                     // low gain straight line triggers
0301     TH1D* histHGTrig_mapped[gMaxLayers][9];                     // high gain straight line triggers
0302     TH1D* histLGTriggNoise_mapped[gMaxLayers][9];               // low gain noise triggers
0303     TH1D* histHGTriggNoise_mapped[gMaxLayers][9];               // high gain noise triggers
0304     
0305     //***************************************************
0306     // map of triggers above threshold
0307     //***************************************************
0308     TH3D* hist3DMap = new TH3D("h_map_z_x_y", "; layer; col; row", 14, -0.5, 13.5, 4, -0.5, 3.5, 2, -0.5, 1.5);
0309     TH2D* hist2DMap = new TH2D("h_map_z_channel", "; channel; layer", 8, 0.5, 8.5, 14, -0.5, 13.5);
0310     TH1D* hist1DMap = new TH1D("h_map_channel", "; channel", 8, 0.5, 8.5);
0311     
0312     //***************************************************
0313     TH1D* histNChAboveNoise   = new TH1D("h_NchannelAboveNoise", "; N_{channels}; counts", (gMaxBoard*gMaxChannels+1), -0.5, (gMaxBoard*gMaxChannels-0.5));
0314     TH1D* histNTrig           = new TH1D("h_NTriggStraigthLine", "; Channel_{board}; counts", 8, 0.5, 8.5);
0315     TH1D* histNTrigNoise      = new TH1D("h_NTriggNoise", "; Channel_{board}; counts", 8, 0.5, 8.5);
0316     
0317     //***************************************************
0318     // Init histograms
0319     //***************************************************
0320     for (Int_t j = 0; j < gMaxBoard; j++){
0321       for (Int_t i = 0; i < gMaxChannels; i++){
0322         if (isTBdata == 1)
0323           hist_T_HG[j][i]    = new TH2D(Form("h_T_HG_B%d_C%02d",j,i),"; t (min); HG (adc); counts",1000,0,120,4201,-200,4001);
0324         else
0325           hist_T_HG[j][i]    = new TH2D(Form("h_T_HG_B%d_C%02d",j,i),"; t (min); HG (adc); counts",1000,0,1000,4201,-200,4001);
0326         histHG[j][i]    = new TH1D(Form("h_HG_B%d_C%02d",j,i),"; HG (adc); counts",4201,-200,4001);
0327         histLG[j][i]    = new TH1D(Form("h_LG_B%d_C%02d",j,i),"; LG (adc); counts",4201,-200,4001);
0328         histLGHG[j][i]  = new TH2D(Form("h_LGHG_B%d_C%02d",j,i),"; LG (adc); HG (adc)",4200/5,-200,4001,4200/5,-200,4001);
0329         histHGLG[j][i]  = new TH2D(Form("h_HGLG_B%d_C%02d",j,i),"; LG (adc); HG (adc)",4200/5,-200,4001,4200/5,-200,4001);
0330         if (isTBdata > 0){
0331           histLGTrig[j][i]        = new TH1D(Form("h_LGTrigg_B%d_C%02d",j,i),"coincidence trigg. front & back ; LG (adc); counts",4201,-200,4001);   // triggered 
0332           histHGTrig[j][i]        = new TH1D(Form("h_HGTrigg_B%d_C%02d",j,i),"coincidence trigg. front & back ; HG (adc); counts",4201,-200,4001);
0333           histLGNoise[j][i]       = new TH1D(Form("h_LGNoise_B%d_C%02d",j,i),"!(coincidence trigg. front & back) ; LG (adc); counts",4201,-200,4001);
0334           histHGNoise[j][i]       = new TH1D(Form("h_HGNoise_B%d_C%02d",j,i),"!(coincidence trigg. front & back) ; HG (adc); counts",4201,-200,4001);
0335           histLGTriggNoise[j][i]  = new TH1D(Form("h_LGTriggeredNoise_B%d_C%02d",j,i),"veto front & back ; LG (adc); counts",4201,-200,4001);
0336           histHGTriggNoise[j][i]  = new TH1D(Form("h_HGTriggeredNoise_B%d_C%02d",j,i),"veto front & back ; HG (adc); counts",4201,-200,4001);
0337         } else {
0338           histLGTrig[j][i]        = nullptr;
0339           histHGTrig[j][i]        = nullptr;
0340           histLGNoise[j][i]       = nullptr;
0341           histHGNoise[j][i]       = nullptr;
0342           histLGTriggNoise[j][i]  = nullptr;
0343           histHGTriggNoise[j][i]  = nullptr;
0344           
0345         }
0346       }
0347     }
0348     for (Int_t l = 0; l < gMaxLayers; l++){
0349       for (Int_t c = 0; c < 9; c++){
0350         histLG_mapped[l][c]             = nullptr;
0351         histHG_mapped[l][c]             = nullptr; 
0352         histLGTrig_mapped[l][c]         = nullptr;
0353         histHGTrig_mapped[l][c]         = nullptr;
0354         histLGTriggNoise_mapped[l][c]   = nullptr;
0355         histHGTriggNoise_mapped[l][c]   = nullptr;
0356       }
0357     }
0358     
0359     // ********************************************************************************************************
0360     // Setup of global variables for parsing tree
0361     // ********************************************************************************************************
0362     Long64_t adcThreshold = 100;                    // minimum ADC count for triggers
0363     Double_t scaledThr        = adcThreshold*1.5;   // scaled ADC trigger threshold
0364     Double_t tstapMin         = 0;                  // min time stamp after start of trigger (#mus)
0365     Double_t tstapMax         = 0;                  // current max time stamp after start of trigger (#mus)
0366     
0367     Long64_t nEventsProcessed = 0;              // running counter
0368     // use this if you wanna start at a specific event for debug
0369     Long64_t startEvent = 0;
0370     if (minNEvent > 0) startEvent = minNEvent;
0371     
0372     // ********************************************************************************************************
0373     // First loop over full tree to obtain RAW histograms
0374     // ********************************************************************************************************
0375     for (Long64_t i=startEvent; i<nEntriesTree;i++) {
0376         // load current event
0377         tt_event->GetEntry(i);
0378         if (i == startEvent) tstapMin = gTRtimeStamp;
0379         tstapMax = gTRtimeStamp;
0380         Double_t tCurr= (tstapMax-tstapMin)/1e6/60; // elapsed time in min
0381         nEventsProcessed++;
0382 
0383         Int_t nChNoNoise = 0;             // number of channels above noise level
0384         // processing progress info
0385         if(i>0 && nEntriesTree>100 && i%(nEntriesTree/(20))==0) std::cout << "//processed " << 100*(i)/nEntriesTree << "%"  << std::endl;
0386         if(verbosity>1){
0387           std::cout << "***********************************************************************************************************" << std::endl;
0388           std::cout << "event " << i << std::endl;
0389           std::cout << "***********************************************************************************************************" << std::endl;
0390         }
0391         // temporary mapping of channels
0392         Int_t signal[2][64][3] = {{{0}}}; // [board][channel][HG, LG, trigg HG]
0393         // readEvent
0394         if (verbosity > 1)std::cout << "------------- Event -------------------------" << std::endl;
0395         if (verbosity > 1)std::cout << gTrID << "\t" << gTRtimeStamp << std::endl;
0396         for(Int_t ch=0; ch<gMaxChannels && ch < nChmapped ; ch++){   
0397           if (mapping[gChannel[ch]][0] == -1 && isTBdata > 0) continue;
0398           signal[gBoard[ch]][gChannel[ch]][0] = gHG[ch];
0399           signal[gBoard[ch]][gChannel[ch]][1] = gLG[ch];
0400           if (gHG[ch] > scaledThr)  // fill potential trigger signals
0401             signal[gBoard[ch]][gChannel[ch]][2] = gHG[ch]-scaledThr;
0402           
0403           // filling hists
0404           histHG[gBoard[ch]][gChannel[ch]]->Fill(gHG[ch]); 
0405           hist_T_HG[gBoard[ch]][gChannel[ch]]->Fill(tCurr,gHG[ch]); 
0406           histLG[gBoard[ch]][gChannel[ch]]->Fill(gLG[ch]); 
0407           histLGHG[gBoard[ch]][gChannel[ch]]->Fill(gLG[ch], gHG[ch]); 
0408           histHGLG[gBoard[ch]][gChannel[ch]]->Fill(gHG[ch], gLG[ch]); 
0409           if (verbosity > 1)std::cout << "--> board: "<< gBoard[ch] << "\t ch:"<< gChannel[ch] << "\t LG:" << gLG[ch] << "\t HG:" << gHG[ch] << std::endl;
0410           
0411           if (gHG[ch] > adcThreshold){
0412             nChNoNoise++;
0413             if (verbosity > 1) std::cout << "not noise" << std::endl;
0414             if (isTBdata > 0){
0415               if (mapping[gChannel[ch]][0] == -1) continue;
0416               hist3DMap->Fill(mapping[gChannel[ch]][0],mapping[gChannel[ch]][3],mapping[gChannel[ch]][2]);
0417               hist2DMap->Fill(mapping[gChannel[ch]][1],mapping[gChannel[ch]][0]);
0418               hist1DMap->Fill(mapping[gChannel[ch]][1]);
0419             }
0420           }
0421         }
0422         // fill channels above noise
0423         histNChAboveNoise->Fill(nChNoNoise);
0424         
0425         // *****************************************************************
0426         // check for conincidence between first and last channel 
0427         // above noise or pure noise triggers
0428         // *****************************************************************
0429         if (isTBdata == 1  || isTBdata == 2){
0430         // only for first CAEN board!!!!!
0431           for (Int_t chCAEN = 0; chCAEN < 64; chCAEN++){            
0432             if (mapping[chCAEN][0] == -1 ) continue;
0433             Int_t chBoard = mapping[chCAEN][1];
0434             Int_t nNeigh  = 0;
0435             Int_t* neighborCh = new Int_t[5];
0436             GetNeighborWithinBoard(chBoard, nNeigh, neighborCh);
0437             if (verbosity > 2)std::cout << "channel in board "<< chBoard <<" found " << nNeigh << " neighbors " << std::endl;
0438           
0439             Int_t chCAENFront = backwardMapping[chBoard][minActiveLayer];
0440             Int_t chCAENBack  = backwardMapping[chBoard][maxActiveLayer];
0441             
0442             // Determine straight line trigger
0443             Bool_t  trigg         = (signal[0][chCAENFront][2] > 0 && signal[0][chCAENBack][2] > 0);
0444             Float_t triggSignal   = (signal[0][chCAENFront][2] + signal[0][chCAENBack][2])/2;
0445             
0446             // Determine if its triggered noise
0447             Bool_t triggNoise     = kFALSE;
0448             Float_t triggSigFN     = signal[0][chCAENFront][2];
0449             Float_t triggSigBN     = signal[0][chCAENBack][2];
0450             for (Int_t n = 0; n < nNeigh; n++){
0451                 triggSigFN = triggSigFN + signal[0][backwardMapping[neighborCh[n]][minActiveLayer]][2];
0452                 triggSigBN = triggSigBN + signal[0][backwardMapping[neighborCh[n]][maxActiveLayer]][2];
0453             }
0454             if (triggSigFN == 0 && triggSigBN == 0)
0455               triggNoise  = kTRUE;
0456             
0457             // Fill triggered histograms
0458             if (verbosity > 1) std::cout << "channel board " << chCAEN << "\t" << chBoard << "\t" << chCAENFront << "\t" << chCAENBack << ""<< std::endl; 
0459             if (trigg){
0460               if(mapping[chCAEN][0] == 0) histNTrig->Fill(chBoard);
0461               histHGTrig[0][chCAEN]->Fill(signal[0][chCAEN][0]); 
0462               histLGTrig[0][chCAEN]->Fill(signal[0][chCAEN][1]); 
0463             } else {
0464               histHGNoise[0][chCAEN]->Fill(signal[0][chCAEN][0]); 
0465               histLGNoise[0][chCAEN]->Fill(signal[0][chCAEN][1]);               
0466             }
0467             if (triggNoise){
0468               if(mapping[chCAEN][0] == 0) histNTrigNoise->Fill(chBoard);
0469               histHGTriggNoise[0][chCAEN]->Fill(signal[0][chCAEN][0]); 
0470               histLGTriggNoise[0][chCAEN]->Fill(signal[0][chCAEN][1]);               
0471             }
0472           }
0473         }
0474         if (verbosity > 1) std::cout << "Channels above noise: "<< nChNoNoise << std::endl;        
0475     }
0476     
0477     //**********************************************************************
0478     // Monitoring infos
0479     //**********************************************************************
0480     std::cout << "Total events processed:" << nEventsProcessed << std::endl;
0481     Double_t tdiff = (tstapMax-tstapMin)/1e6/60; //time in min
0482     std::cout << "times: " << tstapMin << "\t" << tstapMax <<"\t elapsed: " <<  tdiff  << " min"<< std::endl;
0483     
0484     //**********************************************************************
0485     // Create canvases for single channel plotting
0486     //**********************************************************************
0487     TCanvas* canvas2DCorr = new TCanvas("canvasCorrPlots","",0,0,1350,1200);  // gives the page size
0488     DefaultCancasSettings( canvas2DCorr, 0.1, 0.1, 0.02, 0.08);
0489     canvas2DCorr->SetLogz();
0490     TCanvas* canvas1DNoise = new TCanvas("canvas1DNoise","",0,0,700,500);  // gives the page size
0491     DefaultCancasSettings( canvas1DNoise, 0.07, 0.02, 0.02, 0.08);
0492     canvas1DNoise->SetLogy();
0493     TCanvas* canvas1DDiffTrigg = new TCanvas("canvas1DDiffTrigg","",0,0,700,500);  // gives the page size
0494     DefaultCancasSettings( canvas1DDiffTrigg, 0.07, 0.02, 0.02, 0.08);
0495     canvas1DDiffTrigg->SetLogy();
0496     
0497     //***********************************************************************************************************
0498     //********************************* 8 Panel overview plot  **************************************************
0499     //***********************************************************************************************************
0500     //*****************************************************************
0501     // Test beam geometry (beam coming from viewer)
0502     //==============================================
0503     //||    8    ||    7    ||    6    ||    5    ||
0504     //==============================================
0505     //||    1    ||    2    ||    3    ||    4    ||
0506     //==============================================
0507     // rebuild pad geom in similar way (numbering -1)
0508     //*****************************************************************
0509     TCanvas* canvas8Panel;
0510     TPad* pad8Panel[8];
0511     Double_t topRCornerX[8];
0512     Double_t topRCornerY[8];
0513     Int_t textSizePixel = 30;
0514     Double_t relSize8P[8];
0515     CreateCanvasAndPadsFor8PannelTBPlot(canvas8Panel, pad8Panel,  topRCornerX, topRCornerY, relSize8P, textSizePixel);
0516     
0517     //**********************************************************************
0518     // Fits for noise and LG/HG calibration
0519     //**********************************************************************
0520     TF1* fitGausHG_BG[gMaxBoard][gMaxChannels];
0521     TF1* fitGausLG_BG[gMaxBoard][gMaxChannels];
0522     TF1* fitGausHG_BG_mapped[gMaxLayers][9];
0523     TF1* fitGausLG_BG_mapped[gMaxLayers][9];
0524     TF1* fitLGHGCorr[gMaxBoard][gMaxChannels];
0525     TF1* fitHGLGCorr[gMaxBoard][gMaxChannels];
0526     Double_t mean[4][gMaxBoard][gMaxChannels];
0527     Double_t sigma[4][gMaxBoard][gMaxChannels];
0528     Double_t cslope[4][gMaxBoard][gMaxChannels];
0529     Double_t coffset[4][gMaxBoard][gMaxChannels];
0530 
0531     //**********************************************************************
0532     // Monitoring hists for fits to noise and slope LG-HG
0533     //**********************************************************************
0534     TH1D* histNoiseSigma_HG     = new TH1D("histNoiseSigma_HG", "; noise #sigma (HG ADC)", 400, 0, 20);
0535     TH1D* histNoiseMean_HG      = new TH1D("histNoiseMean_HG", "; noise #mu (HG ADC)", 400, 30, 70);
0536     TH1D* histNoiseSigma_LG     = new TH1D("histNoiseSigma_LG", "; noise #sigma (LG ADC)", 400, 0, 20);
0537     TH1D* histNoiseMean_LG      = new TH1D("histNoiseMean_LG", "; noise #mu (LG ADC)", 400, 30, 70);
0538     TH1D* histLGHG_slope        = new TH1D("histLGHGslope", "; slope (HG adc/LG adc)", 400, 0, 40);
0539     
0540     TH2D* hist2DNoiseSigma_HG   = new TH2D("hist2DNoiseSigma_HG_z_channel", "; channel; layer; noise #sigma (HG ADC)", 8, 0.5, 8.5, 14, -0.5, 13.5);
0541     TH2D* hist2DNoiseMean_HG    = new TH2D("hist2DNoiseMean_HG_z_channel", "; channel; layer; noise #mu (HG ADC)", 8, 0.5, 8.5, 14, -0.5, 13.5);
0542     TH2D* hist2DNoiseSigma_LG   = new TH2D("hist2DNoiseSigma_LG_z_channel", "; channel; layer; noise #sigma (LG ADC)", 8, 0.5, 8.5, 14, -0.5, 13.5);
0543     TH2D* hist2DNoiseMean_LG    = new TH2D("hist2DNoiseMean_LG_z_channel", "; channel; layer; noise #mu (LG ADC)", 8, 0.5, 8.5, 14, -0.5, 13.5);
0544     TH2D* hist2DLGHG_slope      = new TH2D("hist2D_LGHGslope_z_channel", "; channel; layer; slope (HG adc/LG adc)", 8, 0.5, 8.5, 14, -0.5, 13.5);
0545     TH2D* hist2DLGHG_offset     = new TH2D("hist2D_LGHGoffset_z_channel", "; channel; layer; offset (HG ADC)", 8, 0.5, 8.5, 14, -0.5, 13.5);
0546     
0547     // 1D channel representation of fit values, x axis scales as 10x layer count + channel within one assembley, 
0548     // - layer 3 channel 3: 33
0549     // - layer 0 channel 2: 2
0550     TH1D* hist1DNoiseSigma_HG   = new TH1D("hist1DNoiseSigma_HG_channels", "; 10x layer + board channel; noise #sigma (HG ADC)", 10*maxActiveLayer+1, -0.5, 10*maxActiveLayer+0.5 );
0551     TH1D* hist1DNoiseMean_HG    = new TH1D("hist1DNoiseMean_HG_channels", "; 10x layer + board channel; noise #mu (HG ADC)", 10*maxActiveLayer+1, -0.5, 10*maxActiveLayer+0.5 );
0552     TH1D* hist1DNoiseSigma_LG   = new TH1D("hist1DNoiseSigma_LG_channels", "; 10x layer + board channel; noise #sigma (LG ADC)", 10*maxActiveLayer+1, -0.5, 10*maxActiveLayer+0.5 );
0553     TH1D* hist1DNoiseMean_LG    = new TH1D("hist1DNoiseMean_LG_channels", "; 10x layer + board channel; noise #mu (LG ADC)", 10*maxActiveLayer+1, -0.5, 10*maxActiveLayer+0.5 );
0554     TH1D* hist1DLGHG_slope      = new TH1D("hist1DLGHG_slope_channels", "; 10x layer + board channel; slope (HG adc/LG adc)", 10*maxActiveLayer+1, -0.5, 10*maxActiveLayer+0.5 );
0555     TH1D* hist1DLGHG_offset     = new TH1D("hist1DLGHG_slope_channels", "; 10x layer + board channel; offset (HG adc/LG adc)", 10*maxActiveLayer+1, -0.5, 10*maxActiveLayer+0.5 );
0556     TH1D* hist1DHGLG_slope      = new TH1D("hist1DHGLG_slope_channels", "; 10x layer + board channel; slope (LG adc/HG adc)", 10*maxActiveLayer+1, -0.5, 10*maxActiveLayer+0.5 );
0557     TH1D* hist1DHGLG_offset     = new TH1D("hist1DHGLG_slope_channels", "; 10x layer + board channel; offset (LG adc/HG adc)", 10*maxActiveLayer+1, -0.5, 10*maxActiveLayer+0.5 );
0558     
0559     // 1D channel representation of fit values, x axis scales as CAEN board channelns 64x CAEN board # + CAEN channel
0560     TH1D* hist1DCAEN_NoiseSigma_HG   = new TH1D("hist1DCAEN_NoiseSigma_HG_channels", "; 64x CAEN board + CAEN channel; noise #sigma (HG ADC)", 64*gMaxBoard+1, -0.5, 64*gMaxBoard+0.5 );
0561     TH1D* hist1DCAEN_NoiseMean_HG    = new TH1D("hist1DCAEN_NoiseMean_HG_channels", "; 64x CAEN board + CAEN channel; noise #mu (HG ADC)", 64*gMaxBoard+1, -0.5, 64*gMaxBoard+0.5 );
0562     TH1D* hist1DCAEN_NoiseSigma_LG   = new TH1D("hist1DCAEN_NoiseSigma_LG_channels", "; 64x CAEN board + CAEN channel; noise #sigma (LG ADC)", 64*gMaxBoard+1, -0.5, 64*gMaxBoard+0.5 );
0563     TH1D* hist1DCAEN_NoiseMean_LG    = new TH1D("hist1DCAEN_NoiseMean_LG_channels", "; 64x CAEN board + CAEN channel; noise #mu (LG ADC)", 64*gMaxBoard+1, -0.5, 64*gMaxBoard+0.5 );
0564     TH1D* hist1DCAEN_LGHG_slope      = new TH1D("hist1DCAEN_LGHG_slope_channels", "; 64x CAEN board + CAEN channel; slope (HG adc/LG adc)", 64*gMaxBoard+1, -0.5, 64*gMaxBoard+0.5 );
0565     TH1D* hist1DCAEN_LGHG_offset     = new TH1D("hist1DCAEN_LGHG_slope_channels", "; 64x CAEN board + CAEN channel; offset (HG adc/LG adc)", 64*gMaxBoard+1, -0.5, 64*gMaxBoard+0.5 );
0566     TH1D* hist1DCAEN_HGLG_slope      = new TH1D("hist1DCAEN_HGLG_slope_channels", "; 10x layer + board channel; slope (LG adc/HG adc)", 64*gMaxBoard+1, -0.5, 10*maxActiveLayer+0.5 );
0567     TH1D* hist1DCAEN_HGLG_offset     = new TH1D("hist1DCAEN_HGLG_slope_channels", "; 10x layer + board channel; offset (LG adc/HG adc)", 64*gMaxBoard+1, -0.5, 10*maxActiveLayer+0.5 );
0568 
0569     //**********************************************************************
0570     // Initialize fits for all layers and channels to nullptr
0571     //**********************************************************************    
0572     for (Int_t l = 0; l < gMaxLayers; l++){
0573       for (Int_t c = 0; c< 9;c++){
0574         fitGausHG_BG_mapped[l][c] = nullptr;
0575         fitGausLG_BG_mapped[l][c] = nullptr;
0576       }
0577     }
0578     
0579     // ********************************************************************************************************
0580     // Fitting Noise and correlation HG-LG, LG-HG
0581     // -> detailed plotting per channel
0582     // -> remapping of CAEN board+channels to physicsal layers and readout-channels
0583     // ********************************************************************************************************    
0584     for (Int_t j = 0; j < gMaxBoard; j++){
0585       for (Int_t i = 0; i < gMaxChannels; i++){
0586         mean[0][j][i] = -1;
0587         mean[1][j][i] = -1;
0588         mean[2][j][i] = -1;
0589         mean[3][j][i] = -1;
0590         sigma[0][j][i] = -1;
0591         sigma[1][j][i] = -1;
0592         sigma[2][j][i] = -1;
0593         sigma[3][j][i] = -1;
0594 
0595         Int_t chMap     = j*64 + i;
0596         if (mapping[chMap][0] == -1 && isTBdata > 0){
0597           fitGausHG_BG[j][i] = nullptr;
0598           fitGausLG_BG[j][i] = nullptr;
0599           fitHGLGCorr[j][i] = nullptr;
0600           fitLGHGCorr[j][i] = nullptr;
0601           continue;
0602         }
0603         Int_t chBoard   = mapping[chMap][1];
0604         Int_t layer     = mapping[chMap][0];
0605         Int_t channelBin1D = hist1DNoiseSigma_HG->FindBin(layer*10+chBoard);          
0606           
0607         if (verbosity > 0)std::cout << j << "\t" << i << "\t" << chMap << "\t L: " << layer  << "\t C:" <<  chBoard << "\t bin ID "<< channelBin1D << std::endl;
0608         // ********************************************************
0609         // map raw and trigger histos from CAEN numbering to 
0610         // physical layers & channels
0611         // ********************************************************
0612         histHG_mapped[layer][chBoard] = (TH1D*)histHG[j][i]->Clone(Form("h_HG_mapped_L%d_C%02d",layer,chBoard));
0613         histLG_mapped[layer][chBoard] = (TH1D*)histLG[j][i]->Clone(Form("h_LG_mapped_L%d_C%02d",layer,chBoard));
0614         if (histHGTrig[j][i] ) histHGTrig_mapped[layer][chBoard] = (TH1D*)histHGTrig[j][i]->Clone(Form("h_HGTrigg_mapped_L%d_C%02d",layer,chBoard));
0615         if (histLGTrig[j][i]) histLGTrig_mapped[layer][chBoard] = (TH1D*)histLGTrig[j][i]->Clone(Form("h_LGTrigg_mapped_L%d_C%02d",layer,chBoard));
0616         
0617         // ********************************************************
0618         // Fit & plot noise histos
0619         // -> map noise histos from CAEN numb. -> physical
0620         // -> fill monitoring plots
0621         // ********************************************************
0622         Bool_t bFit = kFALSE;
0623         // HG noise fits
0624         if (histHGTriggNoise[j][i] && histHGTriggNoise[j][i]->GetEntries() > 0){
0625           bFit = FitNoise (histHGTriggNoise[j][i], fitGausHG_BG[j][i], mean[0][j][i], mean[1][j][i], sigma[0][j][i], sigma[1][j][i], j, i, "f_GaussBG_HG", "HG");
0626           if (bFit && bDetPlot) PlotNoiseSingle (canvas1DNoise, histHGTriggNoise[j][i], fitGausHG_BG[j][i], mean[0][j][i], mean[1][j][i], sigma[0][j][i], sigma[1][j][i], j, i, layer, chBoard, 
0627                                                   Form("%s/HG_NoiseWithFit", outputDirPlotsDet.Data()), currentRunInfo, 0.04);
0628           histHGTriggNoise_mapped[layer][chBoard] = (TH1D*)histHGTriggNoise[j][i]->Clone(Form("h_HGTriggeredNoise_mapped_L%d_C%02d",layer,chBoard));
0629           if (bFit){
0630             fitGausHG_BG_mapped[layer][chBoard] = (TF1*)fitGausHG_BG[j][i]->Clone(Form("f_GaussBG_HG_mapped_L%d_C%02d",layer,chBoard));
0631           }
0632         } else {
0633           bFit = FitNoise (histHG[j][i], fitGausHG_BG[j][i], mean[0][j][i], mean[1][j][i], sigma[0][j][i], sigma[1][j][i], j, i, "f_GaussBG_HG", "HG");
0634           if (bFit && bDetPlot) PlotNoiseSingle (canvas1DNoise, histHG[j][i], fitGausHG_BG[j][i], mean[0][j][i], mean[1][j][i], sigma[0][j][i], sigma[1][j][i], j, i, layer, chBoard, 
0635                                                   Form("%s/HG_NoiseWithFit", outputDirPlotsDet.Data()), currentRunInfo, 0.04);
0636           if (bFit){
0637             fitGausHG_BG_mapped[layer][chBoard] = (TF1*)fitGausHG_BG[j][i]->Clone(Form("f_GaussBG_HG_mapped_L%d_C%02d",layer,chBoard));
0638           }
0639         }
0640         if (bFit == kFALSE) fitGausHG_BG[j][i] = nullptr;
0641         // fill fit monitoring hists HG
0642         if (bFit){
0643           hist2DNoiseMean_HG->Fill(chBoard,layer,mean[0][j][i]);
0644           hist2DNoiseSigma_HG->Fill(chBoard,layer,sigma[0][j][i]);
0645           histNoiseMean_HG->Fill(mean[0][j][i]);
0646           histNoiseSigma_HG->Fill(sigma[0][j][i]);
0647           
0648           hist1DNoiseSigma_HG->SetBinContent(channelBin1D, sigma[0][j][i]);
0649           hist1DNoiseSigma_HG->SetBinError(channelBin1D, sigma[1][j][i]);
0650           hist1DNoiseMean_HG->SetBinContent(channelBin1D, mean[0][j][i]);
0651           hist1DNoiseMean_HG->SetBinError(channelBin1D, mean[1][j][i]);
0652           hist1DCAEN_NoiseSigma_HG->SetBinContent(chMap, sigma[0][j][i]);
0653           hist1DCAEN_NoiseSigma_HG->SetBinError(chMap, sigma[1][j][i]);
0654           hist1DCAEN_NoiseMean_HG->SetBinContent(chMap, mean[0][j][i]);          
0655           hist1DCAEN_NoiseMean_HG->SetBinError(chMap, mean[1][j][i]);          
0656         }
0657         // reset boolean for fit success monitoring
0658         bFit = kFALSE;
0659         // LG noise fits
0660         if (histLGTriggNoise[j][i] && histLGTriggNoise[j][i]->GetEntries() > 0){
0661           bFit = FitNoise (histLGTriggNoise[j][i], fitGausLG_BG[j][i], mean[2][j][i], mean[3][j][i], sigma[2][j][i], sigma[3][j][i], j, i, "f_GaussBG_LG", "LG");
0662           if (bFit && bDetPlot) PlotNoiseSingle (canvas1DNoise, histLGTriggNoise[j][i], fitGausLG_BG[j][i],  mean[2][j][i], mean[3][j][i], sigma[2][j][i], sigma[3][j][i], j, i, layer, chBoard, 
0663                                                   Form("%s/LG_NoiseWithFit", outputDirPlotsDet.Data()), currentRunInfo, 0.04);
0664           histLGTriggNoise_mapped[layer][chBoard] = (TH1D*)histLGTriggNoise[j][i]->Clone(Form("h_LGTriggeredNoise_mapped_L%d_C%02d",layer,chBoard));
0665           if (bFit){
0666             fitGausLG_BG_mapped[layer][chBoard] = (TF1*)fitGausLG_BG[j][i]->Clone(Form("f_GaussBG_LG_mapped_L%d_C%02d",layer,chBoard));
0667           }
0668         } else {
0669           bFit = FitNoise (histLG[j][i], fitGausLG_BG[j][i], mean[2][j][i], mean[3][j][i], sigma[2][j][i], sigma[3][j][i], j, i, "f_GaussBG_LG", "LG");
0670           if (bFit && bDetPlot) PlotNoiseSingle (canvas1DNoise, histLG[j][i], fitGausLG_BG[j][i], mean[2][j][i], mean[3][j][i], sigma[2][j][i], sigma[3][j][i], j, i, layer, chBoard, 
0671                                                   Form("%s/LG_NoiseWithFit", outputDirPlotsDet.Data()), currentRunInfo, 0.04);
0672           if (bFit){
0673             fitGausLG_BG_mapped[layer][chBoard] = (TF1*)fitGausLG_BG[j][i]->Clone(Form("f_GaussBG_LG_mapped_L%d_C%02d",layer,chBoard));
0674           }
0675         }
0676         if (bFit == kFALSE) fitGausLG_BG[j][i] = nullptr;
0677         // fill fit monitoring hists LG
0678         if (bFit){
0679           hist2DNoiseMean_LG->Fill(chBoard,layer,mean[2][j][i]);
0680           hist2DNoiseSigma_LG->Fill(chBoard,layer,sigma[2][j][i]);
0681           histNoiseMean_LG->Fill(mean[2][j][i]);
0682           histNoiseSigma_LG->Fill(sigma[2][j][i]);
0683           hist1DNoiseSigma_LG->SetBinContent(channelBin1D, sigma[2][j][i]);
0684           hist1DNoiseSigma_LG->SetBinError(channelBin1D, sigma[3][j][i]);
0685           hist1DNoiseMean_LG->SetBinContent(channelBin1D, mean[2][j][i]);
0686           hist1DNoiseMean_LG->SetBinError(channelBin1D, mean[3][j][i]);
0687           hist1DCAEN_NoiseSigma_LG->SetBinContent(chMap, sigma[2][j][i]);
0688           hist1DCAEN_NoiseSigma_LG->SetBinError(chMap, sigma[3][j][i]);
0689           hist1DCAEN_NoiseMean_LG->SetBinContent(chMap, mean[2][j][i]);          
0690           hist1DCAEN_NoiseMean_LG->SetBinError(chMap, mean[3][j][i]);          
0691         }
0692         // ********************************************************
0693         // Plot 2D distribution time vs HG for monitoring
0694         // ********************************************************
0695         if (hist_T_HG[j][i]  && hist_T_HG[j][i]->GetEntries() > 0 && bDetPlot){
0696           canvas2DCorr->cd();
0697             SetStyleHistoTH2ForGraphs( hist_T_HG[j][i], hist_T_HG[j][i]->GetXaxis()->GetTitle(), hist_T_HG[j][i]->GetYaxis()->GetTitle(), 0.85*textSizeRel, textSizeRel, 0.85*textSizeRel, textSizeRel,0.9, 1.25);  
0698             // find max x bin
0699             hist_T_HG[j][i]->GetXaxis()->SetRangeUser(0,tdiff+0.1);
0700             hist_T_HG[j][i]->Draw("colz");
0701             
0702             TLatex *labelChannel                     = new TLatex(0.85,0.92,Form("CAEN: B %d, C %d, Stack: L %d, C%d",j, i, layer, chBoard));
0703             SetStyleTLatex( labelChannel, textSizeRel,4,1,42,kTRUE,31);
0704             labelChannel->Draw();
0705 
0706           canvas2DCorr->SaveAs(Form("%s/T_HG_B%d_C%02d.pdf", outputDirPlotsDet.Data(), j,i));
0707           delete labelChannel;
0708         }
0709         // ********************************************************
0710         // Fit & Plot 2D LG vs HG
0711         // ********************************************************
0712         if (histLGHG[j][i] && histLGHG[j][i]->GetEntries() > 0){
0713           FitAndPlotGainCorr ( histLGHG[j][i], fitLGHGCorr[j][i], "f_LGHGCorr", 100, 380, 500, 4000,
0714                               cslope[0][j][i], cslope[1][j][i], coffset[0][j][i], coffset[1][j][i], 
0715                               j, i, layer, chBoard,
0716                               kTRUE, canvas2DCorr, Form("%s/LG_HG_Corr", outputDirPlotsDet.Data()), textSizeRel);
0717           hist2DLGHG_slope->Fill(chBoard,layer,cslope[0][j][i]);
0718           hist2DLGHG_offset->Fill(chBoard,layer,coffset[0][j][i]);
0719           histLGHG_slope->Fill(cslope[0][j][i]);
0720           hist1DLGHG_slope->SetBinContent(channelBin1D, cslope[0][j][i]);
0721           hist1DLGHG_slope->SetBinError(channelBin1D, cslope[1][j][i]);
0722           hist1DLGHG_offset->SetBinContent(channelBin1D, coffset[0][j][i]);
0723           hist1DLGHG_offset->SetBinError(channelBin1D, coffset[1][j][i]);
0724           hist1DCAEN_LGHG_slope->SetBinContent(chMap, cslope[0][j][i]);
0725           hist1DCAEN_LGHG_slope->SetBinError(chMap, cslope[1][j][i]);
0726           hist1DCAEN_LGHG_offset->SetBinContent(chMap, coffset[0][j][i]);
0727           hist1DCAEN_LGHG_offset->SetBinError(chMap, coffset[1][j][i]);
0728         } else {
0729           fitLGHGCorr[j][i] = nullptr;
0730         }
0731         if (histHGLG[j][i] && histLGHG[j][i]->GetEntries() > 0){
0732           FitAndPlotGainCorr ( histHGLG[j][i], fitHGLGCorr[j][i], "f_HGLGCorr", 100, 3800, 4000, 500,
0733                               cslope[2][j][i], cslope[3][j][i], coffset[2][j][i], coffset[3][j][i], 
0734                               j, i, layer, chBoard,
0735                               kFALSE, canvas2DCorr, Form("%s/HG_LG_Corr", outputDirPlotsDet.Data()), textSizeRel);
0736           hist1DHGLG_slope->SetBinContent(channelBin1D, cslope[2][j][i]);
0737           hist1DHGLG_slope->Fill(channelBin1D, cslope[3][j][i]);
0738           hist1DHGLG_offset->SetBinContent(channelBin1D, coffset[2][j][i]);
0739           hist1DHGLG_offset->SetBinError(channelBin1D, coffset[3][j][i]);
0740           hist1DCAEN_HGLG_slope->SetBinContent(chMap, cslope[2][j][i]);
0741           hist1DCAEN_HGLG_slope->SetBinError(chMap, cslope[3][j][i]);
0742           hist1DCAEN_HGLG_offset->SetBinContent(chMap, coffset[2][j][i]);
0743           hist1DCAEN_HGLG_offset->SetBinError(chMap, coffset[3][j][i]);
0744         } else {
0745           fitHGLGCorr[j][i] = nullptr;
0746         }        
0747         
0748         // ********************************************************
0749         // Plot different triggers together
0750         // ********************************************************
0751         Int_t hgmax = 1050;
0752         if (isTBdata == 2) hgmax = 3999;
0753         if (bDetPlot)PlotOverlayDiffTriggers( canvas1DDiffTrigg, histHG[j][i], histHGTrig[j][i], histHGTriggNoise[j][i], fitGausHG_BG[j][i],
0754                                               0, hgmax, Form("%s/HG_DiffTriggers", outputDirPlotsDet.Data()),
0755                                               j, i, layer, chBoard, currentRunInfo, 0.04);
0756         if (bDetPlot)PlotOverlayDiffTriggers( canvas1DDiffTrigg, histLG[j][i], histLGTrig[j][i], histLGTriggNoise[j][i], fitGausLG_BG[j][i],
0757                                               0, 1050, Form("%s/LG_DiffTriggers", outputDirPlotsDet.Data()),
0758                                               j, i, layer, chBoard, currentRunInfo, 0.04);
0759       }
0760     }
0761     
0762     for (Int_t l = 0; l < gMaxLayers; l++){
0763       if (!lActive[l]) continue;      
0764       if (isTBdata == 1){
0765         PlotNoiseWithFitsFullLayer (canvas8Panel,pad8Panel, topRCornerX, topRCornerY, relSize8P, textSizePixel, 
0766                                     histHGTriggNoise_mapped[l], fitGausHG_BG_mapped[l], 0, 275, 1.2, l,
0767                                     Form("%s/Noise_HG_Layer%02d.pdf" ,outputDirPlots.Data(), l), currentRunInfo);
0768         PlotNoiseWithFitsFullLayer (canvas8Panel,pad8Panel, topRCornerX, topRCornerY, relSize8P, textSizePixel, 
0769                                     histLGTriggNoise_mapped[l], fitGausLG_BG_mapped[l], 0, 275, 1.2, l,
0770                                     Form("%s/Noise_LG_Layer%02d.pdf" ,outputDirPlots.Data(), l), currentRunInfo);
0771       }
0772       
0773       if (isTBdata < 2){
0774         PlotDiffTriggersFullLayer (canvas8Panel,pad8Panel, topRCornerX, topRCornerY, relSize8P, textSizePixel, 
0775                                   histHG_mapped[l], histHGTrig_mapped[l], histHGTriggNoise_mapped[l], fitGausHG_BG_mapped[l], 
0776                                   0, 3800, 1.2, l , Form("%s/TriggerOverlay_HG_Layer%02d.pdf" ,outputDirPlots.Data(), l), currentRunInfo);
0777         PlotDiffTriggersFullLayer (canvas8Panel,pad8Panel, topRCornerX, topRCornerY, relSize8P, textSizePixel, 
0778                                   histHG_mapped[l], histHGTrig_mapped[l], histHGTriggNoise_mapped[l], fitGausHG_BG_mapped[l], 
0779                                   0, 2100, 1.2, l , Form("%s/TriggerOverlay_HG_Zoomed_Layer%02d.pdf" ,outputDirPlots.Data(), l), currentRunInfo);
0780         PlotDiffTriggersFullLayer (canvas8Panel,pad8Panel, topRCornerX, topRCornerY, relSize8P, textSizePixel, 
0781                                   histLG_mapped[l], histLGTrig_mapped[l], histLGTriggNoise_mapped[l], fitGausLG_BG_mapped[l], 
0782                                   0, 3800, 1.2, l , Form("%s/TriggerOverlay_LG_Layer%02d.pdf" ,outputDirPlots.Data(), l), currentRunInfo);
0783         PlotDiffTriggersFullLayer (canvas8Panel,pad8Panel, topRCornerX, topRCornerY, relSize8P, textSizePixel, 
0784                                   histLG_mapped[l], histLGTrig_mapped[l], histLGTriggNoise_mapped[l], fitGausLG_BG_mapped[l], 
0785                                   0, 1525, 1.2, l , Form("%s/TriggerOverlay_LG_Zoomed_Layer%02d.pdf" ,outputDirPlots.Data(), l), currentRunInfo);
0786       }
0787       
0788     }
0789     if (isTBdata == 1){
0790       PlotOverlayFullLayer ( canvas8Panel, pad8Panel, topRCornerX, topRCornerY, relSize8P, textSizePixel, 
0791                               histHGTriggNoise_mapped, lActive, 0, gMaxLayers,
0792                               0, 350, -10, 425, 3, Form("%s/HG_Noise_AllLayers.pdf", outputDirPlots.Data()), currentRunInfo, "pe");
0793     }
0794     for (Int_t c = 1; c < 9; c++){      
0795       if (isTBdata == 2 && c > 1) continue; 
0796       PlotChannelOverlaySameReadoutChannel( canvas1DDiffTrigg, histHG_mapped, lActive, 0, gMaxLayers, 
0797                                             -100,4000, 1./5, Form("%s/HG", outputDirPlots.Data()), c, currentRunInfo,0.04,"hist");    
0798     }
0799     
0800     
0801     // ********************************************************
0802     // Create graphs per board and channels with calib values
0803     // -> noise mean & sigma LG, HG
0804     // -> slope and offset of LG-HG & HG-LG correlation
0805     // ********************************************************
0806     TGraphErrors* gNoiseMeanHG      = CreateGraphFromHistAndCleanup(hist1DNoiseMean_HG, "graph_mean_Noise_HG_channels");
0807     TGraphErrors* gNoiseMeanLG      = CreateGraphFromHistAndCleanup(hist1DNoiseMean_LG, "graph_mean_Noise_LG_channels");
0808     TGraphErrors* gNoiseSigmaHG     = CreateGraphFromHistAndCleanup(hist1DNoiseSigma_HG, "graph_sigma_Noise_HG_channels");
0809     TGraphErrors* gNoiseSigmaLG     = CreateGraphFromHistAndCleanup(hist1DNoiseSigma_LG, "graph_sigma_Noise_LG_channels");
0810     TGraphErrors* gCorrLGHGSlope    = CreateGraphFromHistAndCleanup(hist1DLGHG_slope, "graph_slope_corr_LGHG_channels");
0811     TGraphErrors* gCorrLGHGOffset   = CreateGraphFromHistAndCleanup(hist1DLGHG_offset, "graph_offset_corr_LGHG_channels");
0812     TGraphErrors* gCorrHGLGSlope    = CreateGraphFromHistAndCleanup(hist1DHGLG_slope, "graph_slope_corr_HGLG_channels");
0813     TGraphErrors* gCorrHGLGOffset   = CreateGraphFromHistAndCleanup(hist1DHGLG_offset, "graph_offset_corr_HGLG_channels");
0814 
0815     TGraphErrors* gCAEN_NoiseMeanHG     = CreateGraphFromHistAndCleanup(hist1DCAEN_NoiseMean_HG, "graphCAEN_mean_Noise_HG_channels");
0816     TGraphErrors* gCAEN_NoiseMeanLG     = CreateGraphFromHistAndCleanup(hist1DCAEN_NoiseMean_LG, "graphCAEN_mean_Noise_LG_channels");
0817     TGraphErrors* gCAEN_NoiseSigmaHG    = CreateGraphFromHistAndCleanup(hist1DCAEN_NoiseSigma_HG, "graphCAEN_sigma_Noise_HG_channels");
0818     TGraphErrors* gCAEN_NoiseSigmaLG    = CreateGraphFromHistAndCleanup(hist1DCAEN_NoiseSigma_LG, "graphCAEN_sigma_Noise_LG_channels");
0819     TGraphErrors* gCAEN_CorrLGHGSlope   = CreateGraphFromHistAndCleanup(hist1DCAEN_LGHG_slope, "graphCAEN_slope_corr_LGHG_channels");
0820     TGraphErrors* gCAEN_CorrLGHGOffset  = CreateGraphFromHistAndCleanup(hist1DCAEN_LGHG_offset, "graphCAEN_offset_corr_LGHG_channels");
0821     TGraphErrors* gCAEN_CorrHGLGSlope   = CreateGraphFromHistAndCleanup(hist1DCAEN_HGLG_slope, "graphCAEN_slope_corr_HGLG_channels");
0822     TGraphErrors* gCAEN_CorrHGLGOffset  = CreateGraphFromHistAndCleanup(hist1DCAEN_HGLG_offset, "graphCAEN_offset_corr_HGLG_channels");
0823         
0824     // ********************************************************************************************************
0825     // Noise subtracted data monitoring histos per CAEN board and channel
0826     // ********************************************************************************************************    
0827     TH1D* histNSLG[gMaxBoard][gMaxChannels];                // low gain all triggers
0828     TH1D* histNSHG[gMaxBoard][gMaxChannels];                // high gain all triggers
0829     TH1D* histNSCombHG[gMaxBoard][gMaxChannels];            // combined high&low gain all triggers
0830     TH2D* histNSLGHG[gMaxBoard][gMaxChannels];              // LG vs HG correlation
0831     TH2D* histNSHGLG[gMaxBoard][gMaxChannels];              // HG vs LG correlation
0832     TH1D* histNSLGTrig[gMaxBoard][gMaxChannels];            // low gain straight line triggers
0833     TH1D* histNSHGTrig[gMaxBoard][gMaxChannels];            // high gain straight line triggers
0834     TH1D* histNSCombHGTrig[gMaxBoard][gMaxChannels];        // combined high&low gain straight line triggers
0835     TH1D* histNSLGNoise[gMaxBoard][gMaxChannels];           // low gain !(straight line triggers)
0836     TH1D* histNSHGNoise[gMaxBoard][gMaxChannels];           // high gain !(straight line triggers)
0837     TH1D* histNSLGTriggNoise[gMaxBoard][gMaxChannels];      // low gain noise triggered
0838     TH1D* histNSHGTriggNoise[gMaxBoard][gMaxChannels];      // high gain noise triggered
0839     TH2D* hist_T_NSCombHG[gMaxBoard];                       // time vs high&low gain all triggers
0840     TF1*  fitLandauG_NSHGTrig[gMaxBoard][gMaxChannels];     // Landau Gauss fits
0841     
0842     //***************************************************
0843     // mapped channels - layer & read-out board channels
0844     //***************************************************
0845     // channel counting in layer starts with 1, easier acces make array go to 8
0846     TH1D* histNSLG_mapped[gMaxLayers][9];                   // low gain all triggers
0847     TH1D* histNSHG_mapped[gMaxLayers][9];                   // high gain all triggers
0848     TH1D* histNSCombHG_mapped[gMaxLayers][9];               // combined high&low gain all triggers
0849     TH1D* histNSLGTrig_mapped[gMaxLayers][9];               // low gain straight line triggers
0850     TH1D* histNSHGTrig_mapped[gMaxLayers][9];               // high gain straight line triggers
0851     TH1D* histNSCombHGTrig_mapped[gMaxLayers][9];           // combined high&low gain straight line triggers
0852     TH1D* histNSLGTriggNoise_mapped[gMaxLayers][9];         // low gain noise triggered
0853     TH1D* histNSHGTriggNoise_mapped[gMaxLayers][9];         // high gain noise triggered
0854 
0855     //***************************************************
0856     // mapped channels - layer & read-out board channels - rebinned
0857     //***************************************************
0858     // channel counting in layer starts with 1, easier acces make array go to 8
0859     TH1D* histNSHG_mappedReb[gMaxLayers][9];                   // high gain all triggers
0860     TH1D* histNSCombHG_mappedReb[gMaxLayers][9];               // combined high&low gain all triggers
0861     TH1D* histNSHGTrig_mappedReb[gMaxLayers][9];               // high gain straight line triggers
0862     TH1D* histNSCombHGTrig_mappedReb[gMaxLayers][9];           // combined high&low gain straight line triggers
0863     TF1*  fitLandauG_NSHGTrig_mapped[gMaxLayers][9];           // Landau Gauss fits
0864      
0865     //***************************************************
0866     // map of triggers above threshold  
0867     //***************************************************
0868     TH3D* histNS3DMap = new TH3D("h_map_NS_z_x_y", "; layer; col; row", 14, -0.5, 13.5, 4, -0.5, 3.5, 2, -0.5, 1.5);
0869     TH2D* histNS2DMap = new TH2D("h_map_NS_z_channel", "; channel; layer", 8, 0.5, 8.5, 14, -0.5, 13.5);
0870     TH1D* histNS1DMap = new TH1D("h_map_NS_channel", "; channel", 8, 0.5, 8.5);
0871     TH2D* hist2DNSTrigg         = new TH2D("hist2D_NS_Trigg", "; channel; layer; counts", 8, 0.5, 8.5, 14, -0.5, 13.5);
0872     TH2D* hist2DNSTriggNoise    = new TH2D("hist2D_NS_TriggeredNoise", "; channel; layer; counts", 8, 0.5, 8.5, 14, -0.5, 13.5);
0873     //***************************************************
0874     // Init histograms
0875     //***************************************************    
0876     for (Int_t j = 0; j < gMaxBoard; j++){
0877       for (Int_t i = 0; i < gMaxChannels; i++){
0878         histNSHG[j][i]    = new TH1D(Form("h_NS_HG_B%d_C%02d",j,i),";HG (adc); counts",4201,-200,4001);
0879         histNSCombHG[j][i]= new TH1D(Form("h_NS_CombHG_B%d_C%02d",j,i),";comb HG equivalent (adc); counts",40201,-200,40001);
0880         histNSLG[j][i]    = new TH1D(Form("h_NS_LG_B%d_C%02d",j,i),";LG (adc); counts",4201,-200,4001);
0881         histNSLGHG[j][i]  = new TH2D(Form("h_NS_LGHG_B%d_C%02d",j,i),";LG (adc); HG (adc)",420,-200,4001,420,-200,4001);
0882         histNSHGLG[j][i]  = new TH2D(Form("h_NS_HGLG_B%d_C%02d",j,i),";HG (adc); LG (adc)",420,-200,4001,420,-200,4001);
0883         if (isTBdata > 0){
0884           histNSLGTrig[j][i]        = new TH1D(Form("h_NS_LGTrigg_B%d_C%02d",j,i),"coincidence trigg. front & back ; LG (adc); counts",4201,-200,4001);   // triggered 
0885           histNSHGTrig[j][i]        = new TH1D(Form("h_NS_HGTrigg_B%d_C%02d",j,i),"coincidence trigg. front & back ; HG (adc); counts",4201,-200,4001);
0886           histNSCombHGTrig[j][i]    = new TH1D(Form("h_NS_CombHGTrigg_B%d_C%02d",j,i),"coincidence trigg. front & back ; comb HG equivalent (adc); counts",40201,-200,40001);
0887           histNSLGNoise[j][i]       = new TH1D(Form("h_NS_LGNoise_B%d_C%02d",j,i),"!(coincidence trigg. front & back) ;LG (adc); counts",4201,-200,4001);
0888           histNSHGNoise[j][i]       = new TH1D(Form("h_NS_HGNoise_B%d_C%02d",j,i),"!(coincidence trigg. front & back) ;HG (adc); counts",4201,-200,4001);
0889           histNSLGTriggNoise[j][i]  = new TH1D(Form("h_NS_LGTriggeredNoise_B%d_C%02d",j,i),"veto front & back; LG (adc); counts",4201,-200,4001);
0890           histNSHGTriggNoise[j][i]  = new TH1D(Form("h_NS_HGTriggeredNoise_B%d_C%02d",j,i),"veto front & back; HG (adc); counts",4201,-200,4001);
0891         } else {
0892           histNSLGTrig[j][i]        = nullptr;
0893           histNSHGTrig[j][i]        = nullptr;
0894           histNSCombHGTrig[j][i]    = nullptr;
0895           histNSLGNoise[j][i]       = nullptr;
0896           histNSHGNoise[j][i]       = nullptr;
0897           histNSLGTriggNoise[j][i]  = nullptr;
0898           histNSHGTriggNoise[j][i]  = nullptr;
0899         }
0900         fitLandauG_NSHGTrig[j][i]     = nullptr;
0901       }
0902       if (isTBdata)
0903         hist_T_NSCombHG[j]= new TH2D(Form("h_T_NS_CombHG_B%d",j),"; t (min); comb HG equivalent (adc)",1000,0,120,10000,0,1e6);
0904       else 
0905         hist_T_NSCombHG[j]= new TH2D(Form("h_T_NS_CombHG_B%d",j),"; t (min); comb HG equivalent (adc)",10000,0,1000,10000,0,1e6);
0906     }
0907     
0908     for (Int_t l = 0; l < gMaxLayers; l++){
0909       for (Int_t c = 0; c < 9; c++){
0910         histNSLG_mapped[l][c]             = nullptr;
0911         histNSHG_mapped[l][c]             = nullptr; 
0912         histNSLGTrig_mapped[l][c]         = nullptr;
0913         histNSHGTrig_mapped[l][c]         = nullptr;
0914         histNSLGTriggNoise_mapped[l][c]   = nullptr;
0915         histNSHGTriggNoise_mapped[l][c]   = nullptr;
0916         histNSCombHG_mapped[l][c]         = nullptr; 
0917         histNSCombHGTrig_mapped[l][c]     = nullptr;
0918         histNSHG_mappedReb[l][c]             = nullptr;
0919         histNSHGTrig_mappedReb[l][c]         = nullptr;
0920         histNSCombHG_mappedReb[l][c]         = nullptr; 
0921         histNSCombHGTrig_mappedReb[l][c]     = nullptr;
0922         fitLandauG_NSHGTrig_mapped[l][c]     = nullptr;
0923       }
0924     }
0925 
0926     // rebin array
0927     Double_t binningADC[3000];
0928     for (Int_t i = 0; i < 1200; i++) binningADC[i]                      = -200+i;
0929     for (Int_t i = 0; i < 500; i++) binningADC[i+1200]                  = 1000+i*2;   
0930     for (Int_t i = 0; i < 400; i++) binningADC[i+1200+500]              = 2000+i*5;   
0931     for (Int_t i = 0; i < 200; i++) binningADC[i+1200+500+400]          = 4000+i*10;   
0932     for (Int_t i = 0; i < 280; i++) binningADC[i+1200+500+400+200]      = 6000+i*50;   
0933     for (Int_t i = 0; i < 201; i++) binningADC[i+1200+500+400+200+280]  = 20000+i*100;   
0934     
0935     if (verbosity > 2)for (Int_t i = 0; i < 2781; i++) std::cout << binningADC[i] << "," ;
0936     if (verbosity > 2)std::cout<< std::endl; 
0937     
0938     // 1D channel representation of fit values, x axis scales as 10x layer count + channel within one assembley, 
0939     // - layer 3 channel 3: 33
0940     // - layer 0 channel 2: 2
0941     TH1D* hist1DMPV_HG          = new TH1D("hist1DMPV_HG_channels", "; 10x layer + board channel; MPV_{mip} (HG ADC)", 10*maxActiveLayer+1, -0.5, 10*maxActiveLayer+0.5 );
0942     TH1D* hist1DMax_HG          = new TH1D("hist1DMax_HG_channels", "; 10x layer + board channel; Max_{mip} (HG ADC)", 10*maxActiveLayer+1, -0.5, 10*maxActiveLayer+0.5 );
0943     TH1D* hist1DFWHM_HG         = new TH1D("hist1DFWHM_HG_channels", "; 10x layer + board channel; FWHM_{mip} (HG ADC)", 10*maxActiveLayer+1, -0.5, 10*maxActiveLayer+0.5 );
0944     TH1D* hist1DWidth_HG        = new TH1D("hist1DWidth_HG_channels", "; 10x layer + board channel; Width_{mip} (HG ADC)", 10*maxActiveLayer+1, -0.5, 10*maxActiveLayer+0.5 );
0945     TH1D* hist1DGWidth_HG       = new TH1D("hist1DGWidth_HG_channels", "; 10x layer + board channel; Gauss Width_{mip} (HG ADC)", 10*maxActiveLayer+1, -0.5, 10*maxActiveLayer+0.5 );
0946 
0947     // 1D channel representation of fit values, x axis scales as CAEN board channelns 64x CAEN board # + CAEN channel
0948     TH1D* hist1DCAEN_MPV_HG     = new TH1D("hist1DCAEN_MPV_HG_channels", "; 64x CAEN board + CAEN channel; MPV_{mip} (HG ADC)", 64*gMaxBoard+1, -0.5, 64*gMaxBoard+0.5 );
0949     TH1D* hist1DCAEN_Max_HG     = new TH1D("hist1DCAEN_Max_HG_channels", "; 64x CAEN board + CAEN channel; Max_{mip} (HG ADC)", 64*gMaxBoard+1, -0.5, 64*gMaxBoard+0.5 );
0950     TH1D* hist1DCAEN_FWHM_HG    = new TH1D("hist1DCAEN_FWHM_HG_channels", "; 64x CAEN board + CAEN channel; FWHM_{mip} (HG ADC)", 64*gMaxBoard+1, -0.5, 64*gMaxBoard+0.5 );
0951     TH1D* hist1DCAEN_Width_HG   = new TH1D("hist1DCAEN_Width_HG_channels", "; 64x CAEN board + CAEN channel; Width_{mip} (HG ADC)", 64*gMaxBoard+1, -0.5, 64*gMaxBoard+0.5 );
0952     TH1D* hist1DCAEN_GWidth_HG  = new TH1D("hist1DCAEN_GWidth_HG_channels", "; 64x CAEN board + CAEN channel; Gauss Width_{mip} (HG ADC)", 64*gMaxBoard+1, -0.5, 64*gMaxBoard+0.5 );
0953     
0954     // 2D representation of fit values
0955     TH2D* hist2DMPV_HG          = new TH2D("hist2DMPV_HG_z_channel", "; channel; layer; MPV_{mip} (HG ADC)", 8, 0.5, 8.5, 14, -0.5, 13.5);
0956     TH2D* hist2DMPVErr_HG       = new TH2D("hist2DMPVErr_HG_z_channel", "; channel; layer; #Delta(MPV_{mip}) (HG ADC)", 8, 0.5, 8.5, 14, -0.5, 13.5);
0957     TH2D* hist2DMax_HG          = new TH2D("hist2DMax_HG_z_channel", "; channel; layer; Max_{mip} (HG ADC)", 8, 0.5, 8.5, 14, -0.5, 13.5);
0958     TH2D* hist2DFWHM_HG         = new TH2D("hist2DFWHM_HG_z_channel", "; channel; layer; FWHM_{mip} (HG ADC)", 8, 0.5, 8.5, 14, -0.5, 13.5);
0959     TH2D* hist2DWidth_HG        = new TH2D("hist2DWidth_HG_z_channel", "; channel; layer; Width_{mip} (HG ADC)", 8, 0.5, 8.5, 14, -0.5, 13.5);
0960     TH2D* hist2DWidthErr_HG     = new TH2D("hist2DWidthErr_HG_z_channel", "; channel; layer; #Delta(Width_{mip}) (HG ADC)", 8, 0.5, 8.5, 14, -0.5, 13.5);
0961     TH2D* hist2DGWidth_HG       = new TH2D("hist2DGWidth_HG_z_channel", "; channel; layer; Gauss Width_{mip} (HG ADC)", 8, 0.5, 8.5, 14, -0.5, 13.5);
0962     TH2D* hist2DGWidthErr_HG    = new TH2D("hist2DGWidthErr_HG_z_channel", "; channel; layer; #Delta(Gauss Width_{mip}) (HG ADC)", 8, 0.5, 8.5, 14, -0.5, 13.5);
0963     
0964     Double_t mpL[gMaxLayers][9]       = {{0.}};
0965     Double_t mpLErr[gMaxLayers][9]    = {{0.}};
0966     Double_t sigmaG[gMaxLayers][9]    = {{0.}};
0967     Double_t sigmaGErr[gMaxLayers][9] = {{0.}};
0968     Double_t sigmaL[gMaxLayers][9]    = {{0.}};
0969     Double_t sigmaLErr[gMaxLayers][9] = {{0.}};
0970     Double_t maxLandG[gMaxLayers][9]  = {{0.}};
0971     Double_t fwhmLandG[gMaxLayers][9] = {{0.}};
0972     double chisqr[gMaxLayers][9]      = {{0.}};
0973     int ndf[gMaxLayers][9]            = {{0}};
0974   
0975     // ********************************************************************************************************
0976     // Second loop over full tree to obtain noise subtracted histograms
0977     // ********************************************************************************************************
0978     TRandom3* rand    = new TRandom3();
0979     nEventsProcessed  = 0;
0980     for (Long64_t i=startEvent; i<nEntriesTree;i++) {
0981         // load current event
0982         tt_event->GetEntry(i);
0983         nEventsProcessed++;
0984 
0985         Int_t nChNoNoise = 0;
0986         // processing progress info
0987         if(i>0 && nEntriesTree>100 && i%(nEntriesTree/(20))==0) std::cout << "//processed " << 100*(i)/nEntriesTree << "%"  << std::endl;
0988         if(verbosity>1){
0989           std::cout << "***********************************************************************************************************" << std::endl;
0990           std::cout << "event " << i << std::endl;
0991           std::cout << "***********************************************************************************************************" << std::endl;
0992         }
0993         // temporary mapping of channels after noise subtractions
0994         Int_t signalNS[2][64][4] = {{{0}}};       // [board][channel][HG, LG, trigg HG,comb HG&LG]
0995         Double_t summed[2] = {0};                 // total channel sum
0996         // readEvent
0997         if (verbosity > 1)std::cout << "------------- Event -------------------------" << std::endl;
0998         if (verbosity > 1)std::cout << gTrID << "\t" << gTRtimeStamp << std::endl;
0999         if (i == startEvent) tstapMin = gTRtimeStamp;
1000         tstapMax = gTRtimeStamp;
1001         Double_t tCurr= (tstapMax-tstapMin)/1e6/60; // elapsed time in min
1002         
1003         for(Int_t ch=0; ch<gMaxChannels && ch < nChmapped; ch++){
1004           if (mapping[gChannel[ch]][0] == -1 && isTBdata > 0){
1005             continue;
1006           }
1007           Double_t meanNHG = 0;
1008           if (mean[0][gBoard[ch]][gChannel[ch]] != -1)
1009             meanNHG = mean[0][gBoard[ch]][gChannel[ch]];
1010           if (meanNHG > 70 || meanNHG < 30) meanNHG = 50;   // resetting to nominal
1011           Double_t meanNLG = 0;
1012           if (mean[2][gBoard[ch]][gChannel[ch]] != -1)
1013             meanNLG = mean[2][gBoard[ch]][gChannel[ch]];
1014           if (meanNLG > 70 || meanNLG < 30) meanNLG = 50;   // resetting to nominal
1015           
1016           signalNS[gBoard[ch]][gChannel[ch]][0] = gHG[ch]-meanNHG;
1017           signalNS[gBoard[ch]][gChannel[ch]][1] = gLG[ch]-meanNLG;
1018           // set combined HG & LG signal
1019           if (signalNS[gBoard[ch]][gChannel[ch]][0] < 3800)
1020             signalNS[gBoard[ch]][gChannel[ch]][3] = signalNS[gBoard[ch]][gChannel[ch]][0];
1021           else 
1022             signalNS[gBoard[ch]][gChannel[ch]][3] = signalNS[gBoard[ch]][gChannel[ch]][1] *cslope[0][gBoard[ch]][gChannel[ch]] + rand->Rndm()*cslope[0][gBoard[ch]][gChannel[ch]];
1023           Double_t chThres = scaledThr-meanNHG;
1024           if (gHG[ch] > chThres)  // fill potential trigger signals
1025             signalNS[gBoard[ch]][gChannel[ch]][2] = gHG[ch]-chThres;
1026           // fill histos
1027           histNSHG[gBoard[ch]][gChannel[ch]]->Fill(signalNS[gBoard[ch]][gChannel[ch]][0]); 
1028           histNSLG[gBoard[ch]][gChannel[ch]]->Fill(signalNS[gBoard[ch]][gChannel[ch]][1]); 
1029           histNSCombHG[gBoard[ch]][gChannel[ch]]->Fill(signalNS[gBoard[ch]][gChannel[ch]][3]); 
1030           histNSLGHG[gBoard[ch]][gChannel[ch]]->Fill(signalNS[gBoard[ch]][gChannel[ch]][1], signalNS[gBoard[ch]][gChannel[ch]][0]); 
1031           histNSHGLG[gBoard[ch]][gChannel[ch]]->Fill(signalNS[gBoard[ch]][gChannel[ch]][0], signalNS[gBoard[ch]][gChannel[ch]][1]); 
1032           if (signalNS[gBoard[ch]][gChannel[ch]][0] > (adcThreshold-meanNHG)){
1033             nChNoNoise++;
1034             if (verbosity > 1) std::cout << "not noise" << std::endl;
1035             if (isTBdata > 0){
1036               histNS3DMap->Fill(mapping[gChannel[ch]][0],mapping[gChannel[ch]][3],mapping[gChannel[ch]][2]);
1037               histNS2DMap->Fill(mapping[gChannel[ch]][1],mapping[gChannel[ch]][0]);
1038               histNS1DMap->Fill(mapping[gChannel[ch]][1]);
1039             }
1040           }
1041           // calculate summed signal
1042           summed[gBoard[ch]] = summed[gBoard[ch]]+signalNS[gBoard[ch]][gChannel[ch]][3];
1043         }
1044         // fill summed signal
1045         for (Int_t j = 0; j < gMaxBoard; j++)
1046           hist_T_NSCombHG[j]->Fill(tCurr,summed[j]);
1047         
1048         // *****************************************************************
1049         // check for conincidence between first and last channel 
1050         // above noise or pure noise triggers
1051         // *****************************************************************
1052         if (isTBdata == 1 || isTBdata == 2){
1053         // only for first CAEN board!!!!!
1054           for (Int_t chCAEN = 0; chCAEN < 64; chCAEN++){         
1055             if (mapping[chCAEN][0] == -1 ) continue;
1056             Int_t chBoard = mapping[chCAEN][1];
1057             Int_t nNeigh  = 0;
1058             Int_t* neighborCh = new Int_t[5];
1059             GetNeighborWithinBoard(chBoard, nNeigh, neighborCh);
1060             if (verbosity > 2)std::cout << "channel in board "<< chBoard <<" found " << nNeigh << " neighbors " << std::endl;
1061           
1062             Int_t chCAENFront = backwardMapping[chBoard][minActiveLayer];
1063             Int_t chCAENBack  = backwardMapping[chBoard][maxActiveLayer];
1064             
1065             // Determine straight line trigger
1066             Bool_t  trigg         = (signalNS[0][chCAENFront][2] > 0 && signalNS[0][chCAENBack][2] > 0);
1067             Float_t triggSignal   = (signalNS[0][chCAENFront][2] + signalNS[0][chCAENBack][2])/2;
1068             
1069             // Determine noise trigger
1070             Bool_t triggNoise     = kFALSE;
1071             Float_t triggSigFN     = signalNS[0][chCAENFront][2];
1072             Float_t triggSigBN     = signalNS[0][chCAENBack][2];
1073             for (Int_t n = 0; n < nNeigh; n++){
1074                 triggSigFN = triggSigFN + signalNS[0][backwardMapping[neighborCh[n]][minActiveLayer]][2];
1075                 triggSigBN = triggSigBN + signalNS[0][backwardMapping[neighborCh[n]][maxActiveLayer]][2];
1076             }
1077             if (triggSigFN == 0 && triggSigBN == 0)
1078               triggNoise  = kTRUE;
1079             
1080             // Fill triggered histograms
1081             if (verbosity > 1) std::cout << "channel board " << chCAEN << "\t" << chBoard << "\t" << chCAENFront << "\t" << chCAENBack << ""<< std::endl; 
1082             if (trigg){
1083               histNSHGTrig[0][chCAEN]->Fill(signalNS[0][chCAEN][0]); 
1084               histNSCombHGTrig[0][chCAEN]->Fill(signalNS[0][chCAEN][3]); 
1085               histNSLGTrig[0][chCAEN]->Fill(signalNS[0][chCAEN][1]); 
1086               if (signalNS[0][chCAEN][0] > 0) hist2DNSTrigg->Fill(mapping[chCAEN][1],mapping[chCAEN][0]);
1087             } else {
1088               histNSHGNoise[0][chCAEN]->Fill(signalNS[0][chCAEN][0]); 
1089               histNSLGNoise[0][chCAEN]->Fill(signalNS[0][chCAEN][1]);               
1090             }
1091             if (triggNoise){
1092               histNSHGTriggNoise[0][chCAEN]->Fill(signalNS[0][chCAEN][0]); 
1093               histNSLGTriggNoise[0][chCAEN]->Fill(signalNS[0][chCAEN][1]);       
1094               hist2DNSTriggNoise->Fill(mapping[chCAEN][1],mapping[chCAEN][0]);
1095             }
1096           }
1097         }
1098         if (verbosity > 1)std::cout << "Channels above noise: "<< nChNoNoise << std::endl;        
1099     }
1100     std::cout << "Total events processed 2nd round:" << nEventsProcessed << std::endl;
1101 
1102     
1103     // ********************************************************************************************************
1104     // Plotting of single channels after noise subtraction
1105     // -> remapping of CAEN board+channels to physicsal layers and readout-channels
1106     // ********************************************************************************************************    
1107     for (Int_t j = 0; j < gMaxBoard; j++){
1108       for (Int_t i = 0; i < gMaxChannels; i++){
1109         Int_t chMap     = j*64 + i;
1110         if (mapping[chMap][0] == -1 && isTBdata > 0) continue;
1111         Int_t chBoard   = mapping[chMap][1];
1112         Int_t layer     = mapping[chMap][0];
1113 //         if (verbosity > 0)
1114           std::cout << j << "\t" << i << "\t" << chMap << "\t L: " << layer  << "\t C:" <<  chBoard << std::endl;
1115         
1116           
1117           
1118         histNSHG_mapped[layer][chBoard] = (TH1D*)histNSHG[j][i]->Clone(Form("h_NS_HG_mapped_L%d_C%02d",layer,chBoard));
1119         histNSCombHG_mapped[layer][chBoard] = (TH1D*)histNSCombHG[j][i]->Clone(Form("h_NS_CombHG_mapped_L%d_C%02d",layer,chBoard));
1120         histNSLG_mapped[layer][chBoard] = (TH1D*)histNSLG[j][i]->Clone(Form("h_NS_LG_mapped_L%d_C%02d",layer,chBoard));
1121         if (histNSHGTrig[j][i]) histNSHGTrig_mapped[layer][chBoard] = (TH1D*)histNSHGTrig[j][i]->Clone(Form("h_NS_HGTrigg_mapped_L%d_C%02d",layer,chBoard));
1122         if (histNSCombHGTrig[j][i]) histNSCombHGTrig_mapped[layer][chBoard] = (TH1D*)histNSCombHGTrig[j][i]->Clone(Form("h_NS_CombHGTrigg_mapped_L%d_C%02d",layer,chBoard));
1123         if (histNSLGTrig[j][i]) histNSLGTrig_mapped[layer][chBoard] = (TH1D*)histNSLGTrig[j][i]->Clone(Form("h_NS_LGTrigg_mapped_L%d_C%02d",layer,chBoard));
1124         if (histNSHGTriggNoise[j][i]) histNSHGTriggNoise_mapped[layer][chBoard] = (TH1D*)histNSHGTriggNoise[j][i]->Clone(Form("h_NS_HGTriggeredNoise_mapped_L%d_C%02d",layer,chBoard));
1125         if (histNSLGTriggNoise[j][i]) histNSLGTriggNoise_mapped[layer][chBoard] = (TH1D*)histNSLGTriggNoise[j][i]->Clone(Form("h_NS_LGTriggeredNoise_mapped_L%d_C%02d",layer,chBoard));
1126         
1127         Bool_t fTrigg   = kFALSE;
1128         histNSHG_mapped[layer][chBoard]->Sumw2();
1129         histNSHG_mappedReb[layer][chBoard] = (TH1D*)histNSHG_mapped[layer][chBoard]->Rebin(2100,Form("%sReb",(histNSHG_mapped[layer][chBoard]->GetName())), binningADC);
1130         histNSHG_mappedReb[layer][chBoard]->Scale(1,"width");
1131         if (histNSHGTrig[j][i]){
1132           fTrigg    = kTRUE;
1133           histNSHGTrig_mapped[layer][chBoard]->Sumw2();
1134           histNSHGTrig_mappedReb[layer][chBoard] = (TH1D*)histNSHGTrig_mapped[layer][chBoard]->Rebin(2100,Form("%sReb",(histNSHGTrig_mapped[layer][chBoard]->GetName())), binningADC);
1135           histNSHGTrig_mappedReb[layer][chBoard]->Scale(1,"width");
1136         }
1137         histNSCombHG_mapped[layer][chBoard]->Sumw2();
1138         histNSCombHG_mappedReb[layer][chBoard] = (TH1D*)histNSCombHG_mapped[layer][chBoard]->Rebin(2780,Form("%sReb",(histNSCombHG_mapped[layer][chBoard]->GetName())), binningADC);
1139         histNSCombHG_mappedReb[layer][chBoard]->Scale(1,"width");
1140         if (histNSCombHGTrig[j][i]){
1141           histNSCombHGTrig_mapped[layer][chBoard]->Sumw2();
1142           histNSCombHGTrig_mappedReb[layer][chBoard] = (TH1D*)histNSCombHGTrig_mapped[layer][chBoard]->Rebin(2780,Form("%sReb",(histNSCombHGTrig_mapped[layer][chBoard]->GetName())), binningADC);
1143           histNSCombHGTrig_mappedReb[layer][chBoard]->Scale(1,"width");
1144         }
1145         
1146         if (fTrigg && histNSHGTrig_mappedReb[layer][chBoard]->GetEntries() > 0){
1147           // Setting fit range and start values
1148           double fr[2]    = {150, 2000};
1149           double sv[4]    = {50, 600, 5000, 5.0};
1150           double pllo[4]  = {0.5, 100, 1.0, 0.4};
1151           double plhi[4]  = {500, 1000, 10000, 10};
1152           double fp[4];
1153           double fpe[4];
1154           
1155           if (verbosity > 0) std::cout << "\t Int: " << histNSHGTrig_mapped[layer][chBoard]->Integral() << "\t" << histNSHGTrig_mapped[layer][chBoard]->GetRMS() << std::endl;
1156           if (histNSHGTrig_mapped[layer][chBoard]->Integral() < 5000){
1157             histNSHGTrig_mapped[layer][chBoard]->Sumw2();
1158             histNSHGTrig_mapped[layer][chBoard]->Rebin(2);
1159             histNSHGTrig_mapped[layer][chBoard]->Scale(1,"width");          
1160             histNSHGTrig_mappedReb[layer][chBoard]->Sumw2();
1161             histNSHGTrig_mappedReb[layer][chBoard]->Rebin(2);
1162             histNSHGTrig_mappedReb[layer][chBoard]->Scale(1./2);          
1163             
1164             histNSHGTrig[j][i]->Sumw2();
1165             histNSHGTrig[j][i]->Rebin(2);
1166             histNSHGTrig[j][i]->Scale(1,"width");          
1167           }
1168             
1169           plhi[2]=histNSHGTrig_mapped[layer][chBoard]->Integral()*2;
1170           plhi[3]=histNSHGTrig_mapped[layer][chBoard]->GetRMS()*2;
1171           sv[2]=histNSHGTrig_mapped[layer][chBoard]->Integral(); 
1172 
1173           fitLandauG_NSHGTrig_mapped[layer][chBoard]  = langaufit(histNSHGTrig_mappedReb[layer][chBoard],fr,sv,pllo,plhi,fp,fpe,&chisqr[layer][chBoard],&ndf[layer][chBoard], verbosity);
1174           fitLandauG_NSHGTrig_mapped[layer][chBoard]->SetName(Form("f_LandauG_NSHG_L%d_C%02d",layer,chBoard));
1175           fitLandauG_NSHGTrig[j][i]                   = (TF1*)fitLandauG_NSHGTrig_mapped[layer][chBoard]->Clone(Form("f_LandauG_NSHG_B%d_C%02d",j,i));
1176           
1177           mpL[layer][chBoard]           = fitLandauG_NSHGTrig_mapped[layer][chBoard]->GetParameter(1);
1178           mpLErr[layer][chBoard]        = fitLandauG_NSHGTrig_mapped[layer][chBoard]->GetParError(1);
1179           sigmaG[layer][chBoard]        = fitLandauG_NSHGTrig_mapped[layer][chBoard]->GetParameter(3);
1180           sigmaGErr[layer][chBoard]     = fitLandauG_NSHGTrig_mapped[layer][chBoard]->GetParError(3);
1181           sigmaL[layer][chBoard]        = fitLandauG_NSHGTrig_mapped[layer][chBoard]->GetParameter(0);
1182           sigmaLErr[layer][chBoard]     = fitLandauG_NSHGTrig_mapped[layer][chBoard]->GetParError(0);
1183           
1184           hist2DMPV_HG->Fill(chBoard, layer,fitLandauG_NSHGTrig_mapped[layer][chBoard]->GetParameter(1));
1185           hist2DMPVErr_HG->Fill(chBoard, layer,fitLandauG_NSHGTrig_mapped[layer][chBoard]->GetParError(1));
1186           hist2DWidth_HG->Fill(chBoard, layer,fitLandauG_NSHGTrig_mapped[layer][chBoard]->GetParameter(0));
1187           hist2DWidthErr_HG->Fill(chBoard, layer,fitLandauG_NSHGTrig_mapped[layer][chBoard]->GetParError(0));
1188           hist2DGWidth_HG->Fill(chBoard, layer,fitLandauG_NSHGTrig_mapped[layer][chBoard]->GetParameter(3));
1189           hist2DGWidthErr_HG->Fill(chBoard, layer,fitLandauG_NSHGTrig_mapped[layer][chBoard]->GetParError(3));
1190           
1191           /// get bin to fill for 1D representation of channels for fit values
1192           Int_t channelBin1D = hist1DMPV_HG->FindBin(layer*10+chBoard);          
1193           hist1DMPV_HG->SetBinContent(channelBin1D, fitLandauG_NSHGTrig_mapped[layer][chBoard]->GetParameter(1));
1194           hist1DMPV_HG->SetBinError(channelBin1D, fitLandauG_NSHGTrig_mapped[layer][chBoard]->GetParError(1));
1195           hist1DWidth_HG->SetBinContent(channelBin1D, fitLandauG_NSHGTrig_mapped[layer][chBoard]->GetParameter(0));
1196           hist1DWidth_HG->SetBinError(channelBin1D, fitLandauG_NSHGTrig_mapped[layer][chBoard]->GetParError(0));
1197           hist1DGWidth_HG->SetBinContent(channelBin1D, fitLandauG_NSHGTrig_mapped[layer][chBoard]->GetParameter(3));
1198           hist1DGWidth_HG->SetBinError(channelBin1D, fitLandauG_NSHGTrig_mapped[layer][chBoard]->GetParError(3));
1199           
1200           hist1DCAEN_MPV_HG->SetBinContent(chMap+1, fitLandauG_NSHGTrig_mapped[layer][chBoard]->GetParameter(1));
1201           hist1DCAEN_MPV_HG->SetBinError(chMap+1, fitLandauG_NSHGTrig_mapped[layer][chBoard]->GetParError(1));
1202           hist1DCAEN_Width_HG->SetBinContent(chMap+1, fitLandauG_NSHGTrig_mapped[layer][chBoard]->GetParameter(0));
1203           hist1DCAEN_Width_HG->SetBinError(chMap+1, fitLandauG_NSHGTrig_mapped[layer][chBoard]->GetParError(0));
1204           hist1DCAEN_GWidth_HG->SetBinContent(chMap+1, fitLandauG_NSHGTrig_mapped[layer][chBoard]->GetParameter(3));
1205           hist1DCAEN_GWidth_HG->SetBinError(chMap+1, fitLandauG_NSHGTrig_mapped[layer][chBoard]->GetParError(3));
1206           
1207           double SNRPeak, SNRFWHM;
1208           langaupro(fp,SNRPeak,SNRFWHM);
1209 
1210           maxLandG[layer][chBoard]      = (Double_t)SNRPeak;
1211           fwhmLandG[layer][chBoard]     = (Double_t)SNRFWHM;
1212           
1213           hist2DMax_HG->Fill(chBoard, layer,SNRPeak);
1214           hist2DFWHM_HG->Fill(chBoard, layer,SNRFWHM);
1215           hist1DMax_HG->SetBinContent(channelBin1D, SNRPeak);
1216           hist1DFWHM_HG->SetBinContent(channelBin1D, SNRFWHM);
1217           hist1DCAEN_Max_HG->SetBinContent(chMap+1, SNRPeak);
1218           hist1DCAEN_FWHM_HG->SetBinContent(chMap+1, SNRFWHM);
1219         } else {
1220           fitLandauG_NSHGTrig_mapped[layer][chBoard]  = nullptr;
1221           fitLandauG_NSHGTrig[j][i]                   = nullptr;
1222         }
1223 
1224         if (bDetPlot){
1225           // ********************************************************
1226           // Plot different triggers together
1227           // ********************{************************************
1228           PlotOverlayDiffTriggers( canvas1DDiffTrigg, histNSHG[j][i], histNSHGTrig[j][i], histNSHGTriggNoise[j][i], NULL,
1229                                 -100, 4000, Form("%s/HG_NS_DiffTriggers", outputDirPlotsDet.Data()),
1230                                 j, i, layer, chBoard, currentRunInfo, 0.04);
1231           PlotOverlayDiffTriggers( canvas1DDiffTrigg, histNSLG[j][i], histNSLGTrig[j][i], histNSLGTriggNoise[j][i], NULL,
1232                                 -100, 4000, Form("%s/LG_NS_DiffTriggers", outputDirPlotsDet.Data()),
1233                                 j, i, layer, chBoard, currentRunInfo, 0.04);
1234           
1235         }
1236       }
1237     }
1238     // ********************************************************************************************************
1239     // Plotting summed signals vs time per board
1240     // ********************************************************************************************************
1241     for (Int_t j = 0; j < gMaxBoard; j++){
1242       if (hist_T_NSCombHG[j] && bDetPlot){
1243         canvas2DCorr->cd();
1244         DefaultCancasSettings( canvas2DCorr, 0.1, 0.1, 0.04, 0.08);  
1245           SetStyleHistoTH2ForGraphs( hist_T_NSCombHG[j], hist_T_NSCombHG[j]->GetXaxis()->GetTitle(), hist_T_NSCombHG[j]->GetYaxis()->GetTitle(), 0.85*textSizeRel, textSizeRel, 0.85*textSizeRel, textSizeRel,0.9, 1.25);  
1246           hist_T_NSCombHG[j]->GetXaxis()->SetRangeUser(0,tdiff+0.1);
1247           hist_T_NSCombHG[j]->Draw("colz");
1248           
1249           TLatex *labelChannel                     = new TLatex(0.85,0.90,Form("CAEN: B %d",j));
1250           SetStyleTLatex( labelChannel, textSizeRel,4,1,42,kTRUE,31);
1251           labelChannel->Draw();
1252 
1253         canvas2DCorr->SaveAs(Form("%s/T_HGSummed_B%d.pdf", outputDirPlots.Data(), j));
1254       }
1255     }
1256     canvas2DCorr->cd();
1257     DefaultCancasSettings( canvas2DCorr, 0.1, 0.1, 0.02, 0.08);  
1258       SetStyleHistoTH2ForGraphs( hist2DNSTrigg, hist2DNSTrigg->GetXaxis()->GetTitle(), hist2DNSTrigg->GetYaxis()->GetTitle(), 0.85*textSizeRel, textSizeRel, 0.85*textSizeRel, textSizeRel,0.9, 1.25);  
1259       hist2DNSTrigg->GetYaxis()->SetRangeUser(-0.5,maxActiveLayer+1.5);
1260       hist2DNSTrigg->GetZaxis()->SetRangeUser(hist2DNSTrigg->GetMinimum(1),hist2DNSTrigg->GetMaximum());
1261       hist2DNSTrigg->Draw("colz,text");
1262       
1263       TLatex *labelTrigg                     = new TLatex(0.85,0.93,"straight line triggers");
1264       SetStyleTLatex( labelTrigg, textSizeRel,4,1,42,kTRUE,31);
1265       labelTrigg->Draw();
1266 
1267     canvas2DCorr->SaveAs(Form("%s/NTrigg_2D.pdf", outputDirPlots.Data()));
1268 
1269     canvas2DCorr->cd();
1270     DefaultCancasSettings( canvas2DCorr, 0.1, 0.1, 0.02, 0.08);  
1271       SetStyleHistoTH2ForGraphs( hist2DNSTriggNoise, hist2DNSTriggNoise->GetXaxis()->GetTitle(), hist2DNSTriggNoise->GetYaxis()->GetTitle(), 0.85*textSizeRel, textSizeRel, 0.85*textSizeRel, textSizeRel,0.9, 1.25);  
1272       hist2DNSTriggNoise->GetYaxis()->SetRangeUser(-0.5,maxActiveLayer+1.5);
1273       hist2DNSTriggNoise->GetZaxis()->SetRangeUser(hist2DNSTriggNoise->GetMinimum(1),hist2DNSTriggNoise->GetMaximum());
1274       hist2DNSTriggNoise->Draw("colz,text");
1275       
1276       labelTrigg->SetText(0.85,0.93,"noise triggers");
1277       labelTrigg->Draw();
1278 
1279     canvas2DCorr->SaveAs(Form("%s/NTriggNoise_2D.pdf", outputDirPlots.Data()));
1280  
1281     TH2D* hist2DNSTriggEffi = (TH2D*)hist2DNSTrigg->Clone("hist2DNSTrigg_effi");
1282     hist2DNSTriggEffi->Reset();
1283     hist2DNSTriggEffi->GetZaxis()->UnZoom();
1284     for (Int_t c = 1; c < 9; c++){
1285       for (Int_t l = 1; l < gMaxLayers+1; l++){
1286         Double_t content = (Double_t)hist2DNSTrigg->GetBinContent(c,l)/hist2DNSTrigg->GetBinContent(c,1)*100;
1287         hist2DNSTriggEffi->Fill(c,l-1,content );
1288       }        
1289     }
1290     canvas2DCorr->cd();
1291     canvas2DCorr->SetLogz(0);
1292 
1293     DefaultCancasSettings( canvas2DCorr, 0.08, 0.13, 0.02, 0.08);  
1294       SetStyleHistoTH2ForGraphs( hist2DNSTriggEffi, hist2DNSTriggEffi->GetXaxis()->GetTitle(), hist2DNSTriggEffi->GetYaxis()->GetTitle(), 0.85*textSizeRel, textSizeRel, 0.85*textSizeRel, textSizeRel,0.9, 0.9);  
1295       hist2DNSTriggEffi->GetZaxis()->SetTitle("#varepsilon (%)");
1296       hist2DNSTriggEffi->GetYaxis()->SetRangeUser(-0.5,maxActiveLayer+1.5);
1297       hist2DNSTriggEffi->GetZaxis()->SetRangeUser(hist2DNSTriggEffi->GetMinimum(0.5),hist2DNSTriggEffi->GetMaximum());
1298       if (verbosity > 0)std::cout << hist2DNSTriggEffi->GetMinimum(1) << "\t" << hist2DNSTriggEffi->GetMaximum() << std::endl;
1299       hist2DNSTriggEffi->Draw("colz,text");
1300       
1301       labelTrigg->SetText(0.8,0.93,"rec. effi");
1302       SetStyleTLatex( labelTrigg, textSizeRel,4,1,42,kTRUE,31);
1303       labelTrigg->Draw();
1304 
1305     canvas2DCorr->SaveAs(Form("%s/NTriggEff_2D.pdf", outputDirPlots.Data()));
1306 
1307     PlotSimpleMultiLayer2D( canvas2DCorr, hist2DNoiseMean_HG, maxActiveLayer, maxActiveRBCh, textSizeRel, Form("%s/HG_Noise_Mean.pdf", outputDirPlots.Data()), currentRunInfo);
1308     PlotSimpleMultiLayer2D( canvas2DCorr, hist2DNoiseSigma_HG, maxActiveLayer, maxActiveRBCh, textSizeRel,Form("%s/HG_Noise_Sigma.pdf", outputDirPlots.Data()), currentRunInfo);
1309     PlotSimpleMultiLayer2D( canvas2DCorr, hist2DNoiseMean_LG, maxActiveLayer, maxActiveRBCh, textSizeRel,Form("%s/LG_Noise_Mean.pdf", outputDirPlots.Data()), currentRunInfo);
1310     PlotSimpleMultiLayer2D( canvas2DCorr, hist2DNoiseSigma_LG, maxActiveLayer, maxActiveRBCh, textSizeRel, Form("%s/LG_Noise_Sigma.pdf", outputDirPlots.Data()), currentRunInfo);
1311     PlotSimpleMultiLayer2D( canvas2DCorr, hist2DLGHG_slope, maxActiveLayer, maxActiveRBCh, textSizeRel, Form("%s/LGHG_Slope.pdf", outputDirPlots.Data()), currentRunInfo);
1312     PlotSimpleMultiLayer2D( canvas2DCorr, hist2DLGHG_offset, maxActiveLayer, maxActiveRBCh, textSizeRel, Form("%s/LGHG_Offset.pdf", outputDirPlots.Data()), currentRunInfo, kTRUE);
1313 
1314     for (Int_t l = 0; l < gMaxLayers; l++){
1315       if (!lActive[l]) continue; 
1316       if (isTBdata == 0){
1317         PlotNoiseWithFitsFullLayer (canvas8Panel,pad8Panel, topRCornerX, topRCornerY, relSize8P, textSizePixel, 
1318                                     histNSHG_mapped[l], nullptr, -120, 375, 1.2, l,
1319                                     Form("%s/Noise_NS_HG_Layer%02d.pdf" ,outputDirPlots.Data(), l), currentRunInfo);
1320         PlotDiffTriggersFullLayer (canvas8Panel,pad8Panel, topRCornerX, topRCornerY, relSize8P, textSizePixel, 
1321                                   histNSHG_mapped[l], nullptr, nullptr, nullptr, 
1322                                   -80, 3800, 1.2, l , Form("%s/TriggerOverlay_HG_NS_Layer%02d.pdf" ,outputDirPlots.Data(), l), currentRunInfo);
1323         PlotDiffTriggersFullLayer (canvas8Panel,pad8Panel, topRCornerX, topRCornerY, relSize8P, textSizePixel, 
1324                                   histNSLG_mapped[l], nullptr, nullptr, nullptr, 
1325                                   -80, 3800, 1.2, l , Form("%s/TriggerOverlay_LG_NS_Layer%02d.pdf" ,outputDirPlots.Data(), l), currentRunInfo);
1326       } else if (isTBdata == 1){
1327         PlotNoiseWithFitsFullLayer (canvas8Panel,pad8Panel, topRCornerX, topRCornerY, relSize8P, textSizePixel, 
1328                                     histNSHGTriggNoise_mapped[l], nullptr, -120, 375, 1.2, l,
1329                                     Form("%s/Noise_NS_HG_Layer%02d.pdf" ,outputDirPlots.Data(), l), currentRunInfo);
1330         PlotDiffTriggersFullLayer (canvas8Panel,pad8Panel, topRCornerX, topRCornerY, relSize8P, textSizePixel, 
1331                                   histNSHG_mapped[l], histNSHGTrig_mapped[l], histNSHGTriggNoise_mapped[l], nullptr, 
1332                                   -80, 3800, 1.2, l , Form("%s/TriggerOverlay_HG_NS_Layer%02d.pdf" ,outputDirPlots.Data(), l), currentRunInfo);
1333         PlotDiffTriggersFullLayer (canvas8Panel,pad8Panel, topRCornerX, topRCornerY, relSize8P, textSizePixel, 
1334                                   histNSLG_mapped[l], histNSLGTrig_mapped[l], histNSLGTriggNoise_mapped[l], nullptr, 
1335                                   -80, 3800, 1.2, l , Form("%s/TriggerOverlay_LG_NS_Layer%02d.pdf" ,outputDirPlots.Data(), l), currentRunInfo);
1336         
1337       }
1338     }
1339 
1340     // ********************************************************************************************************
1341     // Overlay noise in same layer
1342     // ********************************************************************************************************
1343     for (Int_t l = 0; l < gMaxLayers; l++){
1344       if (!lActive[l]) continue;      
1345       for (Int_t c = 1; c < 9; c++){
1346         PlotMIPSingle (canvas1DNoise, histNSHGTrig_mapped[l][c],fitLandauG_NSHGTrig_mapped[l][c], chisqr[l][c], ndf[l][c], maxLandG[l][c], fwhmLandG[l][c],
1347                         l, c, Form("%s/HG_TriggMipWithFit", outputDirPlotsDet.Data()), currentRunInfo, 0.04);
1348       }
1349       if (isTBdata == 1){
1350         PlotChannelOverlaySameLayer( canvas1DDiffTrigg, histNSHGTriggNoise_mapped[l], 1, 9,
1351                                     -100, 400, 1, Form("%s/HGNS_Noise", outputDirPlots.Data()), l, currentRunInfo, 0.04);
1352         PlotChannelOverlaySameLayer( canvas1DDiffTrigg, histNSHGTrig_mappedReb[l], 1, 9,
1353                                     0, 4000, 1./5, Form("%s/HGNS_Trigg", outputDirPlots.Data()), l, currentRunInfo, 0.04,"hist");
1354         PlotChannelOverlaySameLayer( canvas1DDiffTrigg, histNSHGTrig_mappedReb[l], 1, 9,
1355                                     0, 2000, 1./2, Form("%s/HGNS_TriggZoomed", outputDirPlots.Data()), l, currentRunInfo, 0.04,"hist");
1356         PlotChannelOverlaySameLayer( canvas1DDiffTrigg, histNSHG_mappedReb[l], 1, 9,
1357                                     -100, 4000, 1./5, Form("%s/HGNS", outputDirPlots.Data()), l, currentRunInfo, 0.04,"hist");
1358         PlotChannelOverlaySameLayer( canvas1DDiffTrigg, histNSLGTriggNoise_mapped[l], 1, 9,
1359                                     -100, 400, 1, Form("%s/LGNS_Noise", outputDirPlots.Data()), l, currentRunInfo, 0.04);
1360         PlotChannelOverlaySameLayer( canvas1DDiffTrigg, histNSLGTrig_mapped[l], 1, 9,
1361                                     0, 4000, 1, Form("%s/LGNS_Trigg", outputDirPlots.Data()), l, currentRunInfo,0.04);
1362         PlotChannelOverlaySameLayer( canvas1DDiffTrigg, histNSLG_mapped[l], 1, 9,
1363                                     -100, 4000, 1, Form("%s/LGNS", outputDirPlots.Data()), l, currentRunInfo, 0.04);
1364         PlotChannelOverlaySameLayer( canvas1DDiffTrigg, histNSCombHGTrig_mappedReb[l], 1, 9,
1365                                     0, 40000, 1./100, Form("%s/CombHGNS_Trigg", outputDirPlots.Data()), l, currentRunInfo, 0.04,"hist");
1366         PlotChannelOverlaySameLayer( canvas1DDiffTrigg, histNSCombHG_mappedReb[l], 1, 9,
1367                                     -100, 40000, 1./100, Form("%s/CombHGNS", outputDirPlots.Data()), l, currentRunInfo, 0.04,"hist");
1368         PlotStraigtLineTriggAndFitFullLayer (canvas8Panel,pad8Panel, topRCornerX, topRCornerY, relSize8P, textSizePixel, 
1369                                             histNSHGTrig_mapped[l], fitLandauG_NSHGTrig_mapped[l],maxLandG[l],
1370                                             150, 2200, 0, 2200, 1.2, l, Form("%s/TriggerWithFit_HG_NS_Layer%02d.pdf" ,outputDirPlots.Data(), l), currentRunInfo);
1371         PlotStraigtLineTriggAndFitFullLayer (canvas8Panel,pad8Panel, topRCornerX, topRCornerY, relSize8P, textSizePixel, 
1372                                             histNSHGTrig_mappedReb[l], fitLandauG_NSHGTrig_mapped[l],maxLandG[l],
1373                                             150, 2200, 0, 2200, 1.2, l, Form("%s/TriggerWithFit_HG_NSReb_Layer%02d.pdf" ,outputDirPlots.Data(), l), currentRunInfo);
1374         PlotStraigtLineTriggAndFitFullLayerLin (canvas8Panel,pad8Panel, topRCornerX, topRCornerY, relSize8P, textSizePixel, 
1375                                               histNSHGTrig_mappedReb[l], fitLandauG_NSHGTrig_mapped[l],maxLandG[l],
1376                                               150, 2200, 0, 2200, 1.05, l, Form("%s/TriggerWithFitLinY_HG_NS_Layer%02d.pdf" ,outputDirPlots.Data(), l), currentRunInfo);
1377       } else {
1378         PlotChannelOverlaySameLayer( canvas1DDiffTrigg, histNSHG_mappedReb[l], 1, 9,
1379                                     -100, 4000, 1./5, Form("%s/HGNS", outputDirPlots.Data()), l, currentRunInfo, 0.04,"hist");
1380         PlotChannelOverlaySameLayer( canvas1DDiffTrigg, histNSLG_mapped[l], 1, 9,
1381                                     -100, 4000, 1, Form("%s/LGNS", outputDirPlots.Data()), l, currentRunInfo, 0.04);        
1382         PlotChannelOverlaySameLayer( canvas1DDiffTrigg, histNSCombHG_mappedReb[l], 1, 9,
1383                                     -100, 40000, 1./100, Form("%s/CombHGNS", outputDirPlots.Data()), l, currentRunInfo, 0.04,"hist");
1384       }
1385     }
1386     // ********************************************************************************************************
1387     // Overlay noise for same readout channel
1388     // ********************************************************************************************************
1389     for (Int_t c = 1; c < 9; c++){      
1390       if (isTBdata == 2 && c > 1) continue; 
1391       PlotChannelOverlaySameReadoutChannel( canvas1DDiffTrigg, histNSHG_mappedReb, lActive, 0, gMaxLayers, 
1392                                             -100,4000, 1./5, Form("%s/HGNS", outputDirPlots.Data()), c, currentRunInfo,0.04,"hist");    
1393       PlotChannelOverlaySameReadoutChannel( canvas1DDiffTrigg, histNSCombHG_mappedReb, lActive, 0, gMaxLayers, 
1394                                             -100,40000, 1./100, Form("%s/CombHGNS", outputDirPlots.Data()), c, currentRunInfo, 0.04, "hist");    
1395       if (isTBdata == 1){
1396         PlotChannelOverlaySameReadoutChannel( canvas1DDiffTrigg, histNSHGTriggNoise_mapped, lActive, 0, gMaxLayers, 
1397                                               -200,400, 1, Form("%s/HGNS_Noise", outputDirPlots.Data()), c, currentRunInfo, 0.04);    
1398         PlotChannelOverlaySameReadoutChannel( canvas1DDiffTrigg, histNSHGTrig_mappedReb, lActive, 0, gMaxLayers, 
1399                                               0,4000, 1./5, Form("%s/HGNS_Trigg", outputDirPlots.Data()), c, currentRunInfo, 0.04,"hist");    
1400         PlotChannelOverlaySameReadoutChannel( canvas1DDiffTrigg, histNSHGTrig_mappedReb, lActive, 0, gMaxLayers, 
1401                                               0,2000, 1./2, Form("%s/HGNS_TriggZoomed", outputDirPlots.Data()), c, currentRunInfo, 0.04, "hist");    
1402         PlotChannelOverlaySameReadoutChannel( canvas1DDiffTrigg, histNSCombHGTrig_mappedReb, lActive, 0, gMaxLayers, 
1403                                               0,40000, 1./100, Form("%s/CombHGNS_Trigg", outputDirPlots.Data()), c, currentRunInfo, 0.04,"hist");    
1404       }
1405     }
1406     if (isTBdata == 1){
1407       PlotOverlayFullLayer ( canvas8Panel, pad8Panel, topRCornerX, topRCornerY, relSize8P, textSizePixel, 
1408                               histNSHGTriggNoise_mapped, lActive, 0, gMaxLayers,
1409                               -100, 350, -125, 425, 1.5, Form("%s/HGNS_Noise_AllLayers.pdf", outputDirPlots.Data()), currentRunInfo, "pe");
1410     }
1411     if (isTBdata == 1){
1412       PlotOverlayFullLayer ( canvas8Panel, pad8Panel, topRCornerX, topRCornerY, relSize8P, textSizePixel, 
1413                               histNSHGTrig_mappedReb, lActive, 0, gMaxLayers,
1414                               200, 2200, -10, 2200, 1.5, Form("%s/HGNS_Trigg_AllLayers.pdf", outputDirPlots.Data()), currentRunInfo, "hist");
1415     }
1416     PlotSimpleMultiLayer2D( canvas2DCorr, hist2DMPV_HG, maxActiveLayer, maxActiveRBCh, textSizeRel, Form("%s/HGTrigg_MPV.pdf", outputDirPlots.Data()), currentRunInfo);
1417     PlotSimpleMultiLayer2D( canvas2DCorr, hist2DMPVErr_HG, maxActiveLayer, maxActiveRBCh, textSizeRel, Form("%s/HGTrigg_MPVErr.pdf", outputDirPlots.Data()), currentRunInfo);
1418     PlotSimpleMultiLayer2D( canvas2DCorr, hist2DMax_HG, maxActiveLayer, maxActiveRBCh, textSizeRel, Form("%s/HGTrigg_Max.pdf", outputDirPlots.Data()), currentRunInfo);
1419     PlotSimpleMultiLayer2D( canvas2DCorr, hist2DFWHM_HG, maxActiveLayer, maxActiveRBCh, textSizeRel, Form("%s/HGTrigg_FWHM.pdf", outputDirPlots.Data()), currentRunInfo);
1420     PlotSimpleMultiLayer2D( canvas2DCorr, hist2DWidth_HG, maxActiveLayer, maxActiveRBCh, textSizeRel, Form("%s/HGTrigg_Width.pdf", outputDirPlots.Data()), currentRunInfo);
1421     PlotSimpleMultiLayer2D( canvas2DCorr, hist2DWidthErr_HG, maxActiveLayer, maxActiveRBCh, textSizeRel, Form("%s/HGTrigg_WidthErr.pdf", outputDirPlots.Data()), currentRunInfo);
1422     PlotSimpleMultiLayer2D( canvas2DCorr, hist2DGWidth_HG, maxActiveLayer, maxActiveRBCh, textSizeRel, Form("%s/HGTrigg_GWidth.pdf", outputDirPlots.Data()), currentRunInfo);
1423     PlotSimpleMultiLayer2D( canvas2DCorr, hist2DGWidthErr_HG, maxActiveLayer, maxActiveRBCh, textSizeRel, Form("%s/HGTrigg_GWidthErr.pdf", outputDirPlots.Data()), currentRunInfo);
1424        
1425     // *****************************************************************
1426     // Create graphs per board and channels with calib values MIP values
1427     // *****************************************************************
1428     TGraphErrors* graphMPV_HG     = CreateGraphFromHistAndCleanup(hist1DMPV_HG, "graphMPV_HG_channels");
1429     TGraphErrors* graphMax_HG     = CreateGraphFromHistAndCleanup(hist1DMax_HG, "graphMax_HG_channels");
1430     TGraphErrors* graphWidth_HG   = CreateGraphFromHistAndCleanup(hist1DWidth_HG, "graphWidth_HG_channels");
1431     TGraphErrors* graphGWidth_HG  = CreateGraphFromHistAndCleanup(hist1DGWidth_HG, "graphGWidth_HG_channels");
1432     TGraphErrors* graphFWHM_HG    = CreateGraphFromHistAndCleanup(hist1DFWHM_HG, "graphFWHM_HG_channels");
1433 
1434     TGraphErrors* graphCAEN_MPV_HG    = CreateGraphFromHistAndCleanup(hist1DCAEN_MPV_HG, "graphCAEN_MPV_HG_channels");
1435     TGraphErrors* graphCAEN_Max_HG    = CreateGraphFromHistAndCleanup(hist1DCAEN_Max_HG, "graphCAEN_Max_HG_channels");
1436     TGraphErrors* graphCAEN_Width_HG  = CreateGraphFromHistAndCleanup(hist1DCAEN_Width_HG, "graphCAEN_Width_HG_channels");
1437     TGraphErrors* graphCAEN_GWidth_HG = CreateGraphFromHistAndCleanup(hist1DCAEN_GWidth_HG, "graphCAEN_GWidth_HG_channels");
1438     TGraphErrors* graphCAEN_FWHM_HG   = CreateGraphFromHistAndCleanup(hist1DCAEN_FWHM_HG, "graphCAEN_FWHM_HG_channels");
1439 
1440     TObjString* stringRunInfo = new TObjString;
1441     stringRunInfo->SetString(GetStringFromRunInfo(currentRunInfo,4));
1442     // ********************************************************************************************************
1443     // write output to single file
1444     // ********************************************************************************************************
1445     TFile* fileOutput = new TFile(Form("%s/output_basics.root",outputDir.Data()),"RECREATE");
1446     // ******************************************************
1447     // create folders
1448     // ******************************************************
1449     fileOutput->mkdir("RawData");
1450     fileOutput->mkdir("RawDataMapped");
1451     fileOutput->mkdir("NoiseSubtracted");
1452     fileOutput->mkdir("NoiseSubtractedMapped");
1453     fileOutput->mkdir("CorrelationLGHG");
1454     fileOutput->mkdir("CorrelationHGLG");
1455     // ******************************************************
1456     // run over all CAEN boards and channels
1457     // ******************************************************
1458     for (Int_t j = 0; j < gMaxBoard; j++){
1459       for (Int_t i = 0; i < gMaxChannels; i++){
1460         // raw data
1461         fileOutput->cd("RawData");
1462         if(histHG[j][i])histHG[j][i]->Write();
1463         if(hist_T_HG[j][i])hist_T_HG[j][i]->Write();
1464         if (isTBdata){
1465           if(histHGTrig[j][i])histHGTrig[j][i]->Write();
1466           if(histHGNoise[j][i])histHGNoise[j][i]->Write();
1467           if(histHGTriggNoise[j][i])histHGTriggNoise[j][i]->Write();
1468           if(fitGausHG_BG[j][i]) fitGausHG_BG[j][i]->Write();
1469         }
1470         if (histLG[j][i])histLG[j][i]->Write();
1471         if (isTBdata){
1472           if(histLGTrig[j][i])histLGTrig[j][i]->Write();
1473           if(histLGNoise[j][i])histLGNoise[j][i]->Write();
1474           if(histLGTriggNoise[j][i])histLGTriggNoise[j][i]->Write();
1475           if (fitGausLG_BG[j][i]) fitGausLG_BG[j][i]->Write();          
1476         }
1477         // noise subtracted
1478         fileOutput->cd("NoiseSubtracted");
1479         if(histNSHG[j][i])histNSHG[j][i]->Write();
1480         if(histNSCombHG[j][i])histNSCombHG[j][i]->Write();
1481         if (isTBdata){
1482           if(histNSHGTrig[j][i])histNSHGTrig[j][i]->Write();
1483           if(histNSCombHGTrig[j][i])histNSCombHGTrig[j][i]->Write();
1484           if(histNSHGNoise[j][i])histNSHGNoise[j][i]->Write();
1485           if(histNSHGTriggNoise[j][i])histNSHGTriggNoise[j][i]->Write();
1486           if(fitLandauG_NSHGTrig[j][i])fitLandauG_NSHGTrig[j][i]->Write();
1487         }
1488         if(histNSLG[j][i])histNSLG[j][i]->Write();
1489         if (isTBdata){
1490           if(histNSLGTrig[j][i])histNSLGTrig[j][i]->Write();
1491           if(histNSLGNoise[j][i])histNSLGNoise[j][i]->Write();
1492           if(histNSLGTriggNoise[j][i])histNSLGTriggNoise[j][i]->Write();
1493         }
1494         // correlation histograms
1495         fileOutput->cd("CorrelationLGHG");
1496         if(histLGHG[j][i])histLGHG[j][i]->Write();
1497         if (fitLGHGCorr[j][i]) fitLGHGCorr[j][i]->Write();
1498         if(histNSLGHG[j][i])histNSLGHG[j][i]->Write();        
1499         fileOutput->cd("CorrelationHGLG");
1500         if(histHGLG[j][i])histHGLG[j][i]->Write();
1501         if (fitHGLGCorr[j][i]) fitHGLGCorr[j][i]->Write();
1502         if(histNSHGLG[j][i])histNSHGLG[j][i]->Write();            
1503       }
1504       fileOutput->cd();
1505       if(hist_T_NSCombHG[j])hist_T_NSCombHG[j]->Write();
1506     }
1507 
1508     // *******************************************************
1509     // run over all physical layers and readout board channels
1510     // *******************************************************
1511     for (Int_t l = 0; l  < gMaxLayers; l++){
1512       for (Int_t c = 1; c < 9; c++){
1513         fileOutput->cd("RawDataMapped");
1514         if (histHG_mapped[l][c]) histHG_mapped[l][c]->Write();
1515         if (histHGTrig_mapped[l][c]) histHGTrig_mapped[l][c]->Write();
1516         if (histHGTriggNoise_mapped[l][c]) histHGTriggNoise_mapped[l][c]->Write();
1517         if (fitGausHG_BG_mapped[l][c]) fitGausHG_BG_mapped[l][c]->Write();
1518         if (histLG_mapped[l][c]) histLG_mapped[l][c]->Write();
1519         if (histLGTrig_mapped[l][c]) histLGTrig_mapped[l][c]->Write();
1520         if (histLGTriggNoise_mapped[l][c]) histLGTriggNoise_mapped[l][c]->Write();
1521         if (fitGausLG_BG_mapped[l][c]) fitGausLG_BG_mapped[l][c]->Write();
1522         fileOutput->cd("NoiseSubtractedMapped");
1523         if (histNSHG_mapped[l][c]) histNSHG_mapped[l][c]->Write();
1524         if (histNSHG_mappedReb[l][c]) histNSHG_mappedReb[l][c]->Write();
1525         if (histNSLG_mapped[l][c]) histNSLG_mapped[l][c]->Write();
1526         if (histNSCombHG_mapped[l][c]) histNSCombHG_mapped[l][c]->Write();
1527         if (histNSCombHG_mappedReb[l][c]) histNSCombHG_mappedReb[l][c]->Write();
1528         if (histNSHGTrig_mapped[l][c]) histNSHGTrig_mapped[l][c]->Write();
1529         if (fitLandauG_NSHGTrig_mapped[l][c]) fitLandauG_NSHGTrig_mapped[l][c]->Write(Form("LandauGauss_NSHGTrig_L%d_C%02d",l,c));
1530         if (histNSHGTrig_mappedReb[l][c]) histNSHGTrig_mappedReb[l][c]->Write();
1531         if (histNSLGTrig_mapped[l][c]) histNSLGTrig_mapped[l][c]->Write();
1532         if (histNSCombHGTrig_mapped[l][c]) histNSCombHGTrig_mapped[l][c]->Write();
1533         if (histNSCombHGTrig_mappedReb[l][c]) histNSCombHGTrig_mappedReb[l][c]->Write();
1534         if (histNSHGTriggNoise_mapped[l][c]) histNSHGTriggNoise_mapped[l][c]->Write();
1535         if (histNSLGTriggNoise_mapped[l][c]) histNSLGTriggNoise_mapped[l][c]->Write();
1536       }
1537     }
1538     // *******************************************************
1539     // write summary histograms
1540     // *******************************************************
1541     fileOutput->cd();
1542     stringRunInfo->Write();
1543     histNChAboveNoise->Write();
1544     histNTrig->Write();
1545     histNTrigNoise->Write();
1546     hist2DNoiseMean_HG->Write();
1547     histNoiseMean_HG->Write();
1548     hist2DNoiseSigma_HG->Write();
1549     histNoiseSigma_HG->Write();
1550     hist2DNoiseMean_LG->Write();
1551     histNoiseMean_LG->Write();
1552     hist2DNoiseSigma_LG->Write();
1553     histNoiseSigma_LG->Write();
1554     hist2DLGHG_slope->Write();
1555     histLGHG_slope->Write();
1556     hist2DLGHG_offset->Write();
1557     
1558     hist1DNoiseSigma_HG->Write();
1559     hist1DNoiseMean_HG->Write();
1560     hist1DNoiseSigma_LG->Write();
1561     hist1DNoiseMean_LG->Write();
1562     hist1DLGHG_slope->Write();
1563     hist1DLGHG_offset->Write();
1564     hist1DHGLG_slope->Write();
1565     hist1DHGLG_offset->Write();
1566     hist1DCAEN_NoiseSigma_HG->Write();
1567     hist1DCAEN_NoiseMean_HG->Write();
1568     hist1DCAEN_NoiseSigma_LG->Write();
1569     hist1DCAEN_NoiseMean_LG->Write();
1570     hist1DCAEN_LGHG_slope->Write();
1571     hist1DCAEN_LGHG_offset->Write();
1572     hist1DCAEN_HGLG_slope->Write();
1573     hist1DCAEN_HGLG_offset->Write();
1574 
1575     gNoiseMeanHG->Write();
1576     gNoiseSigmaHG->Write();
1577     gNoiseMeanLG->Write();
1578     gNoiseSigmaLG->Write();
1579     gCorrLGHGSlope->Write();
1580     gCorrLGHGOffset->Write();
1581     gCorrHGLGSlope->Write();
1582     gCorrHGLGOffset->Write();
1583     gCAEN_NoiseMeanHG->Write();
1584     gCAEN_NoiseSigmaHG->Write();
1585     gCAEN_NoiseMeanLG->Write();
1586     gCAEN_NoiseSigmaLG->Write();
1587     gCAEN_CorrLGHGSlope->Write();
1588     gCAEN_CorrLGHGOffset->Write();
1589     gCAEN_CorrHGLGSlope->Write();
1590     gCAEN_CorrHGLGOffset->Write();
1591 
1592     hist1DMPV_HG->Write();
1593     hist1DMax_HG->Write();
1594     hist1DWidth_HG->Write();
1595     hist1DGWidth_HG->Write();
1596     hist1DFWHM_HG->Write();
1597     hist1DCAEN_MPV_HG->Write();
1598     hist1DCAEN_Max_HG->Write();
1599     hist1DCAEN_Width_HG->Write();
1600     hist1DCAEN_GWidth_HG->Write();
1601     hist1DCAEN_FWHM_HG->Write();
1602 
1603     graphMPV_HG->Write();
1604     graphMax_HG->Write();
1605     graphWidth_HG->Write();
1606     graphGWidth_HG->Write();
1607     graphFWHM_HG->Write();
1608     graphCAEN_MPV_HG->Write();
1609     graphCAEN_Max_HG->Write();
1610     graphCAEN_Width_HG->Write();
1611     graphCAEN_GWidth_HG->Write();
1612     graphCAEN_FWHM_HG->Write();
1613    
1614     hist2DMPV_HG->Write();
1615     hist2DMPVErr_HG->Write();
1616     hist2DWidth_HG->Write();
1617     hist2DWidthErr_HG->Write();
1618     hist2DGWidth_HG->Write();
1619     hist2DGWidthErr_HG->Write();
1620     hist2DMax_HG->Write();
1621     hist2DFWHM_HG->Write();
1622         
1623     hist2DNSTrigg->Write();
1624     hist2DNSTriggNoise->Write();
1625     hist2DNSTriggEffi->Write();
1626     
1627     if (isTBdata){
1628       hist3DMap->Write();
1629       hist2DMap->Write();
1630       hist1DMap->Write();
1631       histNS3DMap->Write();
1632       histNS2DMap->Write();
1633       histNS1DMap->Write();
1634     }
1635      // write output file
1636     fileOutput->Write();
1637     fileOutput->Close();
1638     // ********************************************************************************************************
1639     // DONE
1640     // ********************************************************************************************************
1641 
1642 }                                  
1643