CMS 3D CMS Logo

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