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