37 theFile(
pset.getUntrackedParameter<
string>(
"rootFileName",
"DTT0PerLayer.root").c_str(),
"RECREATE"),
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),
50 cout <<
"[DTT0Calibration]Constructor called!" << endl;
53 pset.getUntrackedParameter<
string>(
"calibWheel",
"All");
59 cout <<
"[DTT0CalibrationPerLayer] chosen wheel " <<
selWheel << endl;
64 pset.getUntrackedParameter<
string>(
"calibSector",
"All");
70 cout <<
"[DTT0CalibrationPerLayer] chosen sector " <<
selSector << endl;
73 vector<string> defaultCell;
74 const auto& cellsWithHistos =
pset.getUntrackedParameter<vector<string> >(
"cellsWithHisto", defaultCell);
75 for (
const auto& cell : cellsWithHistos) {
79 linestr >>
wheel >> sector >>
station >> sl >> layer >> wire;
87 cout <<
"[DTT0Calibration]Destructor called!" << endl;
105 for (
const auto& digis_per_layer : *digis) {
111 const DTLayerId layerId = digis_per_layer.first;
121 const double t0 = digi->countsTDC();
122 const DTWireId wireIdtmp(layerId, (*digi).wire());
129 "T0 from pulses by layer (TDC counts, 1 TDC count = 0.781 ns)",
142 const DTWireId wireId(layerId, (*digi).wire());
144 cout <<
" Wire: " << wireId << endl <<
" time (TDC counts): " << (*digi).countsTDC() << endl;
154 "T0 from pulses by wire (TDC counts, 1 TDC count = 0.781 ns)",
167 cout <<
"digi skipped because t0 too far from peak " <<
theTPPeakMap[layerId] << endl;
201 const auto& layerId = lHisto.first;
202 const auto&
hist = lHisto.second;
204 cout <<
"Reading histogram " <<
hist.GetName() <<
" with mean " <<
hist.GetMean() <<
" and RMS "
205 <<
hist.GetRMS() << endl;
210 double* peaks =
spectrum.GetPositionX();
212 vector<double> peakMeans(peaks, peaks + npeaks);
214 sort(peakMeans.begin(), peakMeans.end());
216 if (peakMeans.empty()) {
218 std::cout <<
"No peaks found by peakfinder in layer " << layerId <<
". Taking maximum bin at "
219 <<
theTPPeakMap[layerId] <<
". Please check!" << std::endl;
221 }
else if (fabs(
hist.GetXaxis()->FindBin(peakMeans.front()) -
hist.GetXaxis()->FindBin(peakMeans.back())) <
223 theTPPeakMap[layerId] = peakMeans[peakMeans.size() / 2];
225 bool peak_set =
false;
226 for (
const auto& peak : peakMeans) {
233 sum +=
hist.GetBinContent(ibin);
236 if (sum <
hist.GetMaximum() / 2)
245 std::cout <<
"Peaks to far away from each other in layer " << layerId
246 <<
". Maybe cross talk? Taking first good peak at " <<
theTPPeakMap[layerId] <<
". Please check!"
251 std::cout <<
"Peaks to far away from each other in layer " << layerId
252 <<
" and no good peak found. Taking maximum bin at " <<
theTPPeakMap[layerId] <<
". Please check!"
257 if (peakMeans.size() > 5) {
258 std::cout <<
"Found more than 5 peaks in layer " << layerId <<
". Please check!" << std::endl;
265 for (
int ibin = 0; ibin <
hist.GetNbinsX(); ibin++) {
266 if (
hist.GetBinContent(ibin + 1) >
hist.GetMaximum() * 0.001)
270 std::cout <<
"Found a lot of (>50) small spikes in layer " << layerId
271 <<
". Please check if all wires are functioning as expected!" << std::endl;
287 cout <<
"[DTT0CalibrationPerLayer]Writing histos to file!" << endl;
293 wHisto.second.Write();
296 lHisto.second.Write();
300 cout <<
"[DTT0Calibration] Compute and store t0 and sigma per wire" << endl;
304 auto& wireId = wiret0.first;
318 auto unweighted_mean_function = [](
const std::list<double>&
values,
const std::list<double>& sigmas) {
325 double uncertainty = 0;
329 uncertainty /=
values.size();
330 uncertainty =
sqrt(uncertainty);
331 return std::make_pair(
mean, uncertainty);
335 std::map<DTSuperLayerId, std::pair<double, double> > mean_sigma_even;
336 std::map<DTSuperLayerId, std::pair<double, double> > mean_sigma_odd;
338 const auto superlayer_id = superlayer->id();
339 std::list<double> values_even;
340 std::list<double> sigmas_even;
341 std::list<double> values_odd;
342 std::list<double> sigmas_odd;
345 const auto& wireId = wiret0.first;
346 if (wireId.layerId().superlayerId() == superlayer_id) {
348 if (wireId.layerId().layer() % 2) {
349 values_odd.push_back(
t0);
352 values_even.push_back(
t0);
358 mean_sigma_even.emplace(superlayer_id, unweighted_mean_function(values_even, sigmas_even));
359 mean_sigma_odd.emplace(superlayer_id, unweighted_mean_function(values_odd, sigmas_odd));
364 const auto superlayer_id = superlayer->id();
365 std::list<double> values_even;
366 std::list<double> sigmas_even;
367 std::list<double> values_odd;
368 std::list<double> sigmas_odd;
371 const auto& wireId = wiret0.first;
372 if (wireId.layerId().superlayerId() == superlayer_id) {
374 if (wireId.layerId().layer() % 2 and
375 abs(
t0 - mean_sigma_odd[superlayer_id].
first) < 2 * mean_sigma_odd[superlayer_id].second) {
376 values_odd.push_back(
t0);
379 if (
abs(
t0 - mean_sigma_even[superlayer_id].
first) < 2 * mean_sigma_even[superlayer_id].
second) {
380 values_even.push_back(
t0);
387 mean_sigma_even[superlayer_id] = unweighted_mean_function(values_even, sigmas_even);
388 mean_sigma_odd[superlayer_id] = unweighted_mean_function(values_odd, sigmas_odd);
393 const auto& wire_id = wiret0.first;
394 const auto& superlayer_id = wiret0.first.layerId().superlayerId();
395 const auto& layer = wiret0.first.layerId().layer();
396 auto&
t0 = wiret0.second;
401 t0 += mean_sigma_even[superlayer_id].first - mean_sigma_odd[superlayer_id].first;
403 pow(mean_sigma_odd[superlayer_id].
second, 2) +
pow(mean_sigma_even[superlayer_id].
second, 2);
407 std::map<DTChamberId, std::list<double> > values_per_chamber;
408 std::map<DTChamberId, std::list<double> > sigmas_per_chamber;
410 const auto& wire_id = wire_t0.first;
411 const auto& chamber_id = wire_id.chamberId();
412 const auto&
t0 = wire_t0.second;
413 values_per_chamber[chamber_id].push_back(
t0);
417 std::map<DTChamberId, std::pair<double, double> > mean_per_chamber;
418 for (
const auto& chamber_mean : values_per_chamber) {
419 const auto& chamber_id = chamber_mean.first;
420 const auto& means = chamber_mean.second;
421 const auto& sigmas = sigmas_per_chamber[chamber_id];
422 mean_per_chamber.emplace(chamber_id, unweighted_mean_function(means, sigmas));
427 const auto& wire_id = wire_t0.first;
428 const auto& chamber_id = wire_id.chamberId();
429 const auto&
t0 = wire_t0.second;
432 <<
" (relative, after even-odd layer corrections) "
437 const auto& wire_id = wire_t0.first;
438 const auto&
t0 = wire_t0.second;
444 cout <<
"[DTT0Calibration]Writing values in DB!" << endl;
446 string t0Record =
"DTT0Rcd";
449 delete t0sWRTChamber;
454 stringstream theStream;
456 << wId.
layer() <<
"_W" << wId.
wire() <<
"_hT0Histo";
463 stringstream theStream;
465 << lId.
layer() <<
"_hT0Histo";