23 #include "TSpectrum.h" 35 cout <<
"[DTT0Calibration]Constructor called!" << endl;
44 theFile =
new TFile(rootFileName.c_str(),
"RECREATE");
47 if(theCalibWheel !=
"All") {
50 linestr << theCalibWheel;
52 cout <<
"[DTT0CalibrationPerLayer] chosen wheel " << selWheel << endl;
57 if(theCalibSector !=
"All") {
60 linestr << theCalibSector;
62 cout <<
"[DTT0CalibrationPerLayer] chosen sector " << selSector << endl;
65 vector<string> defaultCell;
66 defaultCell.push_back(
"None");
68 for(vector<string>::const_iterator cell = cellsWithHistos.begin(); cell != cellsWithHistos.end(); ++cell){
69 if((*cell) !=
"None"){
73 linestr >> wheel >> sector >> station >> sl >> layer >>
wire;
74 wireIdWithHistos.push_back(
DTWireId(wheel,station,sector,sl,layer,wire));
78 hT0SectorHisto=
nullptr;
81 eventsForLayerT0 = pset.
getParameter<
unsigned int>(
"eventsForLayerT0");
82 eventsForWireT0 = pset.
getParameter<
unsigned int>(
"eventsForWireT0");
84 tpPeakWidthPerLayer = pset.
getParameter<
double>(
"tpPeakWidthPerLayer");
85 timeBoxWidth = pset.
getParameter<
unsigned int>(
"timeBoxWidth");
86 rejectDigiFromPeak = pset.
getParameter<
unsigned int>(
"rejectDigiFromPeak");
88 spectrum =
new TSpectrum(5);
95 cout <<
"[DTT0Calibration]Destructor called!" << endl;
104 cout <<
"--- [DTT0Calibration] Analysing Event: #Run: " << event.
id().
run()
105 <<
" #Event: " <<
event.id().event() << endl;
121 for (dtLayerIt = digis->begin();
122 dtLayerIt != digis->end();
128 const DTLayerId layerId = (*dtLayerIt).first;
142 if(
debug&&(nevents <= 1)){
144 <<
" tTrig,tTrigRMS= " << tTrig <<
", " << tTrigRMS << endl;
149 digi != digiRange.second;
151 double t0 = (*digi).countsTDC();
154 if(nevents < eventsForLayerT0){
156 TH1I *hT0LayerHisto = theHistoLayerMap[layerId];
158 if(hT0LayerHisto ==
nullptr){
160 float hT0Min = tTrig - 2*tTrigRMS;
161 float hT0Max = hT0Min + timeBoxWidth;
163 "T0 from pulses by layer (TDC counts, 1 TDC count = 0.781 ns)",
164 timeBoxWidth,hT0Min,hT0Max);
166 cout <<
" New T0 per Layer Histo: " << hT0LayerHisto->GetName() << endl;
167 theHistoLayerMap[layerId] = hT0LayerHisto;
172 if(hT0LayerHisto !=
nullptr) {
175 hT0LayerHisto->Fill(t0);
180 if(nevents>eventsForLayerT0){
182 const DTWireId wireId(layerId, (*digi).wire());
184 cout <<
" Wire: " << wireId << endl
185 <<
" time (TDC counts): " << (*digi).countsTDC()<< endl;
189 vector<DTWireId>::iterator it =
find(wireIdWithHistos.begin(),wireIdWithHistos.end(),wireId);
190 if (it!=wireIdWithHistos.end()){
192 TH1I *hT0WireHisto = theHistoWireMap[wireId];
194 if(hT0WireHisto ==
nullptr){
196 hT0WireHisto =
new TH1I(
getHistoName(wireId).c_str(),
"T0 from pulses by wire (TDC counts, 1 TDC count = 0.781 ns)",7000,0,7000);
200 cout <<
" New T0 per wire Histo: " << hT0WireHisto->GetName() << endl;
201 theHistoWireMap[wireId] = hT0WireHisto;
205 if(hT0WireHisto !=
nullptr) {
208 hT0WireHisto->Fill(t0);
226 if(fabs(theTPPeakMap[layerId] - t0) > rejectDigiFromPeak){
228 cout<<
"digi skipped because t0 too far from peak " << theTPPeakMap[layerId] << endl;
233 theSumT0ByChamber[chamberId] = theSumT0ByChamber[chamberId] +
t0;
234 theCountT0ByChamber[chamberId]++;
237 if(nevents< (eventsForLayerT0 + eventsForWireT0)){
238 if(!nDigiPerWire_ref[wireId]){
241 nDigiPerWire_ref[wireId] = nDigiPerWire_ref[wireId] + 1;
242 mK_ref[wireId] = mK_ref[wireId] + (t0-mK_ref[wireId])/nDigiPerWire_ref[wireId];
245 else if(nevents>(eventsForLayerT0 + eventsForWireT0)){
246 if(
abs(t0-mK_ref[wireId]) > tpPeakWidth)
248 if(!nDigiPerWire[wireId]){
249 theAbsoluteT0PerWire[wireId] = 0;
253 nDigiPerWire[wireId] = nDigiPerWire[wireId] + 1;
254 theAbsoluteT0PerWire[wireId] = theAbsoluteT0PerWire[wireId] +
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];
264 if(nevents == eventsForLayerT0){
265 bool increaseEvtsForLayerT0 =
false;
266 for(map<DTLayerId, TH1I*>::const_iterator lHisto = theHistoLayerMap.begin();
267 lHisto != theHistoLayerMap.end();
270 cout<<
"Reading histogram "<<(*lHisto).second->GetName()<<
" with mean "<<(*lHisto).second->GetMean()<<
" and RMS "<<(*lHisto).second->GetRMS() << endl;
275 int npeaks = spectrum->Search((*lHisto).second,(tpPeakWidthPerLayer/2.),
"",0.3);
277 double *peaks = spectrum->GetPositionX();
279 vector<double> peakMeans(peaks,peaks + npeaks);
281 sort(peakMeans.begin(),peakMeans.end());
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){
294 int bin = (*lHisto).second->GetXaxis()->FindBin(mean);
295 double yp = (*lHisto).second->GetBinContent(bin);
296 if(
debug)
cout <<
"Peak : (" << mean <<
"," << yp <<
")" << endl;
299 double previous_peak = (it == peakMeans.begin())?hMin:*(it - 1);
300 double next_peak = (it == (peakMeans.end()-1))?hMax:*(it + 1);
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);
308 double rms_seed = (*lHisto).second->GetRMS();
328 if(rms_seed > tpPeakWidthPerLayer)
continue;
330 if(fabs(mean - timeBoxCenter) < fabs(*tpPeak - timeBoxCenter)) tpPeak = it;
341 double selPeak = (tpPeak != peakMeans.end())?*tpPeak:(*lHisto).second->GetBinCenter((*lHisto).second->GetMaximumBin());
342 if(
debug)
cout <<
"Peak selected at " << selPeak << endl;
344 theTPPeakMap[(*lHisto).first] = selPeak;
386 if(increaseEvtsForLayerT0){
387 cout<<
"[DTT0Calibration]: t0 per layer are still uncorrect: trying with greater number of events"<<endl;
388 eventsForLayerT0 = eventsForLayerT0*2;
400 cout <<
"[DTT0CalibrationPerLayer]Writing histos to file!" << endl;
404 for(map<DTWireId, TH1I*>::const_iterator wHisto = theHistoWireMap.begin();
405 wHisto != theHistoWireMap.end();
407 (*wHisto).second->Write();
409 for(map<DTLayerId, TH1I*>::const_iterator lHisto = theHistoLayerMap.begin();
410 lHisto != theHistoLayerMap.end();
412 (*lHisto).second->Write();
416 cout <<
"[DTT0Calibration] Compute and store t0 and sigma per wire" << endl;
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]);
422 for(map<DTWireId, double>::const_iterator wiret0 = theAbsoluteT0PerWire.begin();
423 wiret0 != theAbsoluteT0PerWire.end();
425 if(nDigiPerWire[(*wiret0).first]){
426 double t0 = (*wiret0).second/nDigiPerWire[(*wiret0).first];
427 DTChamberId chamberId = ((*wiret0).first).chamberId();
429 theRelativeT0PerWire[(*wiret0).first] = t0 - theRefT0ByChamber[chamberId];
430 cout<<
"Wire "<<(*wiret0).first<<
" has t0 "<<t0<<
"(absolute) "<<theRelativeT0PerWire[(*wiret0).first]<<
"(relative)";
433 theSigmaT0PerWire[(*wiret0).first] =
sqrt(qK[(*wiret0).first]/nDigiPerWire[(*wiret0).first]);
434 cout<<
" sigma "<<theSigmaT0PerWire[(*wiret0).first]<<endl;
437 cout<<
"[DTT0CalibrationNew] ERROR: no digis in wire "<<(*wiret0).first<<endl;
444 const vector<const DTSuperLayer*> superLayers = dtGeom->superLayers();
446 for(vector<const DTSuperLayer*>::const_iterator sl = superLayers.begin();
447 sl != superLayers.end(); sl++) {
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();
458 if((*wiret0).first.layerId().superlayerId() == (*sl)->id()){
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;
467 evenLayersMean = evenLayersMean + (*wiret0).second;
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;
478 double oddLayersSigma=0;
479 double evenLayersSigma=0;
480 for(map<DTWireId, double>::const_iterator wiret0 = theRelativeT0PerWire.begin();
481 wiret0 != theRelativeT0PerWire.end();
483 if((*wiret0).first.layerId().superlayerId() == (*sl)->id()){
484 if(((*wiret0).first.layerId().layer()) % 2){
485 oddLayersSigma = oddLayersSigma + ((*wiret0).second - oddLayersMean) * ((*wiret0).second - oddLayersMean);
488 evenLayersSigma = evenLayersSigma + ((*wiret0).second - evenLayersMean) * ((*wiret0).second - evenLayersMean);
492 oddLayersSigma = oddLayersSigma/oddLayersDen;
493 evenLayersSigma = evenLayersSigma/evenLayersDen;
494 oddLayersSigma =
sqrt(oddLayersSigma);
495 evenLayersSigma =
sqrt(evenLayersSigma);
497 if(
debug && oddLayersMean)
498 cout<<
"[DTT0Calibration] Relative T0 sigma for odd layers "<<oddLayersSigma<<
" even layers"<<evenLayersSigma<<endl;
501 double oddLayersFinalMean=0;
502 double evenLayersFinalMean=0;
503 for(map<DTWireId, double>::const_iterator wiret0 = theRelativeT0PerWire.begin();
504 wiret0 != theRelativeT0PerWire.end();
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;
512 if(
abs((*wiret0).second - evenLayersMean) < (2*evenLayersSigma))
513 evenLayersFinalMean = evenLayersFinalMean + (*wiret0).second;
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;
522 for(map<DTWireId, double>::const_iterator wiret0 = theRelativeT0PerWire.begin();
523 wiret0 != theRelativeT0PerWire.end();
525 if((*wiret0).first.layerId().superlayerId() == (*sl)->id()){
527 if(((*wiret0).first.layerId().layer()) % 2)
528 t0 = (*wiret0).second + (evenLayersFinalMean - oddLayersFinalMean);
530 t0 = (*wiret0).second;
532 cout<<
"[DTT0Calibration] Wire "<<(*wiret0).first<<
" has t0 "<<(*wiret0).second<<
" (relative, after even-odd layer corrections) " 533 <<
" sigma "<<theSigmaT0PerWire[(*wiret0).first]<<endl;
542 cout <<
"[DTT0Calibration]Computing relative t0 wrt to chamber average" << endl;
544 map<DTChamberId,double> sumT0ByChamber;
545 map<DTChamberId,int> countT0ByChamber;
553 int channelId =
tzero->channelId;
554 if ( channelId == 0 )
continue;
562 sumT0ByChamber[chamberId] = sumT0ByChamber[chamberId] + t0mean_f;
564 countT0ByChamber[chamberId]++;
582 int channelId =
tzero->channelId;
583 if ( channelId == 0 )
continue;
592 double t0mean = t0mean_f - (sumT0ByChamber[chamberId]/countT0ByChamber[chamberId]);
593 double t0rms = t0rms_f;
595 t0sWRTChamber->
set(wireId,
602 cout<<
"Changing t0 of wire "<<wireId<<
" from "<<
tzero->t0mean<<
" to "<<t0mean<<endl;
608 cout <<
"[DTT0Calibration]Writing values in DB!" << endl;
610 string t0Record =
"DTT0Rcd";
617 stringstream theStream;
620 theStream >> histoName;
626 stringstream theStream;
629 theStream >> histoName;
const_iterator begin() const
int set(int wheelId, int stationId, int sectorId, int slId, int layerId, int cellId, float t0mean, float t0rms, DTTimeUnits::type unit)
T getParameter(std::string const &) const
EventNumber_t event() const
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.
DTSuperLayerId superlayerId() const
Return the corresponding SuperLayerId.
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
int get(int wheelId, int stationId, int sectorId, int slId, int layerId, int cellId, float &t0mean, float &t0rms, DTTimeUnits::type unit) const
std::vector< DTT0Data >::const_iterator const_iterator
Access methods to data.
def getHistoName(wheel, station, sector)
Abs< T >::type abs(const T &t)
~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.
const_iterator end() const
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
std::vector< DTDigi >::const_iterator const_iterator
static const double tzero[3]
std::pair< const_iterator, const_iterator > Range
int station() const
Return the station number.
DTT0Calibration(const edm::ParameterSet &pset)
Constructor.
int wheel() const
Return the wheel number.
static void writeToDB(std::string record, T *payload)