28 #include "TSpectrum.h"
40 cout <<
"[DTT0CalibrationNew]Constructor called!" << endl;
49 theFile =
new TFile(rootFileName.c_str(),
"RECREATE");
52 if(theCalibWheel !=
"All") {
55 linestr << theCalibWheel;
57 cout <<
"[DTT0CalibrationNewPerLayer] chosen wheel " << selWheel << endl;
62 if(theCalibSector !=
"All") {
65 linestr << theCalibSector;
67 cout <<
"[DTT0CalibrationNewPerLayer] chosen sector " << selSector << endl;
70 vector<string> defaultCell;
71 defaultCell.push_back(
"None");
73 for(vector<string>::const_iterator cell = cellsWithHistos.begin(); cell != cellsWithHistos.end(); cell++){
74 if((*cell) !=
"None"){
76 int wheel,sector,
station,sl,layer,wire;
78 linestr >> wheel >> sector >> station >> sl >> layer >> wire;
79 wireIdWithHistos.push_back(
DTWireId(wheel,station,sector,sl,layer,wire));
86 eventsForLayerT0 = pset.
getParameter<
unsigned int>(
"eventsForLayerT0");
87 eventsForWireT0 = pset.
getParameter<
unsigned int>(
"eventsForWireT0");
89 tpPeakWidthPerLayer = pset.
getParameter<
double>(
"tpPeakWidthPerLayer");
90 timeBoxWidth = pset.
getParameter<
unsigned int>(
"timeBoxWidth");
91 rejectDigiFromPeak = pset.
getParameter<
unsigned int>(
"rejectDigiFromPeak");
93 spectrum =
new TSpectrum(5);
100 cout <<
"[DTT0CalibrationNew]Destructor called!" << endl;
109 cout <<
"--- [DTT0CalibrationNew] Analysing Event: #Run: " << event.
id().
run()
110 <<
" #Event: " <<
event.id().event() << endl;
126 for (dtLayerIt = digis->begin();
127 dtLayerIt != digis->end();
133 const DTLayerId layerId = (*dtLayerIt).first;
145 float tTrig,tTrigRMS, kFactor;
149 <<
" tTrig,tTrigRMS= " << tTrig <<
", " << tTrigRMS << endl;
154 digi != digiRange.second;
156 double t0 = (*digi).countsTDC();
159 if(
nevents < eventsForLayerT0){
161 TH1I *hT0LayerHisto = theHistoLayerMap[layerId];
163 if(hT0LayerHisto == 0){
165 float hT0Min = tTrig - 2*tTrigRMS;
166 float hT0Max = hT0Min + timeBoxWidth;
168 "T0 from pulses by layer (TDC counts, 1 TDC count = 0.781 ns)",
169 timeBoxWidth,hT0Min,hT0Max);
171 cout <<
" New T0 per Layer Histo: " << hT0LayerHisto->GetName() << endl;
172 theHistoLayerMap[layerId] = hT0LayerHisto;
177 if(hT0LayerHisto != 0) {
180 hT0LayerHisto->Fill(t0);
187 const DTWireId wireId(layerId, (*digi).wire());
189 cout <<
" Wire: " << wireId << endl
190 <<
" time (TDC counts): " << (*digi).countsTDC()<< endl;
194 vector<DTWireId>::iterator it =
find(wireIdWithHistos.begin(),wireIdWithHistos.end(),wireId);
195 if (it!=wireIdWithHistos.end()){
197 TH1I *hT0WireHisto = theHistoWireMap[wireId];
199 if(hT0WireHisto == 0){
201 hT0WireHisto =
new TH1I(
getHistoName(wireId).c_str(),
"T0 from pulses by wire (TDC counts, 1 TDC count = 0.781 ns)",7000,0,7000);
205 cout <<
" New T0 per wire Histo: " << hT0WireHisto->GetName() << endl;
206 theHistoWireMap[wireId] = hT0WireHisto;
210 if(hT0WireHisto != 0) {
213 hT0WireHisto->Fill(t0);
231 if(fabs(theTPPeakMap[layerId] - t0) > rejectDigiFromPeak){
233 cout<<
"digi skipped because t0 too far from peak " << theTPPeakMap[layerId] << endl;
238 theSumT0ByChamber[chamberId] = theSumT0ByChamber[chamberId] + t0;
239 theCountT0ByChamber[chamberId]++;
242 if(
nevents< (eventsForLayerT0 + eventsForWireT0)){
243 if(!nDigiPerWire_ref[wireId]){
246 nDigiPerWire_ref[wireId] = nDigiPerWire_ref[wireId] + 1;
247 mK_ref[wireId] = mK_ref[wireId] + (t0-mK_ref[wireId])/nDigiPerWire_ref[wireId];
250 else if(
nevents>(eventsForLayerT0 + eventsForWireT0)){
251 if(
abs(t0-mK_ref[wireId]) > tpPeakWidth)
253 if(!nDigiPerWire[wireId]){
254 theAbsoluteT0PerWire[wireId] = 0;
258 nDigiPerWire[wireId] = nDigiPerWire[wireId] + 1;
259 theAbsoluteT0PerWire[wireId] = theAbsoluteT0PerWire[wireId] + t0;
261 qK[wireId] = qK[wireId] + ((nDigiPerWire[wireId]-1)*(t0-mK[wireId])*(t0-mK[wireId])/nDigiPerWire[wireId]);
262 mK[wireId] = mK[wireId] + (t0-mK[wireId])/nDigiPerWire[wireId];
269 if(
nevents == eventsForLayerT0){
270 bool increaseEvtsForLayerT0 =
false;
271 for(map<DTLayerId, TH1I*>::const_iterator lHisto = theHistoLayerMap.begin();
272 lHisto != theHistoLayerMap.end();
275 cout<<
"Reading histogram "<<(*lHisto).second->GetName()<<
" with mean "<<(*lHisto).second->GetMean()<<
" and RMS "<<(*lHisto).second->GetRMS() << endl;
280 int npeaks = spectrum->Search((*lHisto).second,(tpPeakWidthPerLayer/2.),
"",0.3);
282 float *peaks = spectrum->GetPositionX();
284 vector<float> peakMeans(peaks,peaks + npeaks);
286 sort(peakMeans.begin(),peakMeans.end());
289 float tTrig,tTrigRMS, kFactor;
290 tTrigMap->get((*lHisto).first.superlayerId(), tTrig, tTrigRMS, kFactor,
DTTimeUnits::counts );
292 float timeBoxCenter = (2*tTrig + (float)timeBoxWidth)/2.;
293 float hMin = (*lHisto).second->GetXaxis()->GetXmin();
294 float hMax = (*lHisto).second->GetXaxis()->GetXmax();
295 vector<float>::const_iterator tpPeak = peakMeans.end();
296 for(vector<float>::const_iterator it = peakMeans.begin(); it != peakMeans.end(); ++it){
299 int bin = (*lHisto).second->GetXaxis()->FindBin(mean);
300 float yp = (*lHisto).second->GetBinContent(bin);
301 if(
debug)
cout <<
"Peak : (" << mean <<
"," << yp <<
")" << endl;
304 float previous_peak = (it == peakMeans.begin())?hMin:*(it - 1);
305 float next_peak = (it == (peakMeans.end()-1))?hMax:*(it + 1);
307 float rangemin = mean - (mean - previous_peak)/8.;
308 float rangemax = mean + (next_peak -
mean)/8.;
309 int binmin = (*lHisto).second->GetXaxis()->FindBin(rangemin);
310 int binmax = (*lHisto).second->GetXaxis()->FindBin(rangemax);
311 (*lHisto).second->GetXaxis()->SetRange(binmin,binmax);
313 float rms_seed = (*lHisto).second->GetRMS();
334 if(rms_seed > tpPeakWidthPerLayer)
continue;
336 if(fabs(mean - timeBoxCenter) < fabs(*tpPeak - timeBoxCenter)) tpPeak = it;
347 float selPeak = (tpPeak != peakMeans.end())?*tpPeak:(*lHisto).second->GetBinCenter((*lHisto).second->GetMaximumBin());
348 if(
debug)
cout <<
"Peak selected at " << selPeak << endl;
350 theTPPeakMap[(*lHisto).first] = selPeak;
393 if(increaseEvtsForLayerT0){
394 cout<<
"[DTT0CalibrationNew]: t0 per layer are still uncorrect: trying with greater number of events"<<endl;
395 eventsForLayerT0 = eventsForLayerT0*2;
407 cout <<
"[DTT0CalibrationNewPerLayer]Writing histos to file!" << endl;
411 for(map<DTWireId, TH1I*>::const_iterator wHisto = theHistoWireMap.begin();
412 wHisto != theHistoWireMap.end();
414 (*wHisto).second->Write();
416 for(map<DTLayerId, TH1I*>::const_iterator lHisto = theHistoLayerMap.begin();
417 lHisto != theHistoLayerMap.end();
419 (*lHisto).second->Write();
423 cout <<
"[DTT0CalibrationNew] Compute and store t0 and sigma per wire" << endl;
425 for(map<DTChamberId,double>::const_iterator chamber = theSumT0ByChamber.begin();
426 chamber != theSumT0ByChamber.end();
427 ++chamber) theRefT0ByChamber[(*chamber).first] = theSumT0ByChamber[(*chamber).first]/((
double)theCountT0ByChamber[(*chamber).first]);
429 for(map<DTWireId, double>::const_iterator wiret0 = theAbsoluteT0PerWire.begin();
430 wiret0 != theAbsoluteT0PerWire.end();
432 if(nDigiPerWire[(*wiret0).first]){
433 double t0 = (*wiret0).second/nDigiPerWire[(*wiret0).first];
434 DTChamberId chamberId = ((*wiret0).first).chamberId();
436 theRelativeT0PerWire[(*wiret0).first] = t0 - theRefT0ByChamber[chamberId];
437 cout<<
"Wire "<<(*wiret0).first<<
" has t0 "<<t0<<
"(absolute) "<<theRelativeT0PerWire[(*wiret0).first]<<
"(relative)";
440 theSigmaT0PerWire[(*wiret0).first] =
sqrt(qK[(*wiret0).first]/nDigiPerWire[(*wiret0).first]);
441 cout<<
" sigma "<<theSigmaT0PerWire[(*wiret0).first]<<endl;
444 cout<<
"[DTT0CalibrationNew] ERROR: no digis in wire "<<(*wiret0).first<<endl;
451 const vector<DTSuperLayer*> superLayers = dtGeom->superLayers();
453 for(vector<DTSuperLayer*>::const_iterator sl = superLayers.begin();
454 sl != superLayers.end(); sl++) {
458 double oddLayersMean=0;
459 double evenLayersMean=0;
460 double oddLayersDen=0;
461 double evenLayersDen=0;
462 for(map<DTWireId, double>::const_iterator wiret0 = theRelativeT0PerWire.begin();
463 wiret0 != theRelativeT0PerWire.end();
465 if((*wiret0).first.layerId().superlayerId() == (*sl)->id()){
467 cout<<
"[DTT0CalibrationNew] Superlayer "<<(*sl)->id()
468 <<
"layer " <<(*wiret0).first.layerId().layer()<<
" with "<<(*wiret0).second<<endl;
469 if(((*wiret0).first.layerId().layer()) % 2){
470 oddLayersMean = oddLayersMean + (*wiret0).second;
474 evenLayersMean = evenLayersMean + (*wiret0).second;
479 oddLayersMean = oddLayersMean/oddLayersDen;
480 evenLayersMean = evenLayersMean/evenLayersDen;
481 if(
debug && oddLayersMean)
482 cout<<
"[DTT0CalibrationNew] Relative T0 mean for odd layers "<<oddLayersMean<<
" even layers"<<evenLayersMean<<endl;
485 double oddLayersSigma=0;
486 double evenLayersSigma=0;
487 for(map<DTWireId, double>::const_iterator wiret0 = theRelativeT0PerWire.begin();
488 wiret0 != theRelativeT0PerWire.end();
490 if((*wiret0).first.layerId().superlayerId() == (*sl)->id()){
491 if(((*wiret0).first.layerId().layer()) % 2){
492 oddLayersSigma = oddLayersSigma + ((*wiret0).second - oddLayersMean) * ((*wiret0).second - oddLayersMean);
495 evenLayersSigma = evenLayersSigma + ((*wiret0).second - evenLayersMean) * ((*wiret0).second - evenLayersMean);
499 oddLayersSigma = oddLayersSigma/oddLayersDen;
500 evenLayersSigma = evenLayersSigma/evenLayersDen;
501 oddLayersSigma =
sqrt(oddLayersSigma);
502 evenLayersSigma =
sqrt(evenLayersSigma);
504 if(
debug && oddLayersMean)
505 cout<<
"[DTT0CalibrationNew] Relative T0 sigma for odd layers "<<oddLayersSigma<<
" even layers"<<evenLayersSigma<<endl;
508 double oddLayersFinalMean=0;
509 double evenLayersFinalMean=0;
510 for(map<DTWireId, double>::const_iterator wiret0 = theRelativeT0PerWire.begin();
511 wiret0 != theRelativeT0PerWire.end();
513 if((*wiret0).first.layerId().superlayerId() == (*sl)->id()){
514 if(((*wiret0).first.layerId().layer()) % 2){
515 if(
abs((*wiret0).second - oddLayersMean) < (2*oddLayersSigma))
516 oddLayersFinalMean = oddLayersFinalMean + (*wiret0).second;
519 if(
abs((*wiret0).second - evenLayersMean) < (2*evenLayersSigma))
520 evenLayersFinalMean = evenLayersFinalMean + (*wiret0).second;
524 oddLayersFinalMean = oddLayersFinalMean/oddLayersDen;
525 evenLayersFinalMean = evenLayersFinalMean/evenLayersDen;
526 if(
debug && oddLayersMean)
527 cout<<
"[DTT0CalibrationNew] Final relative T0 mean for odd layers "<<oddLayersFinalMean<<
" even layers"<<evenLayersFinalMean<<endl;
529 for(map<DTWireId, double>::const_iterator wiret0 = theRelativeT0PerWire.begin();
530 wiret0 != theRelativeT0PerWire.end();
532 if((*wiret0).first.layerId().superlayerId() == (*sl)->id()){
534 if(((*wiret0).first.layerId().layer()) % 2)
535 t0 = (*wiret0).second + (evenLayersFinalMean - oddLayersFinalMean);
537 t0 = (*wiret0).second;
539 cout<<
"[DTT0CalibrationNew] Wire "<<(*wiret0).first<<
" has t0 "<<(*wiret0).second<<
" (relative, after even-odd layer corrections) "
540 <<
" sigma "<<theSigmaT0PerWire[(*wiret0).first]<<endl;
549 cout <<
"[DTT0CalibrationNew]Computing relative t0 wrt to chamber average" << endl;
551 map<DTChamberId,double> sumT0ByChamber;
552 map<DTChamberId,int> countT0ByChamber;
560 int channelId =
tzero->channelId;
561 if ( channelId == 0 )
continue;
569 sumT0ByChamber[chamberId] = sumT0ByChamber[chamberId] + t0mean_f;
571 countT0ByChamber[chamberId]++;
589 int channelId =
tzero->channelId;
590 if ( channelId == 0 )
continue;
599 double t0mean = t0mean_f - (sumT0ByChamber[chamberId]/countT0ByChamber[chamberId]);
600 double t0rms = t0rms_f;
602 t0sWRTChamber->
set(wireId,
609 cout<<
"Changing t0 of wire "<<wireId<<
" from "<<
tzero->t0mean<<
" to "<<t0mean<<endl;
615 cout <<
"[DTT0CalibrationNew]Writing values in DB!" << endl;
617 string t0Record =
"DTT0Rcd";
624 stringstream theStream;
627 theStream >> histoName;
633 stringstream theStream;
636 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
DTChamberId chamberId() const
Return the corresponding ChamberId.
void endJob()
Compute the mean and the RMS of the t0 from the maps and write them to the DB with channel granularit...
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.
virtual ~DTT0CalibrationNew()
Destructor.
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
int wire() const
Return the wire number.
const_iterator end() const
int superlayer() const
Return the superlayer number (deprecated method name)
DTT0CalibrationNew(const edm::ParameterSet &pset)
Constructor.
std::vector< DigiType >::const_iterator const_iterator
void analyze(const edm::Event &event, const edm::EventSetup &eventSetup)
Fill the maps with t0 (by channel)
static const double tzero[3]
std::pair< const_iterator, const_iterator > Range
std::string getHistoName(const DTWireId &wId) const
int station() const
Return the station number.
int wheel() const
Return the wheel number.
static void writeToDB(std::string record, T *payload)