Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-17 08:28:55

0001 #ifndef ANALYSES_H
0002 #define ANALYSES_H
0003 
0004 #include <iostream>
0005 #include <fstream>
0006 #include "TString.h"
0007 #include "TFile.h"
0008 #include "TTree.h"
0009 #include "TObjString.h"
0010 
0011 #include "Setup.h"
0012 #include "RootSetupWrapper.h"
0013 #include "Calib.h"
0014 #include "Event.h"
0015 #include "Tile.h"
0016 #include "HGCROC.h"
0017 #include "Caen.h"
0018     
0019 class Analyses{
0020 
0021  public:
0022   Analyses(){
0023     eventptr=&event;
0024     rswptr=&rsw;
0025     calibptr=&calib;
0026   }
0027   ~Analyses(){}
0028 
0029   //Getter methods
0030   inline TString GetASCIIinputName()        const {return ASCIIinputName;};
0031   inline TString GetMapInputName()          const {return MapInputName;};
0032   inline TString GetRootCalibInputName()    const {return RootCalibInputName;};
0033   inline TString GetRootCalibOutputName()   const {return RootCalibOutputName;};
0034   inline TString GetRootInputName()         const {return RootInputName;};
0035   inline TString GetRootPedestalInputName() const {return RootPedestalInputName;};
0036   inline TString GetRootOutputName()        const {return RootOutputName;};
0037   inline TString GetPlotOutputDir()         const {return OutputNameDirPlots;};
0038   inline TString GetExternalBadChannelMap() const {return ExternalBadChannelMap;};
0039   inline TString GetExternalCalibFile()     const {return ExternalCalibFile;};
0040   inline TString GetExternalToACalibOffSetFile()     const {return ExternalToACalibOffSetFile;};
0041   
0042   inline std::fstream* GetASCIIinput() {return &ASCIIinput;};
0043   inline std::fstream* GetMapInput()   {return &MapInput;};
0044 
0045   inline TFile* GetRootCalibInput()    {return RootCalibInput;}
0046   inline TFile* GetRootInput()         {return RootInput;}
0047   inline TFile* GetRootPedestalInput() {return RootPedestalInput;}
0048   inline TFile* GetRootOutput()        {return RootOutput;}
0049 
0050   inline bool CanOverWrite(void)                const {return Overwrite;};
0051   inline bool IsToTransferCalib(void)           const {return ApplyTransferCalib;};
0052   inline bool IsToApplyCalibration(void)        const {return ApplyCalibration;};
0053   inline bool IsToVisualizeWaveform(void)       const {return IsVisualizeWaveform;};
0054   inline bool IsToConvert(void)                 const {return Convert;};
0055   inline bool IsToExtractPedestal(void)         const {return ExtractPedestal;};
0056   inline bool IsToExtractToAPhase(void)         const {return ExtractToAPhase;};
0057   inline bool IsToExtractScaling(void)          const {return ExtractScaling;};
0058   inline bool IsToExtractScalingImproved(void)  const {return ExtractScalingImproved;};
0059   inline bool IsHGCROC(void)                    const {return HGCROC;};
0060   
0061   inline bool IsToReextractNoise(void)          const {return ReextractNoise;};
0062   inline bool IsToSaveNoiseOnly(void)           const {return SaveNoiseOnly;};
0063   inline bool IsToSaveMipsOnly(void)            const {return SaveMipsOnly;};
0064   inline bool IsToEvalLocalTrigg(void)          const {return EvalLocalTriggers;};
0065   inline bool UseLocTriggFromFile(void)         const {return LocTriggFile;};
0066   inline bool IsToSaveCalibOnly(void)           const {return SaveCalibOnly;};
0067   inline bool IsCalibSaveToFile(void)           const {return SaveCalibToFile;};
0068   inline bool IsToSkimHGCROC(void)              const {return SkimHGCROC;};
0069   inline short GetCalcBadChannel(void)          const {return CalcBadChannel;};
0070   inline short GetExtPlotting(void)             const {return ExtPlot;};
0071   inline bool GetOverWriteCalib(void)           const {return OverWriteCalib;};
0072   inline bool GetOverWriteSetup(void)           const {return OverWriteSetup;};
0073   inline int GetMaxEvents(void)                 const {return maxEvents;};
0074   inline bool GetHGCROCTrunctation(void)        const {return truncateHGCROC;};
0075   inline int GetHGCROCNSampleInteg(void)        const {return nSampleHGCROCInt;};
0076   inline int GetHGCROCOptInteg(void)            const {return optHGCROCInt;};
0077 
0078   inline bool CheckOutputWriteStatus(void){
0079     bool status = true;
0080     TFile* outputFiles[] = {RootOutput, RootOutputHist, RootCalibOutput};
0081     for (TFile* output : outputFiles){
0082       if (!output) continue;
0083       if (output->IsOpen()) output->Flush();
0084       if (output->TestBit(TFile::kWriteError)){
0085         std::cerr << "ROOT write error detected for " << output->GetName() << std::endl;
0086         status = false;
0087       }
0088     }
0089     return status;
0090   };
0091   
0092   //setter methods
0093   //Overload method for boolean...or is it too dangerous?
0094   inline void CanOverWrite(bool b)               {Overwrite=b;};
0095   inline void IsCalibSaveToFile(bool b)          {SaveCalibToFile=b;};
0096   inline void IsToTransferCalib(bool b)          {ApplyTransferCalib=b;};
0097   inline void IsToVisualizeWaveform(bool b)      {IsVisualizeWaveform=b;};
0098   inline void IsToApplyCalibration(bool b)       {ApplyCalibration=b;};
0099   inline void IsToConvert(bool b)                {Convert=b;};
0100   inline void IsToExtractPedestal(bool b)        {ExtractPedestal=b;};
0101   inline void IsToExtractToAPhase(bool b)        {ExtractToAPhase=b;};
0102   inline void IsToExtractScaling(bool b)         {ExtractScaling=b;};
0103   inline void IsToExtractScalingImproved(bool b) {ExtractScalingImproved=b;};
0104   inline void IsHGCROC(bool b)                   {HGCROC=b;};
0105   inline void IsToReextractNoise(bool b)         {ReextractNoise=b;};
0106   inline void IsToReextractLGHGCorr(bool b)      {ReextractLGHGCorr=b;};
0107   inline void IsNoEventTree(bool b)              {NoEventTreeWrite=b;};
0108   
0109   inline void IsToSaveNoiseOnly(bool b)          {SaveNoiseOnly = b;};
0110   inline void IsToSaveMipsOnly(bool b)           {SaveMipsOnly = b;};
0111   inline void IsToEvalLocalTrigg(bool b)         {EvalLocalTriggers = b;};
0112   inline void DisableRecalcTriggPrimitives()     {EvalTriggerPrimitives = false;};
0113   inline void IsToSaveCalibOnly(bool b)          {SaveCalibOnly = b;};
0114   inline void IsToSkimHGCROC(bool b)             {SkimHGCROC = b;};
0115   inline void UseLocTriggFromFile(bool b)        {LocTriggFile = b;};
0116   inline void SetCalcBadChannel(short b)         {CalcBadChannel = b;};
0117   inline void SetOverWriteCalib(bool b)          {OverWriteCalib = b;};
0118   inline void SetOverWriteSetup(bool b)          {OverWriteSetup = b;};
0119   inline void SetExtPlotting(short b)            {ExtPlot = b;};
0120   inline void SetCleanupEvents(short b)          {EventCleanup = b;};
0121   inline void EnableDebug(int i)                 {debug=i;};
0122   inline void SetHGCROCTruncation(bool b)        {truncateHGCROC = b;};
0123   inline void SetHGCROCNSampleInteg(int n)       {optHGCROCInt      = 1; 
0124                                                   nSampleHGCROCInt  = n;
0125                                                   };
0126   inline void SetHGCROCNSampleIntegForced(int n) {optHGCROCInt      = 101; 
0127                                                   nSampleHGCROCInt  = n;
0128                                                   };
0129   
0130   inline void SetYear(int year)                  {yearData=year;};
0131   inline void SetASCIIinput(TString name)        {ASCIIinputName=name;};
0132   inline void SetMapInput(TString name)          {MapInputName=name;};
0133   inline void SetRunListInput(TString name)      {RunListInputName=name;};
0134   inline void SetRootCalibInput(TString name)    {RootCalibInputName=name;};
0135   inline void SetRootCalibOutput(TString name)   {RootCalibOutputName=name;};
0136   inline void SetRootInput(TString name)         {RootInputName=name;};
0137   inline void SetRootPedestalInput(TString name) {RootPedestalInputName=name;};
0138   inline void SetRootOutput(TString name)        {RootOutputName =name;};
0139   inline void SetRootOutputHists(TString name)   {RootOutputNameHist =name;};
0140   inline void SetPlotOutputDir(TString name)     {OutputNameDirPlots =name;};
0141   inline void SetPlotSkipLayer(int skip)         {skipPlotLayer =skip;};
0142   inline void SetPlotExtension(TString name)     {plotSuffix = name;};
0143   inline void SetExternalBadChannelMap(TString name)     {ExternalBadChannelMap =name;};
0144   inline void SetExternalCalibFile(TString name)     {ExternalCalibFile =name;};
0145   inline void SetExternalToACalibOffSetFile(TString name)     {ExternalToACalibOffSetFile =name;};  
0146   inline void SetMaxEvents(int n)                       {maxEvents = n;};
0147   inline void OverwriteMinMipFrac(double min)          {minMipFrac = min;};
0148   inline void OverwriteMinFracTriggThre(double min)    {minFracTriggThre = min;};
0149   
0150   //General methods
0151   bool CreateOutputRootFile(void);
0152   bool CreateOutputRootHistFile(void);
0153   bool CheckAndOpenIO(void);
0154   bool Process(void);
0155 
0156   //Variable members
0157   TString ASCIIinputName;                 // CAEN ASCII file input name (only needed for converting data)
0158   TString RootOutputName;                 // file name of root output with tree
0159   TString RootOutputNameHist;             // file name of root output with additional histograms & fits
0160   TString OutputNameDirPlots;             // directory name of output for plots
0161   TString RootInputName;                  // file name of input root file 
0162   TString RootCalibInputName;             // file name of calibration root file (mip calib)
0163   TString RootCalibOutputName;            // file name of calibration root file (mip calib) output
0164   TString RootPedestalInputName;          // file name of pedestal root file (pedestal values)
0165   TString MapInputName;                   // file name geometry mapping
0166   TString RunListInputName;               // file name run list 
0167   TString ExternalBadChannelMap;          // file name external bad channel map
0168   TString ExternalCalibFile;              // file name external calib file
0169   TString ExternalToACalibOffSetFile =""; // file name external ToA calib offsets file  
0170   TString plotSuffix        = "pdf";      // plot extension
0171   TFile* RootOutput         =nullptr;     // root file output tree
0172   TFile* RootOutputHist     =nullptr;     // root file output histos
0173   TFile* RootInput          =nullptr;     // root file input 
0174   TFile* RootPedestalInput  =nullptr;     // root file pedestal values
0175   TFile* RootCalibInput     =nullptr;     // root file calib values (mip)
0176   TFile* RootCalibOutput    =nullptr;     // root file calib values (mip) output
0177   std::fstream ASCIIinput;                // ASCII file streamer for CAEN data
0178   std::fstream MapInput;                  // file streamer for geometry mapping
0179   bool Convert                =false;     // Flag for data conversion 
0180   bool HGCROC                 =false;     // Flag for HGCROC data conversion
0181   bool ExtractPedestal        =false;     // Flag for pedestal extraction
0182   bool ExtractScaling         =false;     // Flag for mip scaling extraction
0183   bool ExtractScalingImproved =false;     // Flag for mip scaling extraction 2nd pass
0184   bool ReextractNoise         =false;     // Flag to enable noise trigger extraction and alternative pass for pedestals
0185   bool ReextractLGHGCorr      =false;     // Flag to enable reevaluation of LG-HG extraction 
0186   bool NoEventTreeWrite       =false;     // Flat to disable the event tree writing
0187   bool ExtractToAPhase        =false;     // Flag to enable the ToA Phase extraction
0188   bool ApplyTransferCalib     =false;     // Flag for application of pedestals
0189   bool IsVisualizeWaveform    =false;     // Flag for routine with waveform visualization
0190   bool ApplyCalibration       =false;     // Flag for aplication of calibration
0191   bool SaveNoiseOnly          =false;     // Flag to reduce file to noise/pedestal only
0192   bool SaveMipsOnly           =false;     // Flag to reduce file to mips only
0193   bool SaveCalibOnly          =false;     // Flag to reduce file to mips only
0194   bool SaveCalibToFile        =false;     // Flag to save calib objects to text file
0195   bool SkimHGCROC             =false;     // Flag to skim HGCROC data from pure noise events
0196   bool EvalLocalTriggers      =false;     // Flag to run local trigger eval
0197   bool EvalTriggerPrimitives  =true;      // Flag to evaluate trigger primitives
0198   bool LocTriggFile           =false;     // Flag to use already evaluated triggeres
0199   short CalcBadChannel        =0;         // Flag to create bad channel map
0200   bool OverWriteCalib         =false;     // Flag to overwrite calib from text file
0201   bool OverWriteSetup         =false;     // Flag to overwrite setup from new mapping file
0202   short ExtPlot               =0;         // Enable extended plotting
0203   bool Overwrite              =false;     // Flag to overwrite outputs
0204   int debug                   =0;         // debug level 
0205   int skipPlotLayer           = 0;        // allow for skipping layers
0206   int yearData                =-1;        // data taking year externally set
0207   bool truncateHGCROC         =false;     // flag to enable the truncation of the HGCROC data to 8 bit (masking the 2 least significant bits)
0208   short EventCleanup          = 0;        // flag to reject events due to messed up signals in single channels
0209   double cleanupLGTh          = 20;
0210   double cleanupHGTh          = 150;
0211   double minMipFrac           = 0.3;
0212   double minFracTriggThre     = 3.;
0213   int optHGCROCInt            = 0;
0214   int nSampleHGCROCInt        = 1;
0215   
0216   RootSetupWrapper rsw;                   // Wrapper singleton class for setup
0217   RootSetupWrapper* rswptr;               // Pointer to wrapper for singleton class for setup
0218   Setup* setup;                           // geometry setup
0219   Calib calib;                            // calibration object
0220   Calib* calibptr;                        // pointer to calib object
0221   Event event;
0222   Event* eventptr;
0223   int maxEvents               = -1;
0224   
0225   TTree* TsetupIn=nullptr;
0226   TTree* TsetupOut=nullptr;
0227   TTree* TdataIn=nullptr;
0228   TTree* TdataOut=nullptr;
0229   TTree* TcalibIn=nullptr;
0230   TTree* TcalibOut=nullptr;
0231 
0232  protected:
0233 
0234 
0235  private:
0236   bool ConvertASCII2Root(void);
0237   bool ConvertOldRootFile2Root(void);
0238   bool GetPedestal(void);
0239   bool EvaluateHGCROCToAPhases(void);
0240   bool TransferCalib(void);
0241   bool ReevaluateLGHGCorr(void);
0242   bool VisualizeWaveform(void);
0243   bool GetScaling(void);
0244   bool GetImprovedScaling(void);
0245   bool GetNoiseSampleAndRefitPedestal(void);
0246   bool RunEvalLocalTriggers(void);
0247   bool Calibrate(void);
0248   bool SaveNoiseTriggersOnly(void);
0249   bool SaveMuonTriggersOnly(void);
0250   bool SaveCalibToOutputOnly(void);
0251   bool SkimHGCROCData(void);
0252   bool OverWriteSetupTree(void);
0253   
0254 
0255   std::map<int,short> ReadExternalBadChannelMap(void);
0256  };
0257 
0258 
0259 #endif