29 debug =
pset.getUntrackedParameter<
bool>(
"debug");
31 cout <<
"[DTT0CalibrationRMS]Constructor called!" << endl;
37 string rootFileName =
pset.getUntrackedParameter<
string>(
"rootFileName",
"DTT0PerLayer.root");
41 pset.getUntrackedParameter<
string>(
"calibWheel",
"All");
42 if (theCalibWheel !=
"All") {
45 linestr << theCalibWheel;
47 cout <<
"[DTT0CalibrationRMSPerLayer] chosen wheel " << selWheel << endl;
52 pset.getUntrackedParameter<
string>(
"calibSector",
"All");
53 if (theCalibSector !=
"All") {
56 linestr << theCalibSector;
58 cout <<
"[DTT0CalibrationRMSPerLayer] chosen sector " << selSector << endl;
61 vector<string> defaultCell;
62 defaultCell.push_back(
"None");
63 cellsWithHistos =
pset.getUntrackedParameter<vector<string> >(
"cellsWithHisto", defaultCell);
64 for (vector<string>::const_iterator cell = cellsWithHistos.begin(); cell != cellsWithHistos.end(); ++cell) {
65 if ((*cell) !=
"None") {
69 linestr >>
wheel >> sector >>
station >> sl >> layer >> wire;
74 hT0SectorHisto =
nullptr;
77 eventsForLayerT0 =
pset.getParameter<
unsigned int>(
"eventsForLayerT0");
78 eventsForWireT0 =
pset.getParameter<
unsigned int>(
"eventsForWireT0");
79 rejectDigiFromPeak =
pset.getParameter<
unsigned int>(
"rejectDigiFromPeak");
80 tpPeakWidth =
pset.getParameter<
double>(
"tpPeakWidth");
82 correctByChamberMean_ =
pset.getParameter<
bool>(
"correctByChamberMean");
88 cout <<
"[DTT0CalibrationRMS]Destructor called!" << endl;
96 cout <<
"--- [DTT0CalibrationRMS] Analysing Event: #Run: " <<
event.id().run() <<
" #Event: " <<
event.id().event()
109 for (dtLayerIt = digis->begin(); dtLayerIt != digis->end(); ++dtLayerIt) {
114 const DTLayerId layerId = (*dtLayerIt).first;
127 double t0 = (*digi).countsTDC();
130 if (
nevents < eventsForLayerT0) {
132 TH1I* hT0LayerHisto = theHistoLayerMap[layerId];
134 if (hT0LayerHisto ==
nullptr) {
137 "T0 from pulses by layer (TDC counts, 1 TDC count = 0.781 ns)",
142 cout <<
" New T0 per Layer Histo: " << hT0LayerHisto->GetName() << endl;
143 theHistoLayerMap[layerId] = hT0LayerHisto;
148 if (hT0LayerHisto !=
nullptr) {
151 hT0LayerHisto->Fill(
t0);
156 if (
nevents > eventsForLayerT0) {
158 const DTWireId wireId(layerId, (*digi).wire());
160 cout <<
" Wire: " << wireId << endl <<
" time (TDC counts): " << (*digi).countsTDC() << endl;
164 vector<DTWireId>::iterator it_wire =
find(wireIdWithHistos.begin(), wireIdWithHistos.end(), wireId);
165 if (it_wire != wireIdWithHistos.end()) {
166 if (theHistoWireMap.find(wireId) == theHistoWireMap.end()) {
167 theHistoWireMap[wireId] =
new TH1I(
getHistoName(wireId).c_str(),
168 "T0 from pulses by wire (TDC counts, 1 TDC count = 0.781 ns)",
173 cout <<
" New T0 per wire Histo: " << (theHistoWireMap[wireId])->GetName() << endl;
175 if (theHistoWireMap_ref.find(wireId) == theHistoWireMap_ref.end()) {
176 theHistoWireMap_ref[wireId] =
new TH1I((
getHistoName(wireId) +
"_ref").c_str(),
177 "T0 from pulses by wire (TDC counts, 1 TDC count = 0.781 ns)",
182 cout <<
" New T0 per wire Histo: " << (theHistoWireMap_ref[wireId])->GetName() << endl;
185 TH1I* hT0WireHisto = theHistoWireMap[wireId];
189 hT0WireHisto->Fill(
t0);
193 if (
abs(hT0SectorHisto->GetBinCenter(hT0SectorHisto->GetMaximumBin()) -
t0) > rejectDigiFromPeak) {
195 cout <<
"digi skipped because t0 per sector "
196 << hT0SectorHisto->GetBinCenter(hT0SectorHisto->GetMaximumBin()) << endl;
201 if (
nevents < (eventsForLayerT0 + eventsForWireT0)) {
203 if (it_wire != wireIdWithHistos.end()) {
204 TH1I* hT0WireHisto_ref = theHistoWireMap_ref[wireId];
206 if (hT0WireHisto_ref)
207 hT0WireHisto_ref->Fill(
t0);
209 if (!nDigiPerWire_ref[wireId]) {
212 nDigiPerWire_ref[wireId] = nDigiPerWire_ref[wireId] + 1;
213 mK_ref[wireId] = mK_ref[wireId] + (
t0 - mK_ref[wireId]) / nDigiPerWire_ref[wireId];
216 else if (
nevents > (eventsForLayerT0 + eventsForWireT0)) {
217 if (
abs(
t0 - mK_ref[wireId]) > tpPeakWidth)
219 if (!nDigiPerWire[wireId]) {
220 theAbsoluteT0PerWire[wireId] = 0;
224 nDigiPerWire[wireId] = nDigiPerWire[wireId] + 1;
225 theAbsoluteT0PerWire[wireId] = theAbsoluteT0PerWire[wireId] +
t0;
228 qK[wireId] + ((nDigiPerWire[wireId] - 1) * (
t0 - mK[wireId]) * (
t0 - mK[wireId]) / nDigiPerWire[wireId]);
229 mK[wireId] = mK[wireId] + (
t0 - mK[wireId]) / nDigiPerWire[wireId];
236 if (
nevents == eventsForLayerT0) {
237 for (map<DTLayerId, TH1I*>::const_iterator lHisto = theHistoLayerMap.begin(); lHisto != theHistoLayerMap.end();
240 cout <<
"Reading histogram " << (*lHisto).second->GetName() <<
" with mean " << (*lHisto).second->GetMean()
241 <<
" and RMS " << (*lHisto).second->GetRMS();
244 if ((*lHisto).second->GetRMS() < 5.0) {
245 if (hT0SectorHisto ==
nullptr) {
246 hT0SectorHisto =
new TH1D(
"hT0AllLayerOfSector",
247 "T0 from pulses per layer in sector",
254 cout <<
" accepted" << endl;
255 hT0SectorHisto->Fill((*lHisto).second->GetMean());
272 theT0LayerMap[(*lHisto).second->GetName()] = (*lHisto).second->GetMean();
273 theSigmaT0LayerMap[(*lHisto).second->GetName()] = (*lHisto).second->GetRMS();
275 if (!hT0SectorHisto) {
276 cout <<
"[DTT0CalibrationRMS]: All the t0 per layer are still uncorrect: trying with greater number of events"
278 eventsForLayerT0 = eventsForLayerT0 * 2;
282 cout <<
"[DTT0CalibrationRMS] t0 reference for this sector "
283 << hT0SectorHisto->GetBinCenter(hT0SectorHisto->GetMaximumBin()) << endl;
293 cout <<
"[DTT0CalibrationRMSPerLayer]Writing histos to file!" << endl;
296 theFile->WriteTObject(hT0SectorHisto);
298 for (map<DTWireId, TH1I*>::const_iterator wHisto = theHistoWireMap.begin(); wHisto != theHistoWireMap.end();
300 (*wHisto).second->Write();
302 for (map<DTWireId, TH1I*>::const_iterator wHisto = theHistoWireMap_ref.begin(); wHisto != theHistoWireMap_ref.end();
304 (*wHisto).second->Write();
306 for (map<DTLayerId, TH1I*>::const_iterator lHisto = theHistoLayerMap.begin(); lHisto != theHistoLayerMap.end();
308 (*lHisto).second->Write();
312 cout <<
"[DTT0CalibrationRMS] Compute and store t0 and sigma per wire" << endl;
314 for (map<DTWireId, double>::const_iterator wiret0 = theAbsoluteT0PerWire.begin();
315 wiret0 != theAbsoluteT0PerWire.end();
317 if (nDigiPerWire[(*wiret0).first]) {
318 double t0 = (*wiret0).second / nDigiPerWire[(*wiret0).first];
320 theRelativeT0PerWire[(*wiret0).first] =
t0 - hT0SectorHisto->GetBinCenter(hT0SectorHisto->GetMaximumBin());
323 theSigmaT0PerWire[(*wiret0).first] =
sqrt(qK[(*wiret0).first] / nDigiPerWire[(*wiret0).first]);
325 cout <<
"Wire " << (*wiret0).first <<
" has t0 " <<
t0 <<
"(absolute) " << theRelativeT0PerWire[(*wiret0).first]
327 <<
" sigma " << theSigmaT0PerWire[(*wiret0).first] << endl;
331 cout <<
"[DTT0CalibrationRMS] ERROR: no digis in wire " << (*wiret0).first << endl;
336 if (correctByChamberMean_) {
339 const vector<const DTSuperLayer*> superLayers = dtGeom->superLayers();
341 for (vector<const DTSuperLayer*>::const_iterator sl = superLayers.begin(); sl != superLayers.end(); sl++) {
343 double oddLayersMean = 0;
344 double evenLayersMean = 0;
345 double oddLayersDen = 0;
346 double evenLayersDen = 0;
347 for (map<DTWireId, double>::const_iterator wiret0 = theRelativeT0PerWire.begin();
348 wiret0 != theRelativeT0PerWire.end();
350 if ((*wiret0).first.layerId().superlayerId() == (*sl)->id()) {
352 cout <<
"[DTT0CalibrationRMS] Superlayer " << (*sl)->id() <<
"layer " << (*wiret0).first.layerId().layer()
353 <<
" with " << (*wiret0).second << endl;
354 if (((*wiret0).first.layerId().layer()) % 2) {
355 oddLayersMean = oddLayersMean + (*wiret0).second;
358 evenLayersMean = evenLayersMean + (*wiret0).second;
363 oddLayersMean = oddLayersMean / oddLayersDen;
364 evenLayersMean = evenLayersMean / evenLayersDen;
366 cout <<
"[DTT0CalibrationRMS] Relative T0 mean for odd layers " << oddLayersMean <<
" even layers"
367 << evenLayersMean << endl;
370 double oddLayersSigma = 0;
371 double evenLayersSigma = 0;
372 for (map<DTWireId, double>::const_iterator wiret0 = theRelativeT0PerWire.begin();
373 wiret0 != theRelativeT0PerWire.end();
375 if ((*wiret0).first.layerId().superlayerId() == (*sl)->id()) {
376 if (((*wiret0).first.layerId().layer()) % 2) {
377 oddLayersSigma = oddLayersSigma + ((*wiret0).second - oddLayersMean) * ((*wiret0).second - oddLayersMean);
380 evenLayersSigma + ((*wiret0).second - evenLayersMean) * ((*wiret0).second - evenLayersMean);
384 oddLayersSigma = oddLayersSigma / oddLayersDen;
385 evenLayersSigma = evenLayersSigma / evenLayersDen;
386 oddLayersSigma =
sqrt(oddLayersSigma);
387 evenLayersSigma =
sqrt(evenLayersSigma);
390 cout <<
"[DTT0CalibrationRMS] Relative T0 sigma for odd layers " << oddLayersSigma <<
" even layers"
391 << evenLayersSigma << endl;
394 double oddLayersFinalMean = 0;
395 double evenLayersFinalMean = 0;
396 for (map<DTWireId, double>::const_iterator wiret0 = theRelativeT0PerWire.begin();
397 wiret0 != theRelativeT0PerWire.end();
399 if ((*wiret0).first.layerId().superlayerId() == (*sl)->id()) {
400 if (((*wiret0).first.layerId().layer()) % 2) {
401 if (
abs((*wiret0).second - oddLayersMean) < (2 * oddLayersSigma))
402 oddLayersFinalMean = oddLayersFinalMean + (*wiret0).second;
404 if (
abs((*wiret0).second - evenLayersMean) < (2 * evenLayersSigma))
405 evenLayersFinalMean = evenLayersFinalMean + (*wiret0).second;
409 oddLayersFinalMean = oddLayersFinalMean / oddLayersDen;
410 evenLayersFinalMean = evenLayersFinalMean / evenLayersDen;
412 cout <<
"[DTT0CalibrationRMS] Final relative T0 mean for odd layers " << oddLayersFinalMean <<
" even layers"
413 << evenLayersFinalMean << endl;
415 for (map<DTWireId, double>::const_iterator wiret0 = theRelativeT0PerWire.begin();
416 wiret0 != theRelativeT0PerWire.end();
418 if ((*wiret0).first.layerId().superlayerId() == (*sl)->id()) {
420 if (((*wiret0).first.layerId().layer()) % 2)
421 t0 = (*wiret0).second + (evenLayersFinalMean - oddLayersFinalMean);
423 t0 = (*wiret0).second;
425 cout <<
"[DTT0CalibrationRMS] Wire " << (*wiret0).first <<
" has t0 " << (*wiret0).second
426 <<
" (relative, after even-odd layer corrections) "
427 <<
" sigma " << theSigmaT0PerWire[(*wiret0).first] << endl;
437 cout <<
"[DTT0CalibrationRMS]Computing relative t0 wrt to chamber average" << endl;
439 map<DTChamberId, double> sumT0ByChamber;
440 map<DTChamberId, int> countT0ByChamber;
442 int channelId =
tzero->channelId;
452 sumT0ByChamber[chamberId] = sumT0ByChamber[chamberId] + t0mean_f;
454 countT0ByChamber[chamberId]++;
459 int channelId =
tzero->channelId;
470 double t0mean = t0mean_f - (sumT0ByChamber[chamberId] / countT0ByChamber[chamberId]);
471 double t0rms = t0rms_f;
476 cout <<
"Changing t0 of wire " << wireId <<
" from " << t0mean_f <<
" to " << t0mean << endl;
482 cout <<
"[DTT0CalibrationRMS]Writing values in DB!" << endl;
484 string t0Record =
"DTT0Rcd";
486 if (correctByChamberMean_)
494 std::to_string(wId.
sector()) +
"_SL" + std::to_string(wId.
superlayer()) +
"_L" +
495 std::to_string(wId.
layer()) +
"_W" + std::to_string(wId.
wire()) +
"_hT0Histo";
501 std::to_string(lId.
sector()) +
"_SL" + std::to_string(lId.
superlayer()) +
"_L" +
502 std::to_string(lId.
layer()) +
"_hT0Histo";