CMS 3D CMS Logo

DTT0Calibration.cc
Go to the documentation of this file.
1 /*
2  * See header file for a description of this class.
3  */
4 
7 
10 
13 
16 
19 
20 #include "TH1I.h"
21 #include "TFile.h"
22 #include "TKey.h"
23 #include "TSpectrum.h"
24 #include "TF1.h"
25 
26 using namespace std;
27 using namespace edm;
28 // using namespace cond;
29 
30 // Constructor
32  // Get the debug parameter for verbose output
33  debug = pset.getUntrackedParameter<bool>("debug");
34  if(debug)
35  cout << "[DTT0Calibration]Constructor called!" << endl;
36 
37  // Get the label to retrieve digis from the event
38  digiLabel = pset.getUntrackedParameter<string>("digiLabel");
39 
40  dbLabel = pset.getUntrackedParameter<string>("dbLabel", "");
41 
42  // The root file which contain the histos per layer
43  string rootFileName = pset.getUntrackedParameter<string>("rootFileName","DTT0PerLayer.root");
44  theFile = new TFile(rootFileName.c_str(), "RECREATE");
45 
46  theCalibWheel = pset.getUntrackedParameter<string>("calibWheel", "All"); //FIXME amke a vector of integer instead of a string
47  if(theCalibWheel != "All") {
48  stringstream linestr;
49  int selWheel;
50  linestr << theCalibWheel;
51  linestr >> selWheel;
52  cout << "[DTT0CalibrationPerLayer] chosen wheel " << selWheel << endl;
53  }
54 
55  // Sector/s to calibrate
56  theCalibSector = pset.getUntrackedParameter<string>("calibSector", "All"); //FIXME amke a vector of integer instead of a string
57  if(theCalibSector != "All") {
58  stringstream linestr;
59  int selSector;
60  linestr << theCalibSector;
61  linestr >> selSector;
62  cout << "[DTT0CalibrationPerLayer] chosen sector " << selSector << endl;
63  }
64 
65  vector<string> defaultCell;
66  defaultCell.push_back("None");
67  cellsWithHistos = pset.getUntrackedParameter<vector<string> >("cellsWithHisto", defaultCell);
68  for(vector<string>::const_iterator cell = cellsWithHistos.begin(); cell != cellsWithHistos.end(); ++cell){
69  if((*cell) != "None"){
70  stringstream linestr;
71  int wheel,sector,station,sl,layer,wire;
72  linestr << (*cell);
73  linestr >> wheel >> sector >> station >> sl >> layer >> wire;
74  wireIdWithHistos.push_back(DTWireId(wheel,station,sector,sl,layer,wire));
75  }
76  }
77 
78  hT0SectorHisto=nullptr;
79 
80  nevents=0;
81  eventsForLayerT0 = pset.getParameter<unsigned int>("eventsForLayerT0");
82  eventsForWireT0 = pset.getParameter<unsigned int>("eventsForWireT0");
83  tpPeakWidth = pset.getParameter<double>("tpPeakWidth");
84  tpPeakWidthPerLayer = pset.getParameter<double>("tpPeakWidthPerLayer");
85  timeBoxWidth = pset.getParameter<unsigned int>("timeBoxWidth");
86  rejectDigiFromPeak = pset.getParameter<unsigned int>("rejectDigiFromPeak");
87 
88  spectrum = new TSpectrum(5);
89  retryForLayerT0 = 0;
90 }
91 
92 // Destructor
94  if(debug)
95  cout << "[DTT0Calibration]Destructor called!" << endl;
96 
97  delete spectrum;
98  theFile->Close();
99 }
100 
102 void DTT0Calibration::analyze(const edm::Event & event, const edm::EventSetup& eventSetup) {
103  if(debug || event.id().event() % 500==0)
104  cout << "--- [DTT0Calibration] Analysing Event: #Run: " << event.id().run()
105  << " #Event: " << event.id().event() << endl;
106  nevents++;
107 
108  // Get the digis from the event
110  event.getByLabel(digiLabel, digis);
111 
112  // Get the DT Geometry
113  eventSetup.get<MuonGeometryRecord>().get(dtGeom);
114 
115  // Get ttrig DB
116  edm::ESHandle<DTTtrig> tTrigMap;
117  eventSetup.get<DTTtrigRcd>().get(dbLabel,tTrigMap);
118 
119  // Iterate through all digi collections ordered by LayerId
121  for (dtLayerIt = digis->begin();
122  dtLayerIt != digis->end();
123  ++dtLayerIt){
124  // Get the iterators over the digis associated with this LayerId
125  const DTDigiCollection::Range& digiRange = (*dtLayerIt).second;
126 
127  // Get the layerId
128  const DTLayerId layerId = (*dtLayerIt).first; //FIXME: check to be in the right sector
129  const DTChamberId chamberId = layerId.superlayerId().chamberId();
130 
131  if((theCalibWheel != "All") && (layerId.superlayerId().chamberId().wheel() != selWheel))
132  continue;
133  if((theCalibSector != "All") && (layerId.superlayerId().chamberId().sector() != selSector))
134  continue;
135 
136  //if(debug) {
137  // cout << "Layer " << layerId<<" with "<<distance(digiRange.first, digiRange.second)<<" digi"<<endl;
138  //}
139 
140  float tTrig,tTrigRMS, kFactor;
141  tTrigMap->get(layerId.superlayerId(), tTrig, tTrigRMS, kFactor, DTTimeUnits::counts );
142  if(debug&&(nevents <= 1)){
143  cout << " Superlayer: " << layerId.superlayerId() << endl
144  << " tTrig,tTrigRMS= " << tTrig << ", " << tTrigRMS << endl;
145  }
146 
147  // Loop over all digis in the given layer
148  for (DTDigiCollection::const_iterator digi = digiRange.first;
149  digi != digiRange.second;
150  ++digi) {
151  double t0 = (*digi).countsTDC();
152 
153  //Use first bunch of events to fill t0 per layer
154  if(nevents < eventsForLayerT0){
155  //Get the per-layer histo from the map
156  TH1I *hT0LayerHisto = theHistoLayerMap[layerId];
157  //If it doesn't exist, book it
158  if(hT0LayerHisto == nullptr){
159  theFile->cd();
160  float hT0Min = tTrig - 2*tTrigRMS;
161  float hT0Max = hT0Min + timeBoxWidth;
162  hT0LayerHisto = new TH1I(getHistoName(layerId).c_str(),
163  "T0 from pulses by layer (TDC counts, 1 TDC count = 0.781 ns)",
164  timeBoxWidth,hT0Min,hT0Max);
165  if(debug)
166  cout << " New T0 per Layer Histo: " << hT0LayerHisto->GetName() << endl;
167  theHistoLayerMap[layerId] = hT0LayerHisto;
168  }
169 
170  //Fill the histos
171  theFile->cd();
172  if(hT0LayerHisto != nullptr) {
173  // if(debug)
174  // cout<<"Filling histo "<<hT0LayerHisto->GetName()<<" with digi "<<t0<<" TDC counts"<<endl;
175  hT0LayerHisto->Fill(t0);
176  }
177  }
178 
179  //Use all the remaining events to compute t0 per wire
180  if(nevents>eventsForLayerT0){
181  // Get the wireId
182  const DTWireId wireId(layerId, (*digi).wire());
183  if(debug) {
184  cout << " Wire: " << wireId << endl
185  << " time (TDC counts): " << (*digi).countsTDC()<< endl;
186  }
187 
188  //Fill the histos per wire for the chosen cells
189  vector<DTWireId>::iterator it = find(wireIdWithHistos.begin(),wireIdWithHistos.end(),wireId);
190  if (it!=wireIdWithHistos.end()){
191  //Get the per-wire histo from the map
192  TH1I *hT0WireHisto = theHistoWireMap[wireId];
193  //If it doesn't exist, book it
194  if(hT0WireHisto == nullptr){
195  theFile->cd();
196  hT0WireHisto = new TH1I(getHistoName(wireId).c_str(),"T0 from pulses by wire (TDC counts, 1 TDC count = 0.781 ns)",7000,0,7000);
197  //hT0SectorHisto->GetBinCenter(hT0SectorHisto->GetMaximumBin())-100,
198  //hT0SectorHisto->GetBinCenter(hT0SectorHisto->GetMaximumBin())+100);
199  if(debug)
200  cout << " New T0 per wire Histo: " << hT0WireHisto->GetName() << endl;
201  theHistoWireMap[wireId] = hT0WireHisto;
202  }
203  //Fill the histos
204  theFile->cd();
205  if(hT0WireHisto != nullptr) {
206  //if(debug)
207  // cout<<"Filling histo "<<hT0WireHisto->GetName()<<" with digi "<<t0<<" TDC counts"<<endl;
208  hT0WireHisto->Fill(t0);
209  }
210  }
211 
212  //Check the tzero has reasonable value
213  //float hT0Min = tTrig - 2*tTrigRMS;
214  //float hT0Max = hT0Min + timeBoxWidth;
215  /*if(abs(hT0SectorHisto->GetBinCenter(hT0SectorHisto->GetMaximumBin()) - t0) > rejectDigiFromPeak){
216  if(debug)
217  cout<<"digi skipped because t0 per sector "<<hT0SectorHisto->GetBinCenter(hT0SectorHisto->GetMaximumBin())<<endl;
218  continue;
219  }*/
220  /*if((t0 < hT0Min)||(t0 > hT0Max)){
221  if(debug)
222  cout<<"digi skipped because t0 outside of interval (" << hT0Min << "," << hT0Max << ")" <<endl;
223  continue;
224  }*/
225  //Select per layer
226  if(fabs(theTPPeakMap[layerId] - t0) > rejectDigiFromPeak){
227  if(debug)
228  cout<<"digi skipped because t0 too far from peak " << theTPPeakMap[layerId] << endl;
229  continue;
230  }
231 
232  //Find to ref. per chamber
233  theSumT0ByChamber[chamberId] = theSumT0ByChamber[chamberId] + t0;
234  theCountT0ByChamber[chamberId]++;
235 
236  //Use second bunch of events to compute a t0 reference per wire
237  if(nevents< (eventsForLayerT0 + eventsForWireT0)){
238  if(!nDigiPerWire_ref[wireId]){
239  mK_ref[wireId] = 0;
240  }
241  nDigiPerWire_ref[wireId] = nDigiPerWire_ref[wireId] + 1;
242  mK_ref[wireId] = mK_ref[wireId] + (t0-mK_ref[wireId])/nDigiPerWire_ref[wireId];
243  }
244  //Use last all the remaining events to compute the mean and sigma t0 per wire
245  else if(nevents>(eventsForLayerT0 + eventsForWireT0)){
246  if(abs(t0-mK_ref[wireId]) > tpPeakWidth)
247  continue;
248  if(!nDigiPerWire[wireId]){
249  theAbsoluteT0PerWire[wireId] = 0;
250  qK[wireId] = 0;
251  mK[wireId] = 0;
252  }
253  nDigiPerWire[wireId] = nDigiPerWire[wireId] + 1;
254  theAbsoluteT0PerWire[wireId] = theAbsoluteT0PerWire[wireId] + t0;
255  //theSigmaT0PerWire[wireId] = theSigmaT0PerWire[wireId] + (t0*t0);
256  qK[wireId] = qK[wireId] + ((nDigiPerWire[wireId]-1)*(t0-mK[wireId])*(t0-mK[wireId])/nDigiPerWire[wireId]);
257  mK[wireId] = mK[wireId] + (t0-mK[wireId])/nDigiPerWire[wireId];
258  }
259  }//end if(nevents>1000)
260  }//end loop on digi
261  }//end loop on layer
262 
263  //Use the t0 per layer histos to have an indication about the t0 position
264  if(nevents == eventsForLayerT0){
265  bool increaseEvtsForLayerT0 = false;
266  for(map<DTLayerId, TH1I*>::const_iterator lHisto = theHistoLayerMap.begin();
267  lHisto != theHistoLayerMap.end();
268  ++lHisto){
269  if(debug)
270  cout<<"Reading histogram "<<(*lHisto).second->GetName()<<" with mean "<<(*lHisto).second->GetMean()<<" and RMS "<<(*lHisto).second->GetRMS() << endl;
271 
272  //Find peaks
273  //int npeaks = spectrum->Search((*lHisto).second,0.5,"goff");
274  //int npeaks = spectrum->Search((*lHisto).second,(tpPeakWidthPerLayer/2.),"goff",0.3);
275  int npeaks = spectrum->Search((*lHisto).second,(tpPeakWidthPerLayer/2.),"",0.3);
276 
277  double *peaks = spectrum->GetPositionX();
278  //Put in a std::vector<float>
279  vector<double> peakMeans(peaks,peaks + npeaks);
280  //Sort the peaks in ascending order
281  sort(peakMeans.begin(),peakMeans.end());
282 
283  //Find best peak -- preliminary criteria: find peak closest to center of time box
284  float tTrig,tTrigRMS, kFactor;
285  tTrigMap->get((*lHisto).first.superlayerId(), tTrig, tTrigRMS, kFactor, DTTimeUnits::counts );
286 
287  double timeBoxCenter = (2*tTrig + (float)timeBoxWidth)/2.;
288  double hMin = (*lHisto).second->GetXaxis()->GetXmin();
289  double hMax = (*lHisto).second->GetXaxis()->GetXmax();
290  vector<double>::const_iterator tpPeak = peakMeans.end();
291  for(vector<double>::const_iterator it = peakMeans.begin(); it != peakMeans.end(); ++it){
292  double mean = *it;
293 
294  int bin = (*lHisto).second->GetXaxis()->FindBin(mean);
295  double yp = (*lHisto).second->GetBinContent(bin);
296  if(debug) cout << "Peak : (" << mean << "," << yp << ")" << endl;
297 
298  //Find RMS
299  double previous_peak = (it == peakMeans.begin())?hMin:*(it - 1);
300  double next_peak = (it == (peakMeans.end()-1))?hMax:*(it + 1);
301 
302  double rangemin = mean - (mean - previous_peak)/8.;
303  double rangemax = mean + (next_peak - mean)/8.;
304  int binmin = (*lHisto).second->GetXaxis()->FindBin(rangemin);
305  int binmax = (*lHisto).second->GetXaxis()->FindBin(rangemax);
306  (*lHisto).second->GetXaxis()->SetRange(binmin,binmax);
307  //RMS estimate
308  double rms_seed = (*lHisto).second->GetRMS();
309 
310  /*rangemin = mean - 2*rms_seed;
311  rangemax = mean + 2*rms_seed;
312  if(debug) cout << "Seed for RMS, Fit min, Fit max: " << rms_seed << ", " << rangemin << ", " << rangemax << endl;
313  //Fit to gaussian
314  string funcname("fitFcn_");
315  funcname += (*lHisto).second->GetName();
316  if(debug) cout << "Fitting function " << funcname << endl;
317  TF1* func = new TF1(funcname.c_str(),"gaus",rangemin,rangemax);
318  func->SetParameters(yp,mean,rms_seed);
319  (*lHisto).second->Fit(func,"Q","",rangemin,rangemax);
320  float fitconst = func->GetParameter(0);
321  float fitmean = func->GetParameter(1);
322  float fitrms = func->GetParameter(2);
323  float chisquare = func->GetChisquare()/func->GetNDF();
324  if(debug) cout << "Gaussian fit constant,mean,RMS,chi2= " << fitconst << ", " << fitmean << ", " << fitrms << ", " << chisquare << endl;*/
325 
326  //Reject peaks with RMS larger than specified
327  //if(fitrms > tpPeakWidth) continue;
328  if(rms_seed > tpPeakWidthPerLayer) continue;
329 
330  if(fabs(mean - timeBoxCenter) < fabs(*tpPeak - timeBoxCenter)) tpPeak = it;
331  }
332  //Didn't find peak
333  /*if(tpPeak == peakMeans.end()){
334  if(retryForLayerT0 < 2){
335  increaseEvtsForLayerT0 = true;
336  retryForLayerT0++;
337  break;
338  }
339  }*/
340 
341  double selPeak = (tpPeak != peakMeans.end())?*tpPeak:(*lHisto).second->GetBinCenter((*lHisto).second->GetMaximumBin());
342  if(debug) cout << "Peak selected at " << selPeak << endl;
343 
344  theTPPeakMap[(*lHisto).first] = selPeak;
345 
346  //Take the mean as a first t0 estimation
347  /*if((*lHisto).second->GetRMS() < tpPeakWidth){
348  if(hT0SectorHisto == 0){
349  hT0SectorHisto = new TH1D("hT0AllLayerOfSector","T0 from pulses per layer in sector",
350  //20, (*lHisto).second->GetMean()-100, (*lHisto).second->GetMean()+100);
351  700, 0, 7000);
352  //300,3300,3600);
353  }
354  if(debug)
355  cout<<" accepted"<<endl;
356  //TH1I* aux_histo = (*lHisto).second;
357  //aux_histo->GetXaxis()->SetRangeUser(3300,3600);
358  hT0SectorHisto->Fill((*lHisto).second->GetMean());
359  //hT0SectorHisto->Fill(aux_histo->GetMean());
360  }
361  //Take the mean of noise + 400ns as a first t0 estimation
362  //if((*lHisto).second->GetRMS()>10.0 && ((*lHisto).second->GetRMS()<15.0)){
363  //double t0_estim = (*lHisto).second->GetMean() + 400;
364  //if(hT0SectorHisto == 0){
365  // hT0SectorHisto = new TH1D("hT0AllLayerOfSector","T0 from pulses per layer in sector",
366  // //20, t0_estim-100, t0_estim+100);
367  // 700, 0, 7000);
368  //}
369  //if(debug)
370  // cout<<" accepted + 400ns"<<endl;
371  //hT0SectorHisto->Fill((*lHisto).second->GetMean() + 400);
372  //}
373  if(debug)
374  cout<<endl;
375  theT0LayerMap[(*lHisto).second->GetName()] = (*lHisto).second->GetMean();
376  theSigmaT0LayerMap[(*lHisto).second->GetName()] = (*lHisto).second->GetRMS();*/
377  }
378  /*if(!hT0SectorHisto){
379  cout<<"[DTT0Calibration]: All the t0 per layer are still uncorrect: trying with greater number of events"<<endl;
380  eventsForLayerT0 = eventsForLayerT0*2;
381  return;
382  }
383  if(debug)
384  cout<<"[DTT0Calibration] t0 reference for this sector "<<
385  hT0SectorHisto->GetBinCenter(hT0SectorHisto->GetMaximumBin())<<endl;*/
386  if(increaseEvtsForLayerT0){
387  cout<<"[DTT0Calibration]: t0 per layer are still uncorrect: trying with greater number of events"<<endl;
388  eventsForLayerT0 = eventsForLayerT0*2;
389  return;
390  }
391  }
392 }
393 
395 
396  DTT0* t0s = new DTT0();
397  DTT0* t0sWRTChamber = new DTT0();
398 
399  if(debug)
400  cout << "[DTT0CalibrationPerLayer]Writing histos to file!" << endl;
401 
402  theFile->cd();
403  //hT0SectorHisto->Write();
404  for(map<DTWireId, TH1I*>::const_iterator wHisto = theHistoWireMap.begin();
405  wHisto != theHistoWireMap.end();
406  ++wHisto) {
407  (*wHisto).second->Write();
408  }
409  for(map<DTLayerId, TH1I*>::const_iterator lHisto = theHistoLayerMap.begin();
410  lHisto != theHistoLayerMap.end();
411  ++lHisto) {
412  (*lHisto).second->Write();
413  }
414 
415  if(debug)
416  cout << "[DTT0Calibration] Compute and store t0 and sigma per wire" << endl;
417 
418  for(map<DTChamberId,double>::const_iterator chamber = theSumT0ByChamber.begin();
419  chamber != theSumT0ByChamber.end();
420  ++chamber) theRefT0ByChamber[(*chamber).first] = theSumT0ByChamber[(*chamber).first]/((double)theCountT0ByChamber[(*chamber).first]);
421 
422  for(map<DTWireId, double>::const_iterator wiret0 = theAbsoluteT0PerWire.begin();
423  wiret0 != theAbsoluteT0PerWire.end();
424  ++wiret0){
425  if(nDigiPerWire[(*wiret0).first]){
426  double t0 = (*wiret0).second/nDigiPerWire[(*wiret0).first];
427  DTChamberId chamberId = ((*wiret0).first).chamberId();
428  //theRelativeT0PerWire[(*wiret0).first] = t0 - hT0SectorHisto->GetBinCenter(hT0SectorHisto->GetMaximumBin());
429  theRelativeT0PerWire[(*wiret0).first] = t0 - theRefT0ByChamber[chamberId];
430  cout<<"Wire "<<(*wiret0).first<<" has t0 "<<t0<<"(absolute) "<<theRelativeT0PerWire[(*wiret0).first]<<"(relative)";
431 
432  //theSigmaT0PerWire[(*wiret0).first] = sqrt((theSigmaT0PerWire[(*wiret0).first] / nDigiPerWire[(*wiret0).first]) - t0*t0);
433  theSigmaT0PerWire[(*wiret0).first] = sqrt(qK[(*wiret0).first]/nDigiPerWire[(*wiret0).first]);
434  cout<<" sigma "<<theSigmaT0PerWire[(*wiret0).first]<<endl;
435  }
436  else{
437  cout<<"[DTT0CalibrationNew] ERROR: no digis in wire "<<(*wiret0).first<<endl;
438  abort();
439  }
440  }
441 
443  // Get all the sls from the setup
444  const vector<const DTSuperLayer*> superLayers = dtGeom->superLayers();
445  // Loop over all SLs
446  for(vector<const DTSuperLayer*>::const_iterator sl = superLayers.begin();
447  sl != superLayers.end(); sl++) {
448 
449 
450  //Compute mean for odd and even superlayers
451  double oddLayersMean=0;
452  double evenLayersMean=0;
453  double oddLayersDen=0;
454  double evenLayersDen=0;
455  for(map<DTWireId, double>::const_iterator wiret0 = theRelativeT0PerWire.begin();
456  wiret0 != theRelativeT0PerWire.end();
457  ++wiret0){
458  if((*wiret0).first.layerId().superlayerId() == (*sl)->id()){
459  if(debug)
460  cout<<"[DTT0Calibration] Superlayer "<<(*sl)->id()
461  <<"layer " <<(*wiret0).first.layerId().layer()<<" with "<<(*wiret0).second<<endl;
462  if(((*wiret0).first.layerId().layer()) % 2){
463  oddLayersMean = oddLayersMean + (*wiret0).second;
464  oddLayersDen++;
465  }
466  else{
467  evenLayersMean = evenLayersMean + (*wiret0).second;
468  evenLayersDen++;
469  }
470  }
471  }
472  oddLayersMean = oddLayersMean/oddLayersDen;
473  evenLayersMean = evenLayersMean/evenLayersDen;
474  if(debug && oddLayersMean)
475  cout<<"[DTT0Calibration] Relative T0 mean for odd layers "<<oddLayersMean<<" even layers"<<evenLayersMean<<endl;
476 
477  //Compute sigma for odd and even superlayers
478  double oddLayersSigma=0;
479  double evenLayersSigma=0;
480  for(map<DTWireId, double>::const_iterator wiret0 = theRelativeT0PerWire.begin();
481  wiret0 != theRelativeT0PerWire.end();
482  ++wiret0){
483  if((*wiret0).first.layerId().superlayerId() == (*sl)->id()){
484  if(((*wiret0).first.layerId().layer()) % 2){
485  oddLayersSigma = oddLayersSigma + ((*wiret0).second - oddLayersMean) * ((*wiret0).second - oddLayersMean);
486  }
487  else{
488  evenLayersSigma = evenLayersSigma + ((*wiret0).second - evenLayersMean) * ((*wiret0).second - evenLayersMean);
489  }
490  }
491  }
492  oddLayersSigma = oddLayersSigma/oddLayersDen;
493  evenLayersSigma = evenLayersSigma/evenLayersDen;
494  oddLayersSigma = sqrt(oddLayersSigma);
495  evenLayersSigma = sqrt(evenLayersSigma);
496 
497  if(debug && oddLayersMean)
498  cout<<"[DTT0Calibration] Relative T0 sigma for odd layers "<<oddLayersSigma<<" even layers"<<evenLayersSigma<<endl;
499 
500  //Recompute the mean for odd and even superlayers discarding fluctations
501  double oddLayersFinalMean=0;
502  double evenLayersFinalMean=0;
503  for(map<DTWireId, double>::const_iterator wiret0 = theRelativeT0PerWire.begin();
504  wiret0 != theRelativeT0PerWire.end();
505  ++wiret0){
506  if((*wiret0).first.layerId().superlayerId() == (*sl)->id()){
507  if(((*wiret0).first.layerId().layer()) % 2){
508  if(abs((*wiret0).second - oddLayersMean) < (2*oddLayersSigma))
509  oddLayersFinalMean = oddLayersFinalMean + (*wiret0).second;
510  }
511  else{
512  if(abs((*wiret0).second - evenLayersMean) < (2*evenLayersSigma))
513  evenLayersFinalMean = evenLayersFinalMean + (*wiret0).second;
514  }
515  }
516  }
517  oddLayersFinalMean = oddLayersFinalMean/oddLayersDen;
518  evenLayersFinalMean = evenLayersFinalMean/evenLayersDen;
519  if(debug && oddLayersMean)
520  cout<<"[DTT0Calibration] Final relative T0 mean for odd layers "<<oddLayersFinalMean<<" even layers"<<evenLayersFinalMean<<endl;
521 
522  for(map<DTWireId, double>::const_iterator wiret0 = theRelativeT0PerWire.begin();
523  wiret0 != theRelativeT0PerWire.end();
524  ++wiret0){
525  if((*wiret0).first.layerId().superlayerId() == (*sl)->id()){
526  double t0=-999;
527  if(((*wiret0).first.layerId().layer()) % 2)
528  t0 = (*wiret0).second + (evenLayersFinalMean - oddLayersFinalMean);
529  else
530  t0 = (*wiret0).second;
531 
532  cout<<"[DTT0Calibration] Wire "<<(*wiret0).first<<" has t0 "<<(*wiret0).second<<" (relative, after even-odd layer corrections) "
533  <<" sigma "<<theSigmaT0PerWire[(*wiret0).first]<<endl;
534  //Store the results into DB
535  t0s->set((*wiret0).first, t0, theSigmaT0PerWire[(*wiret0).first],DTTimeUnits::counts);
536  }
537  }
538  }
539 
541  if(debug)
542  cout << "[DTT0Calibration]Computing relative t0 wrt to chamber average" << endl;
543  //Compute the reference for each chamber
544  map<DTChamberId,double> sumT0ByChamber;
545  map<DTChamberId,int> countT0ByChamber;
546  for(DTT0::const_iterator tzero = t0s->begin();
547  tzero != t0s->end(); ++tzero) {
548 // @@@ NEW DTT0 FORMAT
549 // DTChamberId chamberId((*tzero).first.wheelId,
550 // (*tzero).first.stationId,
551 // (*tzero).first.sectorId);
552 // sumT0ByChamber[chamberId] = sumT0ByChamber[chamberId] + (*tzero).second.t0mean;
553  int channelId = tzero->channelId;
554  if ( channelId == 0 ) continue;
555  DTWireId wireId(channelId);
556  DTChamberId chamberId(wireId.chamberId());
557  //sumT0ByChamber[chamberId] = sumT0ByChamber[chamberId] + tzero->t0mean;
558 // @@@ better DTT0 usage
559  float t0mean_f;
560  float t0rms_f;
561  t0s->get(wireId,t0mean_f,t0rms_f,DTTimeUnits::counts);
562  sumT0ByChamber[chamberId] = sumT0ByChamber[chamberId] + t0mean_f;
563 // @@@ NEW DTT0 END
564  countT0ByChamber[chamberId]++;
565  }
566 
567  //Change reference for each wire and store the new t0s in the new map
568  for(DTT0::const_iterator tzero = t0s->begin();
569  tzero != t0s->end(); ++tzero) {
570 // @@@ NEW DTT0 FORMAT
571 // DTChamberId chamberId((*tzero).first.wheelId,
572 // (*tzero).first.stationId,
573 // (*tzero).first.sectorId);
574 // double t0mean = ((*tzero).second.t0mean) - (sumT0ByChamber[chamberId]/countT0ByChamber[chamberId]);
575 // double t0rms = (*tzero).second.t0rms;
576 // DTWireId wireId((*tzero).first.wheelId,
577 // (*tzero).first.stationId,
578 // (*tzero).first.sectorId,
579 // (*tzero).first.slId,
580 // (*tzero).first.layerId,
581 // (*tzero).first.cellId);
582  int channelId = tzero->channelId;
583  if ( channelId == 0 ) continue;
584  DTWireId wireId( channelId );
585  DTChamberId chamberId(wireId.chamberId());
586  //double t0mean = (tzero->t0mean) - (sumT0ByChamber[chamberId]/countT0ByChamber[chamberId]);
587  //double t0rms = tzero->t0rms;
588 // @@@ better DTT0 usage
589  float t0mean_f;
590  float t0rms_f;
591  t0s->get(wireId,t0mean_f,t0rms_f,DTTimeUnits::counts);
592  double t0mean = t0mean_f - (sumT0ByChamber[chamberId]/countT0ByChamber[chamberId]);
593  double t0rms = t0rms_f;
594 // @@@ NEW DTT0 END
595  t0sWRTChamber->set(wireId,
596  t0mean,
597  t0rms,
599  if(debug){
600  //cout<<"Chamber "<<chamberId<<" has reference "<<(sumT0ByChamber[chamberId]/countT0ByChamber[chamberId]);
601 // cout<<"Changing t0 of wire "<<wireId<<" from "<<(*tzero).second.t0mean<<" to "<<t0mean<<endl;
602  cout<<"Changing t0 of wire "<<wireId<<" from "<<tzero->t0mean<<" to "<<t0mean<<endl;
603  }
604  }
605 
607  if(debug)
608  cout << "[DTT0Calibration]Writing values in DB!" << endl;
609  // FIXME: to be read from cfg?
610  string t0Record = "DTT0Rcd";
611  // Write the t0 map to DB
612  DTCalibDBUtils::writeToDB(t0Record, t0sWRTChamber);
613 }
614 
615 string DTT0Calibration::getHistoName(const DTWireId& wId) const {
616  string histoName;
617  stringstream theStream;
618  theStream << "Ch_" << wId.wheel() << "_" << wId.station() << "_" << wId.sector()
619  << "_SL" << wId.superlayer() << "_L" << wId.layer() << "_W"<< wId.wire() <<"_hT0Histo";
620  theStream >> histoName;
621  return histoName;
622 }
623 
624 string DTT0Calibration::getHistoName(const DTLayerId& lId) const {
625  string histoName;
626  stringstream theStream;
627  theStream << "Ch_" << lId.wheel() << "_" << lId.station() << "_" << lId.sector()
628  << "_SL" << lId.superlayer() << "_L" << lId.layer() <<"_hT0Histo";
629  theStream >> histoName;
630  return histoName;
631 }
632 
const_iterator begin() const
Definition: DTT0.cc:204
RunNumber_t run() const
Definition: EventID.h:39
int set(int wheelId, int stationId, int sectorId, int slId, int layerId, int cellId, float t0mean, float t0rms, DTTimeUnits::type unit)
Definition: DTT0.cc:140
T getParameter(std::string const &) const
EventNumber_t event() const
Definition: EventID.h:41
T getUntrackedParameter(std::string const &, T const &) const
void endJob() override
Compute the mean and the RMS of the t0 from the maps and write them to the DB with channel granularit...
DTChamberId chamberId() const
Return the corresponding ChamberId.
std::string getHistoName(const DTWireId &wId) const
int layer() const
Return the layer number.
Definition: DTLayerId.h:53
DTSuperLayerId superlayerId() const
Return the corresponding SuperLayerId.
Definition: DTLayerId.h:59
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
int get(int wheelId, int stationId, int sectorId, int slId, int layerId, int cellId, float &t0mean, float &t0rms, DTTimeUnits::type unit) const
Definition: DTT0.cc:67
std::vector< DTT0Data >::const_iterator const_iterator
Access methods to data.
Definition: DTT0.h:140
Definition: DTT0.h:53
def getHistoName(wheel, station, sector)
T sqrt(T t)
Definition: SSEVec.h:18
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
~DTT0Calibration() override
Destructor.
void analyze(const edm::Event &event, const edm::EventSetup &eventSetup) override
Fill the maps with t0 (by channel)
bin
set the eta bin as selection string.
int wire() const
Return the wire number.
Definition: DTWireId.h:56
const_iterator end() const
Definition: DTT0.cc:209
int superlayer() const
Return the superlayer number (deprecated method name)
int get(int wheelId, int stationId, int sectorId, int slId, float &tTrig, float &tTrms, float &kFact, DTTimeUnits::type unit) const
get content
Definition: DTTtrig.cc:85
#define debug
Definition: HDRShower.cc:19
std::vector< DTDigi >::const_iterator const_iterator
edm::EventID id() const
Definition: EventBase.h:60
HLT enums.
int sector() const
Definition: DTChamberId.h:61
T get() const
Definition: EventSetup.h:62
static const double tzero[3]
std::pair< const_iterator, const_iterator > Range
int station() const
Return the station number.
Definition: DTChamberId.h:51
DTT0Calibration(const edm::ParameterSet &pset)
Constructor.
int wheel() const
Return the wheel number.
Definition: DTChamberId.h:45
static void writeToDB(std::string record, T *payload)
Definition: event.py:1