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  * $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 "TKey.h"
26 #include "TF1.h"
27 
28 #include <cassert>
29 
30 using namespace std;
31 using namespace edm;
32 
33 // Constructor
35  debug(pset.getUntrackedParameter<bool>("debug")),
36  digiToken(consumes<DTDigiCollection>(pset.getUntrackedParameter<string>("digiLabel"))),
37  theFile(pset.getUntrackedParameter<string>("rootFileName", "DTT0PerLayer.root").c_str(), "RECREATE"),
38  nevents(0),
39  eventsForLayerT0(pset.getParameter<unsigned int>("eventsForLayerT0")),
40  eventsForWireT0(pset.getParameter<unsigned int>("eventsForWireT0")),
41  tpPeakWidth(pset.getParameter<double>("tpPeakWidth")),
42  tpPeakWidthPerLayer(pset.getParameter<double>("tpPeakWidthPerLayer")),
43  rejectDigiFromPeak(pset.getParameter<unsigned int>("rejectDigiFromPeak")),
44  hLayerPeaks("hLayerPeaks", "", 3000, 0, 3000),
45  spectrum(20)
46 
47 {
48  // Get the debug parameter for verbose output
49  if(debug)
50  cout << "[DTT0Calibration]Constructor called!" << endl;
51 
52  theCalibWheel = pset.getUntrackedParameter<string>("calibWheel", "All"); //FIXME amke a vector of integer instead of a string
53  if(theCalibWheel != "All") {
54  stringstream linestr;
55  int selWheel;
56  linestr << theCalibWheel;
57  linestr >> selWheel;
58  cout << "[DTT0CalibrationPerLayer] chosen wheel " << selWheel << endl;
59  }
60 
61  // Sector/s to calibrate
62  theCalibSector = pset.getUntrackedParameter<string>("calibSector", "All"); //FIXME amke a vector of integer instead of a string
63  if(theCalibSector != "All") {
64  stringstream linestr;
65  int selSector;
66  linestr << theCalibSector;
67  linestr >> selSector;
68  cout << "[DTT0CalibrationPerLayer] chosen sector " << selSector << endl;
69  }
70 
71  vector<string> defaultCell;
72  const auto& cellsWithHistos = pset.getUntrackedParameter<vector<string> >("cellsWithHisto", defaultCell);
73  for(const auto& cell : cellsWithHistos){
74  stringstream linestr;
75  int wheel, sector, station, sl, layer, wire;
76  linestr << cell;
77  linestr >> wheel >> sector >> station >> sl >> layer >> wire;
78  wireIdWithHistos.push_back(DTWireId(wheel, station, sector, sl, layer, wire));
79  }
80 }
81 
82 // Destructor
84  if(debug)
85  cout << "[DTT0Calibration]Destructor called!" << endl;
86 
87  theFile.Close();
88 }
89 
91 void DTT0Calibration::analyze(const edm::Event & event, const edm::EventSetup& eventSetup) {
92  nevents++;
93 
94  // Get the digis from the event
96  event.getByToken(digiToken, digis);
97 
98  // Get the DT Geometry
99  if (nevents == 1)
100  eventSetup.get<MuonGeometryRecord>().get(dtGeom);
101 
102  // Iterate through all digi collections ordered by LayerId
103  for (const auto& digis_per_layer : *digis)
104  {
105  //std::cout << __LINE__ << std::endl;
106  // Get the iterators over the digis associated with this LayerId
107  const DTDigiCollection::Range& digiRange = digis_per_layer.second;
108 
109  // Get the layerId
110  const DTLayerId layerId = digis_per_layer.first;
111  //const DTChamberId chamberId = layerId.superlayerId().chamberId();
112 
113  if((theCalibWheel != "All") && (layerId.superlayerId().chamberId().wheel() != selWheel))
114  continue;
115  if((theCalibSector != "All") && (layerId.superlayerId().chamberId().sector() != selSector))
116  continue;
117 
118  // Loop over all digis in the given layer
119  for (DTDigiCollection::const_iterator digi = digiRange.first;
120  digi != digiRange.second;
121  ++digi)
122  {
123  const double t0 = digi->countsTDC();
124  const DTWireId wireIdtmp(layerId, (*digi).wire());
125 
126  // Use first bunch of events to fill t0 per layer
127  if(nevents <= eventsForLayerT0){
128  // If it doesn't exist, book it
129  if(not theHistoLayerMap.count(layerId)){
130  theHistoLayerMap[layerId] = TH1I(getHistoName(layerId).c_str(),
131  "T0 from pulses by layer (TDC counts, 1 TDC count = 0.781 ns)",
132  3000,
133  0,
134  3000
135  );
136  if(debug)
137  cout << " New T0 per Layer Histo: " << theHistoLayerMap[layerId].GetName() << endl;
138  }
139  theHistoLayerMap[layerId].Fill(t0);
140  }
141 
142  // Use all the remaining events to compute t0 per wire
144  // Get the wireId
145  const DTWireId wireId(layerId, (*digi).wire());
146  if(debug) {
147  cout << " Wire: " << wireId << endl
148  << " time (TDC counts): " << (*digi).countsTDC()<< endl;
149  }
150 
151  //Fill the histos per wire for the chosen cells
153  std::find(wireIdWithHistos.begin(),wireIdWithHistos.end(),wireId) != wireIdWithHistos.end())
154  {
155  //If it doesn't exist, book it
156  if(theHistoWireMap.count(wireId) == 0){
157  theHistoWireMap[wireId] = TH1I(getHistoName(wireId).c_str(),
158  "T0 from pulses by wire (TDC counts, 1 TDC count = 0.781 ns)",
159  7000,
160  0,
161  7000
162  );
163  if(debug)
164  cout << " New T0 per wire Histo: " << theHistoWireMap[wireId].GetName() << endl;
165  }
166  theHistoWireMap[wireId].Fill(t0);
167  }
168 
169  //Select per layer
170  if(fabs(theTPPeakMap[layerId] - t0) > rejectDigiFromPeak){
171  if(debug)
172  cout<<"digi skipped because t0 too far from peak " << theTPPeakMap[layerId] << endl;
173  continue;
174  }
175 
176  //Use second bunch of events to compute a t0 reference per wire
178  if(!nDigiPerWire_ref[wireId]){
179  mK_ref[wireId] = 0;
180  }
181  nDigiPerWire_ref[wireId] = nDigiPerWire_ref[wireId] + 1;
182  mK_ref[wireId] = mK_ref[wireId] + (t0-mK_ref[wireId])/nDigiPerWire_ref[wireId];
183  }
184  //Use last all the remaining events to compute the mean and sigma t0 per wire
186  if(abs(t0-mK_ref[wireId]) > tpPeakWidth)
187  continue;
188  if(!nDigiPerWire[wireId]){
189  theAbsoluteT0PerWire[wireId] = 0;
190  qK[wireId] = 0;
191  mK[wireId] = 0;
192  }
193  nDigiPerWire[wireId] = nDigiPerWire[wireId] + 1;
194  theAbsoluteT0PerWire[wireId] = theAbsoluteT0PerWire[wireId] + t0;
195  qK[wireId] = qK[wireId] + ((nDigiPerWire[wireId]-1)*(t0-mK[wireId])*(t0-mK[wireId])/nDigiPerWire[wireId]);
196  mK[wireId] = mK[wireId] + (t0-mK[wireId])/nDigiPerWire[wireId];
197  }
198  }//end if(nevents>1000)
199  }//end loop on digi
200  }//end loop on layer
201 
202  //Use the t0 per layer histos to have an indication about the t0 position
203  if(nevents == eventsForLayerT0){
204  for(const auto& lHisto : theHistoLayerMap)
205  {
206  const auto& layerId = lHisto.first;
207  const auto& hist = lHisto.second;
208  if(debug)
209  cout << "Reading histogram " << hist.GetName() << " with mean " << hist.GetMean() << " and RMS " << hist.GetRMS() << endl;
210 
211  //Find peaks
212  int npeaks = spectrum.Search(&hist, (tpPeakWidthPerLayer / 2.), "", 0.3);
213 
214  double *peaks = spectrum.GetPositionX();
215  //Put in a std::vector<float>
216  vector<double> peakMeans(peaks, peaks + npeaks);
217  //Sort the peaks in ascending order
218  sort(peakMeans.begin(), peakMeans.end());
219 
220  if (peakMeans.empty())
221  {
222  theTPPeakMap[layerId] = hist.GetMaximumBin();
223  std::cout << "No peaks found by peakfinder in layer " << layerId << ". Taking maximum bin at " << theTPPeakMap[layerId] << ". Please check!" << std::endl;
224  layerIdWithWireHistos.push_back(layerId);
225  }
226  else if (fabs(hist.GetXaxis()->FindBin(peakMeans.front()) - hist.GetXaxis()->FindBin(peakMeans.back())) < rejectDigiFromPeak)
227  {
228  theTPPeakMap[layerId] = peakMeans[peakMeans.size() / 2];
229  }
230  else
231  {
232  bool peak_set = false;
233  for (const auto& peak : peakMeans)
234  {
235  // Skip if at low edge
236  if (peak - tpPeakWidthPerLayer <= 0)
237  continue;
238  // Get integral of peak
239  double sum = 0;
240  for (int ibin=peak - tpPeakWidthPerLayer; ibin<peak + tpPeakWidthPerLayer; ibin++)
241  {
242  sum += hist.GetBinContent(ibin);
243  }
244  // Skip if peak too small
245  if (sum < hist.GetMaximum() / 2)
246  continue;
247 
248  // Passed all cuts
249  theTPPeakMap[layerId] = peak;
250  peak_set = true;
251  break;
252  }
253  if (peak_set)
254  {
255  std::cout << "Peaks to far away from each other in layer " << layerId << ". Maybe cross talk? Taking first good peak at " << theTPPeakMap[layerId] << ". Please check!" << std::endl;
256  layerIdWithWireHistos.push_back(layerId);
257  }
258  else
259  {
260  theTPPeakMap[layerId] = hist.GetMaximumBin();
261  std::cout << "Peaks to far away from each other in layer " << layerId << " and no good peak found. Taking maximum bin at " << theTPPeakMap[layerId] << ". Please check!" << std::endl;
262  layerIdWithWireHistos.push_back(layerId);
263  }
264  }
265  if (peakMeans.size() > 5)
266  {
267  std::cout << "Found more than 5 peaks in layer " << layerId << ". Please check!" << std::endl;
268  if (std::find(layerIdWithWireHistos.begin(), layerIdWithWireHistos.end(), layerId) == layerIdWithWireHistos.end())
269  layerIdWithWireHistos.push_back(layerId);
270  }
271  // Check for noise
272  int nspikes = 0;
273  for (int ibin=0; ibin<hist.GetNbinsX(); ibin++)
274  {
275  if (hist.GetBinContent(ibin + 1) > hist.GetMaximum() * 0.001)
276  nspikes++;
277  }
278  if (nspikes > 50)
279  {
280  std::cout << "Found a lot of (>50) small spikes in layer " << layerId << ". Please check if all wires are functioning as expected!" << std::endl;
281  if (std::find(layerIdWithWireHistos.begin(), layerIdWithWireHistos.end(), layerId) == layerIdWithWireHistos.end())
282  layerIdWithWireHistos.push_back(layerId);
283  }
284  hLayerPeaks.Fill(theTPPeakMap[layerId]);
285  }
286  }
287 }
288 
290 
291  std::cout << "Analyzed " << nevents << " events" << std::endl;
292 
293  DTT0* t0sWRTChamber = new DTT0();
294 
295  if(debug)
296  cout << "[DTT0CalibrationPerLayer]Writing histos to file!" << endl;
297 
298  theFile.cd();
299  //hT0SectorHisto->Write();
300  hLayerPeaks.Write();
301  for(const auto& wHisto : theHistoWireMap){
302  wHisto.second.Write();
303  }
304  for(const auto& lHisto : theHistoLayerMap ) {
305  lHisto.second.Write();
306  }
307 
308  if(debug)
309  cout << "[DTT0Calibration] Compute and store t0 and sigma per wire" << endl;
310 
311  // Calculate uncertainties per wire (counting experiment)
312  for (auto& wiret0 : theAbsoluteT0PerWire)
313  {
314  auto& wireId = wiret0.first;
315  if (nDigiPerWire[wireId] > 1 )
316  theSigmaT0PerWire[wireId] = qK[wireId] / (nDigiPerWire[wireId] - 1);
317  else
318  theSigmaT0PerWire[wireId] = 999.; // Only one measurement: uncertainty -> infinity
319  // syst uncert
320  //theSigmaT0PerWire[wireId] += pow(0.5, 2));
321  // Every time the same measurement. Use Laplace estimator as estimation how propable it is to measure another value due to limited size of sample
322  if (theSigmaT0PerWire[wireId] == 0)
323  {
324  theSigmaT0PerWire[wireId] += pow(1. / (nDigiPerWire[wireId] + 1), 2);
325  }
326  }
327 
328  // function to calculate unweighted means
329  auto unweighted_mean_function = [] (const std::list<double>& values, const std::list<double>& sigmas)
330  {
331  double mean = 0;
332  for (auto& value : values)
333  {
334  mean += value;
335  }
336  mean /= values.size();
337 
338  double uncertainty = 0;
339  for (auto& value : values)
340  {
341  uncertainty += pow(value - mean, 2);
342  }
343  uncertainty /= values.size();
344  uncertainty = sqrt(uncertainty);
345  return std::make_pair(mean, uncertainty);
346  };
347 
348  // correct for odd-even effect in each super layer
349  std::map<DTSuperLayerId, std::pair<double, double> > mean_sigma_even;
350  std::map<DTSuperLayerId, std::pair<double, double> > mean_sigma_odd;
351  for (const auto& superlayer : dtGeom->superLayers())
352  {
353  const auto superlayer_id = superlayer->id();
354  std::list<double> values_even;
355  std::list<double> sigmas_even;
356  std::list<double> values_odd;
357  std::list<double> sigmas_odd;
358 
359  for (const auto& wiret0 : theAbsoluteT0PerWire)
360  {
361  const auto& wireId = wiret0.first;
362  if (wireId.layerId().superlayerId() == superlayer_id)
363  {
364  const auto& t0 = wiret0.second / nDigiPerWire[wireId];
365  if (wireId.layerId().layer() % 2)
366  {
367  values_odd.push_back(t0);
368  sigmas_odd.push_back(sqrt(theSigmaT0PerWire[wireId]));
369  }
370  else
371  {
372  values_even.push_back(t0);
373  sigmas_even.push_back(sqrt(theSigmaT0PerWire[wireId]));
374  }
375  }
376  }
377  // get mean and uncertainty
378  mean_sigma_even.emplace(superlayer_id, unweighted_mean_function(values_even, sigmas_even));
379  mean_sigma_odd.emplace(superlayer_id, unweighted_mean_function(values_odd, sigmas_odd));
380  }
381 
382  // filter outliers
383  for (const auto& superlayer : dtGeom->superLayers())
384  {
385  const auto superlayer_id = superlayer->id();
386  std::list<double> values_even;
387  std::list<double> sigmas_even;
388  std::list<double> values_odd;
389  std::list<double> sigmas_odd;
390 
391  for (const auto& wiret0 : theAbsoluteT0PerWire)
392  {
393  const auto& wireId = wiret0.first;
394  if (wireId.layerId().superlayerId() == superlayer_id)
395  {
396  const auto& t0 = wiret0.second / nDigiPerWire[wireId];
397  if (wireId.layerId().layer() % 2 and abs(t0 - mean_sigma_odd[superlayer_id].first) < 2 * mean_sigma_odd[superlayer_id].second)
398  {
399  values_odd.push_back(t0);
400  sigmas_odd.push_back(sqrt(theSigmaT0PerWire[wireId]));
401  }
402  else
403  {
404  if (abs(t0 - mean_sigma_even[superlayer_id].first) < 2 * mean_sigma_even[superlayer_id].second)
405  {
406  values_even.push_back(t0);
407  sigmas_even.push_back(sqrt(theSigmaT0PerWire[wireId]));
408  }
409  }
410  }
411  }
412  // get mean and uncertainty
413  mean_sigma_even[superlayer_id] = unweighted_mean_function(values_even, sigmas_even);
414  mean_sigma_odd[superlayer_id] = unweighted_mean_function(values_odd, sigmas_odd);
415  }
416 
417  // apply correction
418  for (auto& wiret0 : theAbsoluteT0PerWire)
419  {
420  const auto& wire_id = wiret0.first;
421  const auto& superlayer_id = wiret0.first.layerId().superlayerId();
422  const auto& layer = wiret0.first.layerId().layer();
423  auto& t0 = wiret0.second;
424  t0 /= nDigiPerWire[wire_id];
425  if (not layer % 2)
426  continue;
427  // t0 is reference. Changing it changes the map
428  t0 += mean_sigma_even[superlayer_id].first - mean_sigma_odd[superlayer_id].first;
429  theSigmaT0PerWire[wire_id] += pow(mean_sigma_odd[superlayer_id].second, 2) + pow(mean_sigma_even[superlayer_id].second, 2);
430  }
431 
432  // get chamber mean
433  std::map<DTChamberId, std::list<double> > values_per_chamber;
434  std::map<DTChamberId, std::list<double> > sigmas_per_chamber;
435  for (const auto& wire_t0 : theAbsoluteT0PerWire)
436  {
437  const auto& wire_id = wire_t0.first;
438  const auto& chamber_id = wire_id.chamberId();
439  const auto& t0 = wire_t0.second;
440  values_per_chamber[chamber_id].push_back(t0);
441  sigmas_per_chamber[chamber_id].push_back(sqrt(theSigmaT0PerWire[wire_id]));
442  }
443 
444  std::map<DTChamberId, std::pair<double, double> > mean_per_chamber;
445  for (const auto& chamber_mean : values_per_chamber)
446  {
447  const auto& chamber_id = chamber_mean.first;
448  const auto& means = chamber_mean.second;
449  const auto& sigmas = sigmas_per_chamber[chamber_id];
450  mean_per_chamber.emplace(chamber_id, unweighted_mean_function(means, sigmas));
451  }
452 
453  // calculate relative values
454  for (const auto& wire_t0 : theAbsoluteT0PerWire)
455  {
456  const auto& wire_id = wire_t0.first;
457  const auto& chamber_id = wire_id.chamberId();
458  const auto& t0 = wire_t0.second;
459  theRelativeT0PerWire.emplace(wire_id, t0 - mean_per_chamber[chamber_id].first);
460  cout << "[DTT0Calibration] Wire " << wire_id << " has t0 "<< theRelativeT0PerWire[wire_id] << " (relative, after even-odd layer corrections) "
461  << " sigma "<< sqrt(theSigmaT0PerWire[wire_id]) <<endl;
462  }
463 
464  for(const auto& wire_t0 : theRelativeT0PerWire)
465  {
466  const auto& wire_id = wire_t0.first;
467  const auto& t0 = wire_t0.second;
468  t0sWRTChamber->set(wire_id,
469  t0,
470  sqrt(theSigmaT0PerWire[wire_id]),
472  );
473  }
474 
476  if(debug)
477  cout << "[DTT0Calibration]Writing values in DB!" << endl;
478  // FIXME: to be read from cfg?
479  string t0Record = "DTT0Rcd";
480  // Write the t0 map to DB
481  DTCalibDBUtils::writeToDB(t0Record, t0sWRTChamber);
482  delete t0sWRTChamber;
483 }
484 
485 string DTT0Calibration::getHistoName(const DTWireId& wId) const {
486  string histoName;
487  stringstream theStream;
488  theStream << "Ch_" << wId.wheel() << "_" << wId.station() << "_" << wId.sector()
489  << "_SL" << wId.superlayer() << "_L" << wId.layer() << "_W"<< wId.wire() <<"_hT0Histo";
490  theStream >> histoName;
491  return histoName;
492 }
493 
494 string DTT0Calibration::getHistoName(const DTLayerId& lId) const {
495  string histoName;
496  stringstream theStream;
497  theStream << "Ch_" << lId.wheel() << "_" << lId.station() << "_" << lId.sector()
498  << "_SL" << lId.superlayer() << "_L" << lId.layer() <<"_hT0Histo";
499  theStream >> histoName;
500  return histoName;
501 }
std::vector< DTLayerId > layerIdWithWireHistos
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
double tpPeakWidthPerLayer
T getUntrackedParameter(std::string const &, T const &) const
std::map< DTLayerId, TH1I > theHistoLayerMap
void endJob() override
Compute the mean and the RMS of the t0 from the maps and write them to the DB with channel granularit...
unsigned int nevents
std::map< DTWireId, double > theAbsoluteT0PerWire
DTChamberId chamberId() const
Return the corresponding ChamberId.
std::map< DTLayerId, double > theTPPeakMap
std::map< DTWireId, int > nDigiPerWire
std::vector< DTWireId > wireIdWithHistos
std::string theCalibWheel
std::string getHistoName(const DTWireId &wId) const
std::map< DTWireId, double > theSigmaT0PerWire
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
edm::ESHandle< DTGeometry > dtGeom
std::map< DTWireId, double > mK
unsigned int eventsForLayerT0
U second(std::pair< T, U > const &p)
Definition: DTT0.h:53
unsigned int rejectDigiFromPeak
std::map< DTWireId, double > theRelativeT0PerWire
T sqrt(T t)
Definition: SSEVec.h:18
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
~DTT0Calibration() override
Destructor.
std::string theCalibSector
std::map< DTWireId, double > qK
Definition: value.py:1
edm::EDGetTokenT< DTDigiCollection > digiToken
void analyze(const edm::Event &event, const edm::EventSetup &eventSetup) override
Fill the maps with t0 (by channel)
std::map< DTWireId, double > mK_ref
int wire() const
Return the wire number.
Definition: DTWireId.h:56
int superlayer() const
Return the superlayer number (deprecated method name)
#define debug
Definition: HDRShower.cc:19
std::map< DTWireId, TH1I > theHistoWireMap
std::vector< DigiType >::const_iterator const_iterator
HLT enums.
int sector() const
Definition: DTChamberId.h:61
T get() const
Definition: EventSetup.h:71
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
TSpectrum spectrum
static void writeToDB(std::string record, T *payload)
unsigned int eventsForWireT0
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
Definition: event.py:1
const std::vector< const DTSuperLayer * > & superLayers() const
Return a vector of all SuperLayer.
Definition: DTGeometry.cc:107
std::map< DTWireId, int > nDigiPerWire_ref