31 cout <<
"[DTT0CalibrationRMS]Constructor called!" << endl;
38 theFile =
new TFile(rootFileName.c_str(),
"RECREATE");
41 if(theCalibWheel !=
"All") {
44 linestr << theCalibWheel;
46 cout <<
"[DTT0CalibrationRMSPerLayer] chosen wheel " << selWheel << endl;
51 if(theCalibSector !=
"All") {
54 linestr << theCalibSector;
56 cout <<
"[DTT0CalibrationRMSPerLayer] chosen sector " << selSector << endl;
59 vector<string> defaultCell;
60 defaultCell.push_back(
"None");
62 for(vector<string>::const_iterator cell = cellsWithHistos.begin(); cell != cellsWithHistos.end(); ++cell){
63 if((*cell) !=
"None"){
67 linestr >> wheel >> sector >> station >> sl >> layer >> wire;
68 wireIdWithHistos.push_back(
DTWireId(wheel,station,sector,sl,layer,wire));
72 hT0SectorHisto=
nullptr;
75 eventsForLayerT0 = pset.
getParameter<
unsigned int>(
"eventsForLayerT0");
76 eventsForWireT0 = pset.
getParameter<
unsigned int>(
"eventsForWireT0");
77 rejectDigiFromPeak = pset.
getParameter<
unsigned int>(
"rejectDigiFromPeak");
80 correctByChamberMean_ = pset.
getParameter<
bool>(
"correctByChamberMean");
86 cout <<
"[DTT0CalibrationRMS]Destructor called!" << endl;
94 cout <<
"--- [DTT0CalibrationRMS] Analysing Event: #Run: " << event.
id().
run()
95 <<
" #Event: " <<
event.id().event() << endl;
107 for (dtLayerIt = digis->begin();
108 dtLayerIt != digis->end();
114 const DTLayerId layerId = (*dtLayerIt).first;
127 digi != digiRange.second;
129 double t0 = (*digi).countsTDC();
132 if(nevents < eventsForLayerT0){
134 TH1I *hT0LayerHisto = theHistoLayerMap[layerId];
136 if(hT0LayerHisto ==
nullptr){
139 "T0 from pulses by layer (TDC counts, 1 TDC count = 0.781 ns)",
140 200, t0-100, t0+100);
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
161 <<
" time (TDC counts): " << (*digi).countsTDC()<< endl;
165 vector<DTWireId>::iterator it_wire =
find(wireIdWithHistos.begin(),wireIdWithHistos.end(),wireId);
166 if(it_wire != wireIdWithHistos.end()){
167 if(theHistoWireMap.find(wireId) == theHistoWireMap.end()){
168 theHistoWireMap[wireId] =
new TH1I(
getHistoName(wireId).c_str(),
"T0 from pulses by wire (TDC counts, 1 TDC count = 0.781 ns)",7000,0,7000);
169 if(
debug)
cout <<
" New T0 per wire Histo: " << (theHistoWireMap[wireId])->GetName() << endl;
171 if(theHistoWireMap_ref.find(wireId) == theHistoWireMap_ref.end()){
172 theHistoWireMap_ref[wireId] =
new TH1I((
getHistoName(wireId) +
"_ref").c_str(),
"T0 from pulses by wire (TDC counts, 1 TDC count = 0.781 ns)",7000,0,7000);
173 if(
debug)
cout <<
" New T0 per wire Histo: " << (theHistoWireMap_ref[wireId])->GetName() << endl;
176 TH1I* hT0WireHisto = theHistoWireMap[wireId];
179 if(hT0WireHisto) hT0WireHisto->Fill(t0);
183 if(
abs(hT0SectorHisto->GetBinCenter(hT0SectorHisto->GetMaximumBin()) - t0) > rejectDigiFromPeak){
185 cout<<
"digi skipped because t0 per sector "<<hT0SectorHisto->GetBinCenter(hT0SectorHisto->GetMaximumBin())<<endl;
190 if(nevents< (eventsForLayerT0 + eventsForWireT0)){
192 if(it_wire != wireIdWithHistos.end()){
193 TH1I* hT0WireHisto_ref = theHistoWireMap_ref[wireId];
195 if(hT0WireHisto_ref) hT0WireHisto_ref->Fill(t0);
197 if(!nDigiPerWire_ref[wireId]){
200 nDigiPerWire_ref[wireId] = nDigiPerWire_ref[wireId] + 1;
201 mK_ref[wireId] = mK_ref[wireId] + (t0-mK_ref[wireId])/nDigiPerWire_ref[wireId];
204 else if(nevents>(eventsForLayerT0 + eventsForWireT0)){
205 if(
abs(t0-mK_ref[wireId]) > tpPeakWidth)
207 if(!nDigiPerWire[wireId]){
208 theAbsoluteT0PerWire[wireId] = 0;
212 nDigiPerWire[wireId] = nDigiPerWire[wireId] + 1;
213 theAbsoluteT0PerWire[wireId] = theAbsoluteT0PerWire[wireId] +
t0;
215 qK[wireId] = qK[wireId] + ((nDigiPerWire[wireId]-1)*(t0-mK[wireId])*(t0-mK[wireId])/nDigiPerWire[wireId]);
216 mK[wireId] = mK[wireId] + (t0-mK[wireId])/nDigiPerWire[wireId];
223 if(nevents == eventsForLayerT0){
224 for(map<DTLayerId, TH1I*>::const_iterator lHisto = theHistoLayerMap.begin();
225 lHisto != theHistoLayerMap.end();
228 cout<<
"Reading histogram "<<(*lHisto).second->GetName()<<
" with mean "<<(*lHisto).second->GetMean()<<
" and RMS "<<(*lHisto).second->GetRMS();
231 if((*lHisto).second->GetRMS()<5.0){
232 if(hT0SectorHisto ==
nullptr){
233 hT0SectorHisto =
new TH1D(
"hT0AllLayerOfSector",
"T0 from pulses per layer in sector",
238 cout<<
" accepted"<<endl;
239 hT0SectorHisto->Fill((*lHisto).second->GetMean());
256 theT0LayerMap[(*lHisto).second->GetName()] = (*lHisto).second->GetMean();
257 theSigmaT0LayerMap[(*lHisto).second->GetName()] = (*lHisto).second->GetRMS();
260 cout<<
"[DTT0CalibrationRMS]: All the t0 per layer are still uncorrect: trying with greater number of events"<<endl;
261 eventsForLayerT0 = eventsForLayerT0*2;
265 cout<<
"[DTT0CalibrationRMS] t0 reference for this sector "<<
266 hT0SectorHisto->GetBinCenter(hT0SectorHisto->GetMaximumBin())<<endl;
278 cout <<
"[DTT0CalibrationRMSPerLayer]Writing histos to file!" << endl;
281 theFile->WriteTObject(hT0SectorHisto);
283 for(map<DTWireId, TH1I*>::const_iterator wHisto = theHistoWireMap.begin();
284 wHisto != theHistoWireMap.end();
286 (*wHisto).second->Write();
288 for(map<DTWireId, TH1I*>::const_iterator wHisto = theHistoWireMap_ref.begin();
289 wHisto != theHistoWireMap_ref.end();
291 (*wHisto).second->Write();
293 for(map<DTLayerId, TH1I*>::const_iterator lHisto = theHistoLayerMap.begin();
294 lHisto != theHistoLayerMap.end();
296 (*lHisto).second->Write();
300 cout <<
"[DTT0CalibrationRMS] Compute and store t0 and sigma per wire" << endl;
302 for(map<DTWireId, double>::const_iterator wiret0 = theAbsoluteT0PerWire.begin();
303 wiret0 != theAbsoluteT0PerWire.end();
305 if(nDigiPerWire[(*wiret0).first]){
306 double t0 = (*wiret0).second/nDigiPerWire[(*wiret0).first];
308 theRelativeT0PerWire[(*wiret0).first] = t0 - hT0SectorHisto->GetBinCenter(hT0SectorHisto->GetMaximumBin());
311 theSigmaT0PerWire[(*wiret0).first] =
sqrt(qK[(*wiret0).first]/nDigiPerWire[(*wiret0).first]);
313 cout <<
"Wire " << (*wiret0).first <<
" has t0 " << t0 <<
"(absolute) " 314 << theRelativeT0PerWire[(*wiret0).first] <<
"(relative)" 315 <<
" sigma " << theSigmaT0PerWire[(*wiret0).first] << endl;
320 cout<<
"[DTT0CalibrationRMS] ERROR: no digis in wire "<<(*wiret0).first<<endl;
325 if(correctByChamberMean_){
328 const vector<const DTSuperLayer*> superLayers = dtGeom->superLayers();
330 for(vector<const DTSuperLayer*>::const_iterator sl = superLayers.begin();
331 sl != superLayers.end(); sl++) {
335 double oddLayersMean=0;
336 double evenLayersMean=0;
337 double oddLayersDen=0;
338 double evenLayersDen=0;
339 for(map<DTWireId, double>::const_iterator wiret0 = theRelativeT0PerWire.begin();
340 wiret0 != theRelativeT0PerWire.end();
342 if((*wiret0).first.layerId().superlayerId() == (*sl)->id()){
344 cout<<
"[DTT0CalibrationRMS] Superlayer "<<(*sl)->id()
345 <<
"layer " <<(*wiret0).first.layerId().layer()<<
" with "<<(*wiret0).second<<endl;
346 if(((*wiret0).first.layerId().layer()) % 2){
347 oddLayersMean = oddLayersMean + (*wiret0).second;
351 evenLayersMean = evenLayersMean + (*wiret0).second;
356 oddLayersMean = oddLayersMean/oddLayersDen;
357 evenLayersMean = evenLayersMean/evenLayersDen;
359 cout<<
"[DTT0CalibrationRMS] Relative T0 mean for odd layers "<<oddLayersMean<<
" even layers"<<evenLayersMean<<endl;
362 double oddLayersSigma=0;
363 double evenLayersSigma=0;
364 for(map<DTWireId, double>::const_iterator wiret0 = theRelativeT0PerWire.begin();
365 wiret0 != theRelativeT0PerWire.end();
367 if((*wiret0).first.layerId().superlayerId() == (*sl)->id()){
368 if(((*wiret0).first.layerId().layer()) % 2){
369 oddLayersSigma = oddLayersSigma + ((*wiret0).second - oddLayersMean) * ((*wiret0).second - oddLayersMean);
372 evenLayersSigma = evenLayersSigma + ((*wiret0).second - evenLayersMean) * ((*wiret0).second - evenLayersMean);
376 oddLayersSigma = oddLayersSigma/oddLayersDen;
377 evenLayersSigma = evenLayersSigma/evenLayersDen;
378 oddLayersSigma =
sqrt(oddLayersSigma);
379 evenLayersSigma =
sqrt(evenLayersSigma);
382 cout<<
"[DTT0CalibrationRMS] Relative T0 sigma for odd layers "<<oddLayersSigma<<
" even layers"<<evenLayersSigma<<endl;
385 double oddLayersFinalMean=0;
386 double evenLayersFinalMean=0;
387 for(map<DTWireId, double>::const_iterator wiret0 = theRelativeT0PerWire.begin();
388 wiret0 != theRelativeT0PerWire.end();
390 if((*wiret0).first.layerId().superlayerId() == (*sl)->id()){
391 if(((*wiret0).first.layerId().layer()) % 2){
392 if(
abs((*wiret0).second - oddLayersMean) < (2*oddLayersSigma))
393 oddLayersFinalMean = oddLayersFinalMean + (*wiret0).second;
396 if(
abs((*wiret0).second - evenLayersMean) < (2*evenLayersSigma))
397 evenLayersFinalMean = evenLayersFinalMean + (*wiret0).second;
401 oddLayersFinalMean = oddLayersFinalMean/oddLayersDen;
402 evenLayersFinalMean = evenLayersFinalMean/evenLayersDen;
404 cout<<
"[DTT0CalibrationRMS] Final relative T0 mean for odd layers "<<oddLayersFinalMean<<
" even layers"<<evenLayersFinalMean<<endl;
406 for(map<DTWireId, double>::const_iterator wiret0 = theRelativeT0PerWire.begin();
407 wiret0 != theRelativeT0PerWire.end();
409 if((*wiret0).first.layerId().superlayerId() == (*sl)->id()){
411 if(((*wiret0).first.layerId().layer()) % 2)
412 t0 = (*wiret0).second + (evenLayersFinalMean - oddLayersFinalMean);
414 t0 = (*wiret0).second;
416 cout <<
"[DTT0CalibrationRMS] Wire " << (*wiret0).first <<
" has t0 " << (*wiret0).second
417 <<
" (relative, after even-odd layer corrections) " 418 <<
" sigma " << theSigmaT0PerWire[(*wiret0).first] << endl;
428 cout <<
"[DTT0CalibrationRMS]Computing relative t0 wrt to chamber average" << endl;
430 map<DTChamberId,double> sumT0ByChamber;
431 map<DTChamberId,int> countT0ByChamber;
434 int channelId =
tzero->channelId;
435 if ( channelId == 0 )
continue;
443 sumT0ByChamber[chamberId] = sumT0ByChamber[chamberId] + t0mean_f;
445 countT0ByChamber[chamberId]++;
451 int channelId =
tzero->channelId;
452 if ( channelId == 0 )
continue;
461 double t0mean = t0mean_f - (sumT0ByChamber[chamberId]/countT0ByChamber[chamberId]);
462 double t0rms = t0rms_f;
464 t0sWRTChamber->
set(wireId,
470 cout <<
"Changing t0 of wire " << wireId <<
" from " << t0mean_f
471 <<
" to " << t0mean << endl;
477 cout <<
"[DTT0CalibrationRMS]Writing values in DB!" << endl;
479 string t0Record =
"DTT0Rcd";
486 string histoName =
"Ch_" + std::to_string(wId.
wheel()) +
"_" + std::to_string(wId.
station())
487 +
"_" + std::to_string(wId.
sector()) +
"_SL" + std::to_string(wId.
superlayer())
488 +
"_L" + std::to_string(wId.
layer()) +
"_W" + std::to_string(wId.
wire()) +
"_hT0Histo";
493 string histoName =
"Ch_" + std::to_string(lId.
wheel()) +
"_" + std::to_string(lId.
station())
494 +
"_" + std::to_string(lId.
sector()) +
"_SL" + std::to_string(lId.
superlayer())
495 +
"_L" + std::to_string(lId.
layer()) +
"_hT0Histo";
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.
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.
std::string getHistoName(const DTWireId &wId) const
~DTT0CalibrationRMS() override
Destructor.
def getHistoName(wheel, station, sector)
Abs< T >::type abs(const T &t)
int wire() const
Return the wire number.
const_iterator end() const
int superlayer() const
Return the superlayer number (deprecated method name)
std::vector< DigiType >::const_iterator const_iterator
DTT0CalibrationRMS(const edm::ParameterSet &pset)
Constructor.
static const double tzero[3]
void analyze(const edm::Event &event, const edm::EventSetup &eventSetup) override
Fill the maps with t0 (by channel)
std::pair< const_iterator, const_iterator > Range
int station() const
Return the station number.
int wheel() const
Return the wheel number.
static void writeToDB(std::string record, T *payload)