CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ZdcSimpleRecAlgo.cc
Go to the documentation of this file.
4 #include <algorithm> // for "max"
5 #include <iostream>
6 #include <math.h>
7 
8 
9 
10 static double MaximumFractionalError = 0.0005; // 0.05% error allowed from this source
11 
12 ZdcSimpleRecAlgo::ZdcSimpleRecAlgo(bool correctForTimeslew, bool correctForPulse, float phaseNS, int recoMethod) :
13  recoMethod_(recoMethod),
14  correctForTimeslew_(correctForTimeslew),
15  correctForPulse_(correctForPulse) {
16 }
17 
19  recoMethod_(recoMethod),
20  correctForTimeslew_(false) {
21 }
23  if (correctForPulse_) {
24  pulseCorr_=std::auto_ptr<HcalPulseContainmentCorrection>(new HcalPulseContainmentCorrection(toadd,phaseNS_,MaximumFractionalError));
25  }
26 }
27 static float timeshift_ns_zdc(float wpksamp);
28 
29 namespace ZdcSimpleRecAlgoImpl {
30  template<class Digi, class RecHit>
31  inline RecHit reco1(const Digi& digi, const HcalCoder& coder, const HcalCalibrations& calibs,
32  const std::vector<unsigned int>& myNoiseTS, const std::vector<unsigned int>& mySignalTS, bool slewCorrect, const HcalPulseContainmentCorrection* corr, HcalTimeSlew::BiasSetting slewFlavor) {
33  CaloSamples tool;
34  coder.adc2fC(digi,tool);
35  int ifirst = mySignalTS[0];
36  int n = mySignalTS.size();
37  double ampl=0; int maxI = -1; double maxA = -1e10; double ta=0;
38  double fc_ampl=0;
39  for (int i=ifirst; i<tool.size() && i<n+ifirst; i++) {
40  int capid=digi[i].capid();
41  ta = (tool[i]-calibs.pedestal(capid)); // pedestal subtraction
42  fc_ampl+=ta;
43  ta*= calibs.respcorrgain(capid) ; // fC --> GeV
44  ampl+=ta;
45  if(ta>maxA){
46  maxA=ta;
47  maxI=i;
48  }
49  }
50 
51  double time=-9999;
53  if(maxI==0 || maxI==(tool.size()-1)) {
54  LogDebug("HCAL Pulse") << "ZdcSimpleRecAlgo::reconstruct :"
55  << " Invalid max amplitude position, "
56  << " max Amplitude: "<< maxI
57  << " first: "<<ifirst
58  << " last: "<<(tool.size()-1)
59  << std::endl;
60  } else {
61  maxA=fabs(maxA);
62  int capid=digi[maxI-1].capid();
63  double t0 = fabs((tool[maxI-1]-calibs.pedestal(capid))*calibs.respcorrgain(capid) );
64  capid=digi[maxI+1].capid();
65  double t2 = fabs((tool[maxI+1]-calibs.pedestal(capid))*calibs.respcorrgain(capid) );
66  double wpksamp = (t0 + maxA + t2);
67  if (wpksamp!=0) wpksamp=(maxA + 2.0*t2) / wpksamp;
68  time = (maxI - digi.presamples())*25.0 + timeshift_ns_zdc(wpksamp);
69  if (corr!=0) {
70  // Apply phase-based amplitude correction:
71  ampl *= corr->getCorrection(fc_ampl);
72  }
73 
74  if (slewCorrect) time-=HcalTimeSlew::delay(std::max(1.0,fc_ampl),slewFlavor);
75  }
76 
77  time=time-calibs.timecorr(); // time calibration
78  return RecHit(digi.id(),ampl,time);
79  }
80 }
81 
82 namespace ZdcSimpleRecAlgoImpl {
83  template<class Digi, class RecHit>
84  inline RecHit reco2(const Digi& digi, const HcalCoder& coder, const HcalCalibrations& calibs,
85  const std::vector<unsigned int>& myNoiseTS, const std::vector<unsigned int>& mySignalTS, bool slewCorrect, const HcalPulseContainmentCorrection* corr, HcalTimeSlew::BiasSetting slewFlavor) {
86  CaloSamples tool;
87  coder.adc2fC(digi,tool);
88  // Reads noiseTS and signalTS from database
89  int ifirst = mySignalTS[0];
90 // int n = mySignalTS.size();
91  double ampl=0; int maxI = -1; double maxA = -1e10; double ta=0;
92  double Allnoise = 0;
93  int noiseslices = 0;
94  int CurrentTS = 0;
95  double noise = 0;
96  double fc_ampl=0;
97 
98  for(unsigned int iv = 0; iv<myNoiseTS.size(); ++iv)
99  {
100  CurrentTS = myNoiseTS[iv];
101  Allnoise += tool[CurrentTS];
102  noiseslices++;
103  }
104  if(noiseslices != 0) {
105  noise = (Allnoise)/double(noiseslices);
106  } else {
107  noise = 0;
108  }
109  // factor to multiply by noise to make 0 or 1 to handle negative noise situations
110  double noisefactor=1.;
111  for(unsigned int ivs = 0; ivs<mySignalTS.size(); ++ivs)
112  {
113  if(noise<0){
114  // flag hit as having negative noise, and don't subtract anything, because
115  // it will falsely increase the energy
116  noisefactor=0.;
117  }
118  CurrentTS = mySignalTS[ivs];
119  int capid=digi[CurrentTS].capid();
120  if(noise<0){
121  // flag hit as having negative noise, and don't subtract anything, because
122  // it will falsely increase the energy
123  noisefactor=0.;
124  }
125  ta = tool[CurrentTS]-noisefactor*noise;
126  fc_ampl+=ta;
127  ta*= calibs.respcorrgain(capid) ; // fC --> GeV
128  ampl+=ta;
129  if(ta>maxA){
130  maxA=ta;
131  maxI=CurrentTS;
132  }
133  }
134 
135 // if(ta<0){
136 // // flag hits that have negative energy
137 // }
138 
139  double time=-9999;
141  if(maxI==0 || maxI==(tool.size()-1)) {
142  LogDebug("HCAL Pulse") << "ZdcSimpleRecAlgo::reco2 :"
143  << " Invalid max amplitude position, "
144  << " max Amplitude: "<< maxI
145  << " first: "<<ifirst
146  << " last: "<<(tool.size()-1)
147  << std::endl;
148  } else {
149  int capid=digi[maxI-1].capid();
150  double Energy0 = ((tool[maxI-1])*calibs.respcorrgain(capid) );
151 // if any of the energies used in the weight are negative, make them 0 instead
152 // these are actually QIE values, not energy
153  if(Energy0<0){Energy0=0.;}
154  capid=digi[maxI].capid();
155  double Energy1 = ((tool[maxI])*calibs.respcorrgain(capid) ) ;
156  if(Energy1<0){Energy1=0.;}
157  capid=digi[maxI+1].capid();
158  double Energy2 = ((tool[maxI+1])*calibs.respcorrgain(capid) );
159  if(Energy2<0){Energy2=0.;}
160 //
161  double TSWeightEnergy = ((maxI-1)*Energy0 + maxI*Energy1 + (maxI+1)*Energy2);
162  double EnergySum=Energy0+Energy1+Energy2;
163  double AvgTSPos=0.;
164  if (EnergySum!=0) AvgTSPos=TSWeightEnergy/ EnergySum;
165 // If time is zero, set it to the "nonsensical" -99
166 // Time should be between 75ns and 175ns (Timeslices 3-7)
167  if(AvgTSPos==0){
168  time=-99;
169  } else {
170  time = (AvgTSPos*25.0);
171  }
172  if (corr!=0) {
173  // Apply phase-based amplitude correction:
174  ampl *= corr->getCorrection(fc_ampl);
175  }
176  }
177  return RecHit(digi.id(),ampl,time);
178  }
179 }
180 
181 ZDCRecHit ZdcSimpleRecAlgo::reconstruct(const ZDCDataFrame& digi, const std::vector<unsigned int>& myNoiseTS, const std::vector<unsigned int>& mySignalTS,const HcalCoder& coder, const HcalCalibrations& calibs) const {
182 
183  if(recoMethod_ == 1)
184  return ZdcSimpleRecAlgoImpl::reco1<ZDCDataFrame,ZDCRecHit>(digi,coder,calibs,
185  myNoiseTS,mySignalTS,false,
186  0,
188  if(recoMethod_ == 2)
189  return ZdcSimpleRecAlgoImpl::reco2<ZDCDataFrame,ZDCRecHit>(digi,coder,calibs,
190  myNoiseTS,mySignalTS,false,
192 
193  edm::LogError("ZDCSimpleRecAlgoImpl::reconstruct, recoMethod was not declared");
194  throw cms::Exception("ZDCSimpleRecoAlgos::exiting process");
195 
196 }
197 
198 static const float wpksamp0_zdc = 0.500053;
199 static const float scale_zdc = 0.999683;
200 static const int num_bins_zdc = 100;
201 
202 static const float actual_ns_zdc[num_bins_zdc] = {
203  0.00250, // 0.000-0.010
204  0.08000, // 0.010-0.020
205  0.16000, // 0.020-0.030
206  0.23750, // 0.030-0.040
207  0.31750, // 0.040-0.050
208  0.39500, // 0.050-0.060
209  0.47500, // 0.060-0.070
210  0.55500, // 0.070-0.080
211  0.63000, // 0.080-0.090
212  0.70000, // 0.090-0.100
213  0.77000, // 0.100-0.110
214  0.84000, // 0.110-0.120
215  0.91000, // 0.120-0.130
216  0.98000, // 0.130-0.140
217  1.05000, // 0.140-0.150
218  1.12000, // 0.150-0.160
219  1.19000, // 0.160-0.170
220  1.26000, // 0.170-0.180
221  1.33000, // 0.180-0.190
222  1.40000, // 0.190-0.200
223  1.47000, // 0.200-0.210
224  1.54000, // 0.210-0.220
225  1.61000, // 0.220-0.230
226  1.68000, // 0.230-0.240
227  1.75000, // 0.240-0.250
228  1.82000, // 0.250-0.260
229  1.89000, // 0.260-0.270
230  1.96000, // 0.270-0.280
231  2.03000, // 0.280-0.290
232  2.10000, // 0.290-0.300
233  2.17000, // 0.300-0.310
234  2.24000, // 0.310-0.320
235  2.31000, // 0.320-0.330
236  2.38000, // 0.330-0.340
237  2.45000, // 0.340-0.350
238  2.52000, // 0.350-0.360
239  2.59000, // 0.360-0.370
240  2.68500, // 0.370-0.380
241  2.79250, // 0.380-0.390
242  2.90250, // 0.390-0.400
243  3.01000, // 0.400-0.410
244  3.11750, // 0.410-0.420
245  3.22500, // 0.420-0.430
246  3.33500, // 0.430-0.440
247  3.44250, // 0.440-0.450
248  3.55000, // 0.450-0.460
249  3.73250, // 0.460-0.470
250  4.02000, // 0.470-0.480
251  4.30750, // 0.480-0.490
252  4.59500, // 0.490-0.500
253  6.97500, // 0.500-0.510
254 10.98750, // 0.510-0.520
255 13.03750, // 0.520-0.530
256 14.39250, // 0.530-0.540
257 15.39500, // 0.540-0.550
258 16.18250, // 0.550-0.560
259 16.85250, // 0.560-0.570
260 17.42750, // 0.570-0.580
261 17.91500, // 0.580-0.590
262 18.36250, // 0.590-0.600
263 18.76500, // 0.600-0.610
264 19.11250, // 0.610-0.620
265 19.46000, // 0.620-0.630
266 19.76500, // 0.630-0.640
267 20.03500, // 0.640-0.650
268 20.30250, // 0.650-0.660
269 20.57250, // 0.660-0.670
270 20.79250, // 0.670-0.680
271 21.00250, // 0.680-0.690
272 21.21000, // 0.690-0.700
273 21.42000, // 0.700-0.710
274 21.62750, // 0.710-0.720
275 21.79000, // 0.720-0.730
276 21.95250, // 0.730-0.740
277 22.11500, // 0.740-0.750
278 22.27750, // 0.750-0.760
279 22.44000, // 0.760-0.770
280 22.60500, // 0.770-0.780
281 22.73250, // 0.780-0.790
282 22.86000, // 0.790-0.800
283 22.98500, // 0.800-0.810
284 23.11250, // 0.810-0.820
285 23.23750, // 0.820-0.830
286 23.36500, // 0.830-0.840
287 23.49000, // 0.840-0.850
288 23.61750, // 0.850-0.860
289 23.71500, // 0.860-0.870
290 23.81250, // 0.870-0.880
291 23.91250, // 0.880-0.890
292 24.01000, // 0.890-0.900
293 24.10750, // 0.900-0.910
294 24.20750, // 0.910-0.920
295 24.30500, // 0.920-0.930
296 24.40500, // 0.930-0.940
297 24.50250, // 0.940-0.950
298 24.60000, // 0.950-0.960
299 24.68250, // 0.960-0.970
300 24.76250, // 0.970-0.980
301 24.84000, // 0.980-0.990
302 24.92000 // 0.990-1.000
303 };
304 
305 float timeshift_ns_zdc(float wpksamp) {
306  float flx = (num_bins_zdc*(wpksamp - wpksamp0_zdc)/scale_zdc);
307  int index = (int)flx;
308  float yval;
309 
310  if (index < 0) return actual_ns_zdc[0];
311  else if (index >= num_bins_zdc-1) return actual_ns_zdc[num_bins_zdc-1];
312 
313  // else interpolate:
314  float y1 = actual_ns_zdc[index];
315  float y2 = actual_ns_zdc[index+1];
316 
317  // float delta_x = 1/(float)num_bins_zdc;
318  // yval = y1 + (y2-y1)*(flx-(float)index)/delta_x;
319 
320  yval = y1 + (y2-y1)*(flx-(float)index);
321  return yval;
322 }
#define LogDebug(id)
int i
Definition: DBlmapReader.cc:9
double respcorrgain(int fCapId) const
get response corrected gain for capid=0..3
static const float scale_zdc
static const float wpksamp0_zdc
double getCorrection(double fc_ampl) const
RecHit reco2(const Digi &digi, const HcalCoder &coder, const HcalCalibrations &calibs, const std::vector< unsigned int > &myNoiseTS, const std::vector< unsigned int > &mySignalTS, bool slewCorrect, const HcalPulseContainmentCorrection *corr, HcalTimeSlew::BiasSetting slewFlavor)
double pedestal(int fCapId) const
get pedestal for capid=0..3
ZdcSimpleRecAlgo(bool correctForTimeslew, bool correctForContainment, float fixedPhaseNs, int recoMethod)
ZDCRecHit reconstruct(const ZDCDataFrame &digi, const std::vector< unsigned int > &myNoiseTS, const std::vector< unsigned int > &mySignalTS, const HcalCoder &coder, const HcalCalibrations &calibs) const
RecHit reco1(const Digi &digi, const HcalCoder &coder, const HcalCalibrations &calibs, const std::vector< unsigned int > &myNoiseTS, const std::vector< unsigned int > &mySignalTS, bool slewCorrect, const HcalPulseContainmentCorrection *corr, HcalTimeSlew::BiasSetting slewFlavor)
std::auto_ptr< HcalPulseContainmentCorrection > pulseCorr_
static float timeshift_ns_zdc(float wpksamp)
void initPulseCorr(int toadd)
const T & max(const T &a, const T &b)
static double MaximumFractionalError
static const float actual_ns_zdc[num_bins_zdc]
JetCorrectorParameters corr
Definition: classes.h:9
int size() const
get the size
Definition: CaloSamples.h:24
double timecorr() const
get time correction factor
virtual void adc2fC(const HBHEDataFrame &df, CaloSamples &lf) const =0
static const int num_bins_zdc
static double delay(double fC, BiasSetting bias=Medium)
Returns the amount (ns) by which a pulse of the given number of fC will be delayed by the timeslew ef...
Definition: HcalTimeSlew.cc:8