00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "CalibMuon/DTCalibration/plugins/DTT0CalibrationNew.h"
00011 #include "CalibMuon/DTCalibration/interface/DTCalibDBUtils.h"
00012
00013 #include "FWCore/Framework/interface/Event.h"
00014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00015
00016 #include "Geometry/Records/interface/MuonGeometryRecord.h"
00017 #include "Geometry/Records/interface/MuonNumberingRecord.h"
00018
00019 #include "DataFormats/DTDigi/interface/DTDigiCollection.h"
00020 #include "CondFormats/DTObjects/interface/DTT0.h"
00021
00022 #include <CondFormats/DTObjects/interface/DTTtrig.h>
00023 #include <CondFormats/DataRecord/interface/DTTtrigRcd.h>
00024
00025 #include "TH1I.h"
00026 #include "TFile.h"
00027 #include "TKey.h"
00028 #include "TSpectrum.h"
00029 #include "TF1.h"
00030
00031 using namespace std;
00032 using namespace edm;
00033
00034
00035
00036 DTT0CalibrationNew::DTT0CalibrationNew(const edm::ParameterSet& pset) {
00037
00038 debug = pset.getUntrackedParameter<bool>("debug");
00039 if(debug)
00040 cout << "[DTT0CalibrationNew]Constructor called!" << endl;
00041
00042
00043 digiLabel = pset.getUntrackedParameter<string>("digiLabel");
00044
00045
00046 string rootFileName = pset.getUntrackedParameter<string>("rootFileName","DTT0PerLayer.root");
00047 theFile = new TFile(rootFileName.c_str(), "RECREATE");
00048
00049 theCalibWheel = pset.getUntrackedParameter<string>("calibWheel", "All");
00050 if(theCalibWheel != "All") {
00051 stringstream linestr;
00052 int selWheel;
00053 linestr << theCalibWheel;
00054 linestr >> selWheel;
00055 cout << "[DTT0CalibrationNewPerLayer] chosen wheel " << selWheel << endl;
00056 }
00057
00058
00059 theCalibSector = pset.getUntrackedParameter<string>("calibSector", "All");
00060 if(theCalibSector != "All") {
00061 stringstream linestr;
00062 int selSector;
00063 linestr << theCalibSector;
00064 linestr >> selSector;
00065 cout << "[DTT0CalibrationNewPerLayer] chosen sector " << selSector << endl;
00066 }
00067
00068 vector<string> defaultCell;
00069 defaultCell.push_back("None");
00070 cellsWithHistos = pset.getUntrackedParameter<vector<string> >("cellsWithHisto", defaultCell);
00071 for(vector<string>::const_iterator cell = cellsWithHistos.begin(); cell != cellsWithHistos.end(); cell++){
00072 if((*cell) != "None"){
00073 stringstream linestr;
00074 int wheel,sector,station,sl,layer,wire;
00075 linestr << (*cell);
00076 linestr >> wheel >> sector >> station >> sl >> layer >> wire;
00077 wireIdWithHistos.push_back(DTWireId(wheel,station,sector,sl,layer,wire));
00078 }
00079 }
00080
00081 hT0SectorHisto=0;
00082
00083 nevents=0;
00084 eventsForLayerT0 = pset.getParameter<unsigned int>("eventsForLayerT0");
00085 eventsForWireT0 = pset.getParameter<unsigned int>("eventsForWireT0");
00086 tpPeakWidth = pset.getParameter<double>("tpPeakWidth");
00087 tpPeakWidthPerLayer = pset.getParameter<double>("tpPeakWidthPerLayer");
00088 timeBoxWidth = pset.getParameter<unsigned int>("timeBoxWidth");
00089 rejectDigiFromPeak = pset.getParameter<unsigned int>("rejectDigiFromPeak");
00090
00091 spectrum = new TSpectrum(5);
00092 retryForLayerT0 = 0;
00093 }
00094
00095
00096 DTT0CalibrationNew::~DTT0CalibrationNew(){
00097 if(debug)
00098 cout << "[DTT0CalibrationNew]Destructor called!" << endl;
00099
00100 delete spectrum;
00101 theFile->Close();
00102 }
00103
00105 void DTT0CalibrationNew::analyze(const edm::Event & event, const edm::EventSetup& eventSetup) {
00106 if(debug || event.id().event() % 500==0)
00107 cout << "--- [DTT0CalibrationNew] Analysing Event: #Run: " << event.id().run()
00108 << " #Event: " << event.id().event() << endl;
00109 nevents++;
00110
00111
00112 Handle<DTDigiCollection> digis;
00113 event.getByLabel(digiLabel, digis);
00114
00115
00116 eventSetup.get<MuonGeometryRecord>().get(dtGeom);
00117
00118
00119 edm::ESHandle<DTTtrig> tTrigMap;
00120 eventSetup.get<DTTtrigRcd>().get(tTrigMap);
00121
00122
00123 DTDigiCollection::DigiRangeIterator dtLayerIt;
00124 for (dtLayerIt = digis->begin();
00125 dtLayerIt != digis->end();
00126 ++dtLayerIt){
00127
00128 const DTDigiCollection::Range& digiRange = (*dtLayerIt).second;
00129
00130
00131 const DTLayerId layerId = (*dtLayerIt).first;
00132 const DTChamberId chamberId = layerId.superlayerId().chamberId();
00133
00134 if((theCalibWheel != "All") && (layerId.superlayerId().chamberId().wheel() != selWheel))
00135 continue;
00136 if((theCalibSector != "All") && (layerId.superlayerId().chamberId().sector() != selSector))
00137 continue;
00138
00139
00140
00141
00142
00143 float tTrig,tTrigRMS;
00144 tTrigMap->slTtrig(layerId.superlayerId(), tTrig, tTrigRMS);
00145 if(debug&&(nevents <= 1)){
00146 cout << " Superlayer: " << layerId.superlayerId() << endl
00147 << " tTrig,tTrigRMS= " << tTrig << ", " << tTrigRMS << endl;
00148 }
00149
00150
00151 for (DTDigiCollection::const_iterator digi = digiRange.first;
00152 digi != digiRange.second;
00153 digi++) {
00154 double t0 = (*digi).countsTDC();
00155
00156
00157 if(nevents < eventsForLayerT0){
00158
00159 TH1I *hT0LayerHisto = theHistoLayerMap[layerId];
00160
00161 if(hT0LayerHisto == 0){
00162 theFile->cd();
00163 float hT0Min = tTrig - 2*tTrigRMS;
00164 float hT0Max = hT0Min + timeBoxWidth;
00165 hT0LayerHisto = new TH1I(getHistoName(layerId).c_str(),
00166 "T0 from pulses by layer (TDC counts, 1 TDC count = 0.781 ns)",
00167 timeBoxWidth,hT0Min,hT0Max);
00168 if(debug)
00169 cout << " New T0 per Layer Histo: " << hT0LayerHisto->GetName() << endl;
00170 theHistoLayerMap[layerId] = hT0LayerHisto;
00171 }
00172
00173
00174 theFile->cd();
00175 if(hT0LayerHisto != 0) {
00176
00177
00178 hT0LayerHisto->Fill(t0);
00179 }
00180 }
00181
00182
00183 if(nevents>eventsForLayerT0){
00184
00185 const DTWireId wireId(layerId, (*digi).wire());
00186 if(debug) {
00187 cout << " Wire: " << wireId << endl
00188 << " time (TDC counts): " << (*digi).countsTDC()<< endl;
00189 }
00190
00191
00192 vector<DTWireId>::iterator it = find(wireIdWithHistos.begin(),wireIdWithHistos.end(),wireId);
00193 if (it!=wireIdWithHistos.end()){
00194
00195 TH1I *hT0WireHisto = theHistoWireMap[wireId];
00196
00197 if(hT0WireHisto == 0){
00198 theFile->cd();
00199 hT0WireHisto = new TH1I(getHistoName(wireId).c_str(),"T0 from pulses by wire (TDC counts, 1 TDC count = 0.781 ns)",7000,0,7000);
00200
00201
00202 if(debug)
00203 cout << " New T0 per wire Histo: " << hT0WireHisto->GetName() << endl;
00204 theHistoWireMap[wireId] = hT0WireHisto;
00205 }
00206
00207 theFile->cd();
00208 if(hT0WireHisto != 0) {
00209
00210
00211 hT0WireHisto->Fill(t0);
00212 }
00213 }
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229 if(fabs(theTPPeakMap[layerId] - t0) > rejectDigiFromPeak){
00230 if(debug)
00231 cout<<"digi skipped because t0 too far from peak " << theTPPeakMap[layerId] << endl;
00232 continue;
00233 }
00234
00235
00236 theSumT0ByChamber[chamberId] = theSumT0ByChamber[chamberId] + t0;
00237 theCountT0ByChamber[chamberId]++;
00238
00239
00240 if(nevents< (eventsForLayerT0 + eventsForWireT0)){
00241 if(!nDigiPerWire_ref[wireId]){
00242 mK_ref[wireId] = 0;
00243 }
00244 nDigiPerWire_ref[wireId] = nDigiPerWire_ref[wireId] + 1;
00245 mK_ref[wireId] = mK_ref[wireId] + (t0-mK_ref[wireId])/nDigiPerWire_ref[wireId];
00246 }
00247
00248 else if(nevents>(eventsForLayerT0 + eventsForWireT0)){
00249 if(abs(t0-mK_ref[wireId]) > tpPeakWidth)
00250 continue;
00251 if(!nDigiPerWire[wireId]){
00252 theAbsoluteT0PerWire[wireId] = 0;
00253 qK[wireId] = 0;
00254 mK[wireId] = 0;
00255 }
00256 nDigiPerWire[wireId] = nDigiPerWire[wireId] + 1;
00257 theAbsoluteT0PerWire[wireId] = theAbsoluteT0PerWire[wireId] + t0;
00258
00259 qK[wireId] = qK[wireId] + ((nDigiPerWire[wireId]-1)*(t0-mK[wireId])*(t0-mK[wireId])/nDigiPerWire[wireId]);
00260 mK[wireId] = mK[wireId] + (t0-mK[wireId])/nDigiPerWire[wireId];
00261 }
00262 }
00263 }
00264 }
00265
00266
00267 if(nevents == eventsForLayerT0){
00268 bool increaseEvtsForLayerT0 = false;
00269 for(map<DTLayerId, TH1I*>::const_iterator lHisto = theHistoLayerMap.begin();
00270 lHisto != theHistoLayerMap.end();
00271 lHisto++){
00272 if(debug)
00273 cout<<"Reading histogram "<<(*lHisto).second->GetName()<<" with mean "<<(*lHisto).second->GetMean()<<" and RMS "<<(*lHisto).second->GetRMS() << endl;
00274
00275
00276
00277
00278 int npeaks = spectrum->Search((*lHisto).second,(tpPeakWidthPerLayer/2.),"",0.3);
00279
00280 float *peaks = spectrum->GetPositionX();
00281
00282 vector<float> peakMeans(peaks,peaks + npeaks);
00283
00284 sort(peakMeans.begin(),peakMeans.end());
00285
00286
00287 float tTrig,tTrigRMS;
00288 tTrigMap->slTtrig((*lHisto).first.superlayerId(), tTrig, tTrigRMS);
00289 float timeBoxCenter = (2*tTrig + (float)timeBoxWidth)/2.;
00290 float hMin = (*lHisto).second->GetXaxis()->GetXmin();
00291 float hMax = (*lHisto).second->GetXaxis()->GetXmax();
00292 vector<float>::const_iterator tpPeak = peakMeans.end();
00293 for(vector<float>::const_iterator it = peakMeans.begin(); it != peakMeans.end(); ++it){
00294 float mean = *it;
00295
00296 int bin = (*lHisto).second->GetXaxis()->FindBin(mean);
00297 float yp = (*lHisto).second->GetBinContent(bin);
00298 if(debug) cout << "Peak : (" << mean << "," << yp << ")" << endl;
00299
00300
00301 float previous_peak = (it == peakMeans.begin())?hMin:*(it - 1);
00302 float next_peak = (it == (peakMeans.end()-1))?hMax:*(it + 1);
00303
00304 float rangemin = mean - (mean - previous_peak)/8.;
00305 float rangemax = mean + (next_peak - mean)/8.;
00306 int binmin = (*lHisto).second->GetXaxis()->FindBin(rangemin);
00307 int binmax = (*lHisto).second->GetXaxis()->FindBin(rangemax);
00308 (*lHisto).second->GetXaxis()->SetRange(binmin,binmax);
00309
00310 float rms_seed = (*lHisto).second->GetRMS();
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331 if(rms_seed > tpPeakWidthPerLayer) continue;
00332
00333 if(fabs(mean - timeBoxCenter) < fabs(*tpPeak - timeBoxCenter)) tpPeak = it;
00334 }
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344 float selPeak = (tpPeak != peakMeans.end())?*tpPeak:(*lHisto).second->GetBinCenter((*lHisto).second->GetMaximumBin());
00345 if(debug) cout << "Peak selected at " << selPeak << endl;
00346
00347 theTPPeakMap[(*lHisto).first] = selPeak;
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381 }
00382
00383
00384
00385
00386
00387
00388
00389
00390 if(increaseEvtsForLayerT0){
00391 cout<<"[DTT0CalibrationNew]: t0 per layer are still uncorrect: trying with greater number of events"<<endl;
00392 eventsForLayerT0 = eventsForLayerT0*2;
00393 return;
00394 }
00395 }
00396 }
00397
00398 void DTT0CalibrationNew::endJob() {
00399
00400 DTT0* t0s = new DTT0();
00401 DTT0* t0sWRTChamber = new DTT0();
00402
00403 if(debug)
00404 cout << "[DTT0CalibrationNewPerLayer]Writing histos to file!" << endl;
00405
00406 theFile->cd();
00407
00408 for(map<DTWireId, TH1I*>::const_iterator wHisto = theHistoWireMap.begin();
00409 wHisto != theHistoWireMap.end();
00410 wHisto++) {
00411 (*wHisto).second->Write();
00412 }
00413 for(map<DTLayerId, TH1I*>::const_iterator lHisto = theHistoLayerMap.begin();
00414 lHisto != theHistoLayerMap.end();
00415 lHisto++) {
00416 (*lHisto).second->Write();
00417 }
00418
00419 if(debug)
00420 cout << "[DTT0CalibrationNew] Compute and store t0 and sigma per wire" << endl;
00421
00422 for(map<DTChamberId,double>::const_iterator chamber = theSumT0ByChamber.begin();
00423 chamber != theSumT0ByChamber.end();
00424 ++chamber) theRefT0ByChamber[(*chamber).first] = theSumT0ByChamber[(*chamber).first]/((double)theCountT0ByChamber[(*chamber).first]);
00425
00426 for(map<DTWireId, double>::const_iterator wiret0 = theAbsoluteT0PerWire.begin();
00427 wiret0 != theAbsoluteT0PerWire.end();
00428 wiret0++){
00429 if(nDigiPerWire[(*wiret0).first]){
00430 double t0 = (*wiret0).second/nDigiPerWire[(*wiret0).first];
00431 DTChamberId chamberId = ((*wiret0).first).chamberId();
00432
00433 theRelativeT0PerWire[(*wiret0).first] = t0 - theRefT0ByChamber[chamberId];
00434 cout<<"Wire "<<(*wiret0).first<<" has t0 "<<t0<<"(absolute) "<<theRelativeT0PerWire[(*wiret0).first]<<"(relative)";
00435
00436
00437 theSigmaT0PerWire[(*wiret0).first] = sqrt(qK[(*wiret0).first]/nDigiPerWire[(*wiret0).first]);
00438 cout<<" sigma "<<theSigmaT0PerWire[(*wiret0).first]<<endl;
00439 }
00440 else{
00441 cout<<"[DTT0CalibrationNew] ERROR: no digis in wire "<<(*wiret0).first<<endl;
00442 abort();
00443 }
00444 }
00445
00447
00448 const vector<DTSuperLayer*> superLayers = dtGeom->superLayers();
00449
00450 for(vector<DTSuperLayer*>::const_iterator sl = superLayers.begin();
00451 sl != superLayers.end(); sl++) {
00452
00453
00454
00455 double oddLayersMean=0;
00456 double evenLayersMean=0;
00457 double oddLayersDen=0;
00458 double evenLayersDen=0;
00459 for(map<DTWireId, double>::const_iterator wiret0 = theRelativeT0PerWire.begin();
00460 wiret0 != theRelativeT0PerWire.end();
00461 wiret0++){
00462 if((*wiret0).first.layerId().superlayerId() == (*sl)->id()){
00463 if(debug)
00464 cout<<"[DTT0CalibrationNew] Superlayer "<<(*sl)->id()
00465 <<"layer " <<(*wiret0).first.layerId().layer()<<" with "<<(*wiret0).second<<endl;
00466 if(((*wiret0).first.layerId().layer()) % 2){
00467 oddLayersMean = oddLayersMean + (*wiret0).second;
00468 oddLayersDen++;
00469 }
00470 else{
00471 evenLayersMean = evenLayersMean + (*wiret0).second;
00472 evenLayersDen++;
00473 }
00474 }
00475 }
00476 oddLayersMean = oddLayersMean/oddLayersDen;
00477 evenLayersMean = evenLayersMean/evenLayersDen;
00478 if(debug && oddLayersMean)
00479 cout<<"[DTT0CalibrationNew] Relative T0 mean for odd layers "<<oddLayersMean<<" even layers"<<evenLayersMean<<endl;
00480
00481
00482 double oddLayersSigma=0;
00483 double evenLayersSigma=0;
00484 for(map<DTWireId, double>::const_iterator wiret0 = theRelativeT0PerWire.begin();
00485 wiret0 != theRelativeT0PerWire.end();
00486 wiret0++){
00487 if((*wiret0).first.layerId().superlayerId() == (*sl)->id()){
00488 if(((*wiret0).first.layerId().layer()) % 2){
00489 oddLayersSigma = oddLayersSigma + ((*wiret0).second - oddLayersMean) * ((*wiret0).second - oddLayersMean);
00490 }
00491 else{
00492 evenLayersSigma = evenLayersSigma + ((*wiret0).second - evenLayersMean) * ((*wiret0).second - evenLayersMean);
00493 }
00494 }
00495 }
00496 oddLayersSigma = oddLayersSigma/oddLayersDen;
00497 evenLayersSigma = evenLayersSigma/evenLayersDen;
00498 oddLayersSigma = sqrt(oddLayersSigma);
00499 evenLayersSigma = sqrt(evenLayersSigma);
00500
00501 if(debug && oddLayersMean)
00502 cout<<"[DTT0CalibrationNew] Relative T0 sigma for odd layers "<<oddLayersSigma<<" even layers"<<evenLayersSigma<<endl;
00503
00504
00505 double oddLayersFinalMean=0;
00506 double evenLayersFinalMean=0;
00507 for(map<DTWireId, double>::const_iterator wiret0 = theRelativeT0PerWire.begin();
00508 wiret0 != theRelativeT0PerWire.end();
00509 wiret0++){
00510 if((*wiret0).first.layerId().superlayerId() == (*sl)->id()){
00511 if(((*wiret0).first.layerId().layer()) % 2){
00512 if(abs((*wiret0).second - oddLayersMean) < (2*oddLayersSigma))
00513 oddLayersFinalMean = oddLayersFinalMean + (*wiret0).second;
00514 }
00515 else{
00516 if(abs((*wiret0).second - evenLayersMean) < (2*evenLayersSigma))
00517 evenLayersFinalMean = evenLayersFinalMean + (*wiret0).second;
00518 }
00519 }
00520 }
00521 oddLayersFinalMean = oddLayersFinalMean/oddLayersDen;
00522 evenLayersFinalMean = evenLayersFinalMean/evenLayersDen;
00523 if(debug && oddLayersMean)
00524 cout<<"[DTT0CalibrationNew] Final relative T0 mean for odd layers "<<oddLayersFinalMean<<" even layers"<<evenLayersFinalMean<<endl;
00525
00526 for(map<DTWireId, double>::const_iterator wiret0 = theRelativeT0PerWire.begin();
00527 wiret0 != theRelativeT0PerWire.end();
00528 wiret0++){
00529 if((*wiret0).first.layerId().superlayerId() == (*sl)->id()){
00530 double t0=-999;
00531 if(((*wiret0).first.layerId().layer()) % 2)
00532 t0 = (*wiret0).second + (evenLayersFinalMean - oddLayersFinalMean);
00533 else
00534 t0 = (*wiret0).second;
00535
00536 cout<<"[DTT0CalibrationNew] Wire "<<(*wiret0).first<<" has t0 "<<(*wiret0).second<<" (relative, after even-odd layer corrections) "
00537 <<" sigma "<<theSigmaT0PerWire[(*wiret0).first]<<endl;
00538
00539 t0s->set((*wiret0).first, t0, theSigmaT0PerWire[(*wiret0).first],DTTimeUnits::counts);
00540 }
00541 }
00542 }
00543
00545 if(debug)
00546 cout << "[DTT0CalibrationNew]Computing relative t0 wrt to chamber average" << endl;
00547
00548 map<DTChamberId,double> sumT0ByChamber;
00549 map<DTChamberId,int> countT0ByChamber;
00550 for(DTT0::const_iterator tzero = t0s->begin();
00551 tzero != t0s->end(); tzero++) {
00552 DTChamberId chamberId((*tzero).first.wheelId,
00553 (*tzero).first.stationId,
00554 (*tzero).first.sectorId);
00555 sumT0ByChamber[chamberId] = sumT0ByChamber[chamberId] + (*tzero).second.t0mean;
00556 countT0ByChamber[chamberId]++;
00557 }
00558
00559
00560 for(DTT0::const_iterator tzero = t0s->begin();
00561 tzero != t0s->end(); tzero++) {
00562 DTChamberId chamberId((*tzero).first.wheelId,
00563 (*tzero).first.stationId,
00564 (*tzero).first.sectorId);
00565 double t0mean = ((*tzero).second.t0mean) - (sumT0ByChamber[chamberId]/countT0ByChamber[chamberId]);
00566 double t0rms = (*tzero).second.t0rms;
00567 DTWireId wireId((*tzero).first.wheelId,
00568 (*tzero).first.stationId,
00569 (*tzero).first.sectorId,
00570 (*tzero).first.slId,
00571 (*tzero).first.layerId,
00572 (*tzero).first.cellId);
00573 t0sWRTChamber->set(wireId,
00574 t0mean,
00575 t0rms,
00576 DTTimeUnits::counts);
00577 if(debug){
00578
00579 cout<<"Changing t0 of wire "<<wireId<<" from "<<(*tzero).second.t0mean<<" to "<<t0mean<<endl;
00580 }
00581 }
00582
00584 if(debug)
00585 cout << "[DTT0CalibrationNew]Writing values in DB!" << endl;
00586
00587 string t0Record = "DTT0Rcd";
00588
00589 DTCalibDBUtils::writeToDB(t0Record, t0sWRTChamber);
00590 }
00591
00592 string DTT0CalibrationNew::getHistoName(const DTWireId& wId) const {
00593 string histoName;
00594 stringstream theStream;
00595 theStream << "Ch_" << wId.wheel() << "_" << wId.station() << "_" << wId.sector()
00596 << "_SL" << wId.superlayer() << "_L" << wId.layer() << "_W"<< wId.wire() <<"_hT0Histo";
00597 theStream >> histoName;
00598 return histoName;
00599 }
00600
00601 string DTT0CalibrationNew::getHistoName(const DTLayerId& lId) const {
00602 string histoName;
00603 stringstream theStream;
00604 theStream << "Ch_" << lId.wheel() << "_" << lId.station() << "_" << lId.sector()
00605 << "_SL" << lId.superlayer() << "_L" << lId.layer() <<"_hT0Histo";
00606 theStream >> histoName;
00607 return histoName;
00608 }
00609