Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:17:53

0001 #ifndef MBD_MBDSIG_H
0002 #define MBD_MBDSIG_H
0003 
0004 #include "MbdRunningStats.h"
0005 
0006 #include <TH1.h>
0007 
0008 #include <fstream>
0009 #include <vector>
0010 
0011 class TTree;
0012 class TGraphErrors;
0013 class TH2;
0014 class MbdCalib;
0015 
0016 /**
0017 
0018 MbdSig: Single Channel digital signal class, includes processing
0019 
0020 */
0021 
0022 class MbdSig
0023 {
0024  public:
0025   explicit MbdSig(const int chnum = 0, const int nsamp = 0);
0026   // explicit MbdSig(const MbdSig &obj);    // never used
0027   virtual ~MbdSig();
0028 
0029   // MbdSig& operator= (const MbdSig& obj) = delete; // never used
0030 
0031   void SetNSamples( const int s ) { _nsamples = s; }
0032   void SetY(const Float_t *y, const int invert = 1);
0033   void SetXY(const Float_t *x, const Float_t *y, const int invert = 1);
0034 
0035   void SetCalib(MbdCalib *mcal);
0036 
0037   TH1 *GetHist() { return hpulse; }
0038   TGraphErrors *GetGraph() { return gpulse; }
0039   Double_t GetAmpl() { return f_ampl; }
0040   Double_t GetTime() { return f_time; }
0041   Double_t GetIntegral() { return f_integral; }
0042 
0043   /**
0044    * Fill hists from data between minsamp and maxsamp bins
0045    * sample number starts from 0
0046    * Ped0 is the running pedestal and rms
0047    * */
0048   void FillPed0(const Int_t sampmin, const Int_t sampmax);
0049   void FillPed0(const Double_t begin, const Double_t end);
0050 
0051   Double_t GetPed0() { return ped0; }
0052   Double_t GetPed0RMS() { return ped0rms; }
0053 
0054   /** Set the global pedestal. Once set, it is applied to the data for all events.  */
0055   void SetPed0(const Double_t mean, const Double_t rms = 0.);
0056 
0057   /** Use the event by event pedestal, */
0058   void SetEventPed0Range(const Int_t minsamp, const Int_t maxsamp)
0059   {
0060     minped0samp = minsamp;
0061     maxped0samp = maxsamp;
0062   }
0063 
0064   void SetEventPed0Range(const Double_t minx, const Double_t maxx)
0065   {
0066     minped0x = minx;
0067     maxped0x = maxx;
0068   }
0069 
0070   void SetEventPed0PreSamp(const Int_t presample, const Int_t nsamps = -1, const int max_samp = -1);
0071 
0072   void CalcEventPed0(const Int_t minpedsamp, const Int_t maxpedsamp);
0073   void CalcEventPed0(const Double_t minpedx, const Double_t maxpedx);
0074   int CalcEventPed0_PreSamp(const Int_t presample, const Int_t nsamps = 1);
0075   void Remove_Pileup();
0076 
0077   TH1 *GetPedHist() { return hPed0; }
0078 
0079   /** Leading Edge Discriminator signal */
0080   Double_t LeadingEdge(const Double_t threshold);  // Leading Edge Discriminator Time
0081 
0082   /** digital CFD, threshold is in fraction of amplitude */
0083   Double_t dCFD(const Double_t fraction_threshold);
0084 
0085   /** MBD method to get time, max_samp is the sample to use */
0086   Double_t MBDTDC(const Int_t max_samp);
0087 
0088   /** Get pulse amplitude with spline fit */
0089   Double_t GetSplineAmpl();
0090 
0091   /** Simple integral to get total charge, etc */
0092   Double_t Integral(const Double_t xmin, const Double_t xmax);
0093 
0094   /** The maximum value from all samples */
0095   void LocMax(Double_t &x_at_max, Double_t &ymax, Double_t xminrange = 0., Double_t xmaxrange = 0.);
0096 
0097   /** The minimum value from all samples (including negatives) */
0098   void LocMin(Double_t &x_at_min, Double_t &ymin, Double_t xminrange = 0., Double_t xmaxrange = 0.);
0099 
0100   /** Use template fit to get ampl and time */
0101   Int_t FitTemplate(const Int_t sampmax = -1);
0102   // Double_t Ampl() { return f_ampl; }
0103   // Double_t Time() { return f_time; }
0104 
0105   /** Make template waveforms for later fits */
0106   void SetTemplateSize(const Int_t nptsx, const Int_t nptsy, const Double_t begt, const Double_t endt);
0107   Int_t SetTemplate(const std::vector<float> &shape, const std::vector<float> &sherr);
0108 
0109   // Double_t FitPulse();
0110   void SetTimeOffset(const Double_t o) { f_time_offset = o; }
0111   Double_t TemplateFcn(const Double_t *x, const Double_t *par);
0112   TF1 *GetTemplateFcn() { return template_fcn; }
0113   void SetMinMaxFitTime(const Double_t mintime, const Double_t maxtime);
0114 
0115   void WritePedHist();
0116 
0117   void DrawWaveform();      /// Draw Subtracted Waveform
0118   void PadUpdate() const;
0119   void Print();
0120   void Verbose(const int v) { _verbose = v; }
0121 
0122  private:
0123   void Init();
0124 
0125   int _ch;
0126   int _nsamples;
0127   int _status{0};
0128 
0129   int _evt_counter{0};
0130   MbdCalib *_mbdcal{nullptr};
0131   float _pileup_p0{0.};
0132   float _pileup_p1{0.};
0133   float _pileup_p2{0.};
0134   TF1 *fit_pileup{nullptr};
0135 
0136   /** fit values*/
0137   // should make an array for the different methods
0138   Double_t f_ampl{0,}; /** best guess (from fit of spline or template, or max adc) */
0139   Double_t f_time{0.}; /** best guess (from fit of spline or template, or max adc) */
0140 
0141   Double_t f_time_offset{4.0}; /** time offset used in fit */
0142 
0143   Double_t f_integral{0.}; /** integral */
0144 
0145   TH1 *hRawPulse{nullptr};           //!
0146   TH1 *hSubPulse{nullptr};           //!
0147   TH1 *hpulse{nullptr};              //!
0148   TGraphErrors *gRawPulse{nullptr};  //!
0149   TGraphErrors *gSubPulse{nullptr};  //!
0150   TGraphErrors *gpulse{nullptr};     //!
0151 
0152   /** for CalcPed0 */
0153   //std::unique_ptr<MbdRunningStats> ped0stats{nullptr};    //!
0154   MbdRunningStats *ped0stats{nullptr};    //!
0155   TH1 *hPed0{nullptr};            //! all events
0156   TH1 *hPedEvt{nullptr};          //! evt-by-event pedestal
0157   TF1 *ped_fcn{nullptr};
0158   TF1 *ped_tail{nullptr};         //! tail of prev signal
0159   Double_t ped0{0.};                  //!
0160   Double_t ped0rms{0.};               //!
0161   int   use_ped0{0};                 //! whether to apply ped0
0162   Int_t minped0samp{-9999};       //! min sample for event-by-event ped, inclusive
0163   Int_t maxped0samp{-9999};       //! max sample for event-by-event ped, inclusive
0164   Double_t minped0x{0.};              //! min x for event-by-event ped, inclusive
0165   Double_t maxped0x{0.};              //! max x for event-by-event ped, inclusive
0166   Double_t ped_presamp{};         //! presamples for ped calculation
0167   Double_t ped_presamp_nsamps{};  //! num of presamples for ped calculation
0168   Double_t ped_presamp_maxsamp{-1}; //! a peak sample for ped calc (-1 = use max)
0169 
0170   /** for time calibration */
0171   // Double_t time_calib;
0172 
0173   /** For pulse template extraction */
0174   TH2 *h2Template{nullptr};
0175   TH2 *h2Residuals{nullptr};
0176 
0177   TH1 *hAmpl{nullptr};
0178   TH1 *hTime{nullptr};
0179   Int_t template_npointsx{0};
0180   Int_t template_npointsy{0};
0181   Double_t template_begintime{0.};
0182   Double_t template_endtime{0.};
0183   // Double_t template_min_good_amplitude{20.};    //! for template, in original units of waveform data
0184   // Double_t template_max_good_amplitude{4080.};  //! for template, in original units of waveform data
0185   // Double_t template_min_xrange{0.};             //! for template, in original units of waveform data
0186   // Double_t template_max_xrange{0.};             //! for template, in original units of waveform data
0187   std::vector<float> template_y;
0188   std::vector<float> template_yrms;
0189   TF1 *template_fcn{nullptr};
0190   Double_t fit_min_time{};  //! min time for fit, in original units of waveform data
0191   Double_t fit_max_time{};  //! max time for fit, in original units of waveform data
0192 
0193   std::ofstream *_pileupfile{nullptr};  // for writing out waveforms from prev. crossing pileup
0194                                         // use for calibrating out the tail from these events
0195 
0196 
0197   int _verbose{0};
0198 };
0199 
0200 #endif  // __MBDSIG_H__