CMS 3D CMS Logo

EcalUncalibRecHitRecAnalFitAlgo.h
Go to the documentation of this file.
1 #ifndef RecoLocalCalo_EcalRecAlgos_EcalUncalibRecHitRecAnalFitAlgo_HH
2 #define RecoLocalCalo_EcalRecAlgos_EcalUncalibRecHitRecAnalFitAlgo_HH
3 
11 #include "CLHEP/Matrix/Matrix.h"
12 #include "CLHEP/Matrix/SymMatrix.h"
14 #include <vector>
15 #include <string>
16 
17 #include "TROOT.h"
18 #include "TMinuit.h"
19 #include "TGraph.h"
20 #include "TF1.h"
21 
22 template <class C>
24 private:
25  double pulseShapeFunction(double* var, double* par) {
26  double x = var[0];
27  double ampl = par[0];
28  double tp = par[1];
29  double alpha = par[2];
30  double t0 = par[3];
31 
32  double f = pow((x - t0) / tp, alpha) * exp(-alpha * (x - tp - t0) / tp);
33  return ampl * f;
34  };
35 
36  double pedestalFunction(double* var, double* par) {
37  double ped = par[0];
38  return ped;
39  };
40 
41 public:
42  // destructor
44 
47  const double* pedestals,
48  const double* gainRatios,
50  const EcalWeightSet::EcalChi2WeightMatrix** chi2Matrix) override {
51  double amplitude_(-1.), pedestal_(-1.), jitter_(-1.), chi2_(-1.);
52 
53  // Get time samples
54  //HepMatrix frame(C::MAXSAMPLES, 1);
55  double frame[C::MAXSAMPLES];
56  // int gainId0 = dataFrame.sample(0).gainId();
57  int gainId0 = 1;
58  int iGainSwitch = 0;
59  double maxsample(-1);
60  int imax(-1);
61  bool isSaturated = false;
62  uint32_t flag = 0;
63  for (int iSample = 0; iSample < C::MAXSAMPLES; iSample++) {
64  int gainId = dataFrame.sample(iSample).gainId();
65  if (dataFrame.isSaturated()) {
66  gainId = 3;
67  isSaturated = true;
68  }
69 
70  if (gainId != gainId0)
71  ++iGainSwitch;
72  if (!iGainSwitch)
73  frame[iSample] = double(dataFrame.sample(iSample).adc());
74  else
75  frame[iSample] =
76  double(((double)(dataFrame.sample(iSample).adc()) - pedestals[gainId - 1]) * gainRatios[gainId - 1]);
77 
78  if (frame[iSample] > maxsample) {
79  maxsample = frame[iSample];
80  imax = iSample;
81  }
82  }
83 
84  // Compute parameters
85  //std::cout << "EcalUncalibRecHitRecAnalFitAlgo::makeRecHit() not yey implemented. returning dummy rechit" << std::endl;
86 
87  // prepare TGraph for analytic fit
88  double xarray[10] = {0., 1., 2., 3., 4., 5., 6., 7., 8., 9.};
89  TGraph graph(10, xarray, frame);
90 
91  // fit functions
92  TF1 pulseShape =
93  TF1("pulseShape", "[0]*pow((x - [3])/[1],[2])*exp(-[2]*(x - [1] - [3])/[1])", imax - 1., imax + 3.);
94  TF1 pedestal = TF1("pedestal", "[0]", 0., 2.);
95 
96  //TF1 pulseShape = TF1("pulseShape",pulseShapeFunction,imax-1.,imax+3.);
97  //TF1 pedestal = TF1("pedestal",pedestalFunction,0.,2.);
98  TF1 pluseAndPed = TF1("pulseAndPed", "pedestal+pulseShape");
99 
100  //pulseShape parameters
101  // Amplitude
102  double FIT_A = (double)maxsample; //Amplitude
103  pulseShape.SetParameter(0, FIT_A);
104  pulseShape.SetParName(0, "Amplitude");
105  // T peak
106  double FIT_Tp = (double)imax; //T peak
107  pulseShape.SetParameter(1, FIT_Tp);
108  pulseShape.SetParName(1, "t_{P}");
109  // Alpha
110  double FIT_ALFA = 1.5; //Alpha
111  pulseShape.SetParameter(2, FIT_ALFA);
112  pulseShape.SetParName(2, "\\alpha");
113  // T off
114  double FIT_To = 3.; //T off
115  pulseShape.SetParameter(3, FIT_To);
116  pulseShape.SetParName(3, "t_{0}");
117 
118  // pedestal
119  pedestal.SetParameter(0, frame[0]);
120  pedestal.SetParName(0, "Pedestal");
121 
122  graph.Fit(&pulseShape, "QRM");
123  //TF1 *pulseShape2=graph.GetFunction("pulseShape");
124 
125  if (std::string(gMinuit->fCstatu.Data()) == std::string("CONVERGED ")) {
126  double amplitude_value = pulseShape.GetParameter(0);
127 
128  graph.Fit(&pedestal, "QRL");
129  //TF1 *pedestal2=graph.GetFunction("pedestal");
130  double pedestal_value = pedestal.GetParameter(0);
131 
132  if (!iGainSwitch)
133  amplitude_ = amplitude_value - pedestal_value;
134  else
135  amplitude_ = amplitude_value;
136 
137  pedestal_ = pedestal_value;
138  jitter_ = pulseShape.GetParameter(3);
139  chi2_ = 1.; // successful fit
140  if (isSaturated)
142  /*
143  std::cout << "separate fits\nA: " << amplitude_value << ", Ped: " << pedestal_value
144  << ", t0: " << jitter_ << ", tp: " << pulseShape.GetParameter(1)
145  << ", alpha: " << pulseShape.GetParameter(2)
146  << std::endl;
147  */
148  }
149 
150  return EcalUncalibratedRecHit(dataFrame.id(), amplitude_, pedestal_, jitter_ - 6, chi2_, flag);
151  }
152 };
153 #endif
float alpha
Definition: AMPTWrapper.h:105
double pedestalFunction(double *var, double *par)
math::Matrix< 10, 10 >::type EcalChi2WeightMatrix
Definition: EcalWeightSet.h:20
math::Matrix< 3, 10 >::type EcalWeightMatrix
Definition: EcalWeightSet.h:19
double f[11][100]
constexpr int gainId(sample_type sample)
get the gainId (2 bits)
EcalUncalibratedRecHit makeRecHit(const C &dataFrame, const double *pedestals, const double *gainRatios, const EcalWeightSet::EcalWeightMatrix **weights, const EcalWeightSet::EcalChi2WeightMatrix **chi2Matrix) override
Compute parameters.
bool isSaturated(const Digi &digi, const int &maxADCvalue, int ifirst, int n)
double pulseShapeFunction(double *var, double *par)
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29