CMS 3D CMS Logo

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