00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "CalibMuon/DTCalibration/plugins/DTTTrigOffsetCalibration.h"
00011
00012 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00013 #include "FWCore/Framework/interface/EventSetup.h"
00014 #include "FWCore/Framework/interface/Event.h"
00015 #include "FWCore/Framework/interface/ESHandle.h"
00016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00017 #include "FWCore/Utilities/interface/InputTag.h"
00018
00019 #include "Geometry/DTGeometry/interface/DTGeometry.h"
00020 #include "Geometry/Records/interface/MuonGeometryRecord.h"
00021
00022 #include "DataFormats/DTRecHit/interface/DTRecSegment4DCollection.h"
00023 #include "CondFormats/DTObjects/interface/DTTtrig.h"
00024 #include "CondFormats/DataRecord/interface/DTTtrigRcd.h"
00025
00026 #include "CondFormats/DataRecord/interface/DTStatusFlagRcd.h"
00027 #include "CondFormats/DTObjects/interface/DTStatusFlag.h"
00028
00029 #include "CalibMuon/DTCalibration/interface/DTCalibDBUtils.h"
00030
00031
00032 #include <map>
00033 #include <string>
00034 #include <sstream>
00035 #include "TFile.h"
00036 #include "TH1F.h"
00037
00038 using namespace std;
00039 using namespace edm;
00040
00041 DTTTrigOffsetCalibration::DTTTrigOffsetCalibration(const ParameterSet& pset) {
00042
00043 LogVerbatim("Calibration") << "[DTTTrigOffsetCalibration] Constructor called!";
00044
00045
00046 string rootFileName = pset.getUntrackedParameter<string>("rootFileName","DTT0SegHistos.root");
00047 theFile_ = new TFile(rootFileName.c_str(), "RECREATE");
00048 theFile_->cd();
00049
00050 dbLabel = pset.getUntrackedParameter<string>("dbLabel", "");
00051
00052
00053 doTTrigCorrection_ = pset.getUntrackedParameter<bool>("doT0SegCorrection", false);
00054
00055
00056 theCalibChamber_ = pset.getUntrackedParameter<string>("calibChamber", "All");
00057
00058
00059 theRecHits4DLabel_ = pset.getParameter<InputTag>("recHits4DLabel");
00060
00061
00062 checkNoisyChannels_ = pset.getParameter<bool>("checkNoisyChannels");
00063
00064
00065 theMaxChi2_ = pset.getParameter<double>("maxChi2");
00066
00067
00068 theMaxPhiAngle_ = pset.getParameter<double>("maxAnglePhi");
00069
00070
00071 theMaxZAngle_ = pset.getParameter<double>("maxAngleZ");
00072 }
00073
00074 void DTTTrigOffsetCalibration::beginRun(const edm::Run& run, const edm::EventSetup& setup) {
00075 if(doTTrigCorrection_){
00076 ESHandle<DTTtrig> tTrig;
00077 setup.get<DTTtrigRcd>().get(dbLabel,tTrig);
00078 tTrigMap = &*tTrig;
00079 LogVerbatim("Calibration") << "[DTTTrigOffsetCalibration]: TTrig version: " << tTrig->version() << endl;
00080 }
00081 }
00082
00083 DTTTrigOffsetCalibration::~DTTTrigOffsetCalibration(){
00084 theFile_->Close();
00085 LogVerbatim("Calibration") << "[DTTTrigOffsetCalibration] Destructor called!";
00086 }
00087
00088 void DTTTrigOffsetCalibration::analyze(const Event & event, const EventSetup& eventSetup) {
00089 theFile_->cd();
00090 DTChamberId chosenChamberId;
00091
00092 if(theCalibChamber_ != "All") {
00093 stringstream linestr;
00094 int selWheel, selStation, selSector;
00095 linestr << theCalibChamber_;
00096 linestr >> selWheel >> selStation >> selSector;
00097 chosenChamberId = DTChamberId(selWheel, selStation, selSector);
00098 LogVerbatim("Calibration") << " chosen chamber " << chosenChamberId << endl;
00099 }
00100
00101
00102 ESHandle<DTGeometry> dtGeom;
00103 eventSetup.get<MuonGeometryRecord>().get(dtGeom);
00104
00105
00106 Handle<DTRecSegment4DCollection> all4DSegments;
00107 event.getByLabel(theRecHits4DLabel_, all4DSegments);
00108
00109
00110 ESHandle<DTStatusFlag> statusMap;
00111 if(checkNoisyChannels_) eventSetup.get<DTStatusFlagRcd>().get(statusMap);
00112
00113
00114 DTRecSegment4DCollection::id_iterator chamberIdIt;
00115 for (chamberIdIt = all4DSegments->id_begin();
00116 chamberIdIt != all4DSegments->id_end();
00117 ++chamberIdIt){
00118
00119
00120 const DTChamber* chamber = dtGeom->chamber(*chamberIdIt);
00121 LogTrace("Calibration") << "Chamber Id: " << *chamberIdIt;
00122
00123
00124 if(theT0SegHistoMap_.find(*chamberIdIt) == theT0SegHistoMap_.end()){
00125 bookHistos(*chamberIdIt);
00126 }
00127
00128
00129 if((theCalibChamber_ != "All") && ((*chamberIdIt) != chosenChamberId)) continue;
00130
00131
00132 DTRecSegment4DCollection::range range = all4DSegments->get((*chamberIdIt));
00133
00134
00135 for (DTRecSegment4DCollection::const_iterator segment = range.first;
00136 segment!=range.second; ++segment){
00137
00138 LogTrace("Calibration") << "Segment local pos (in chamber RF): " << (*segment).localPosition()
00139 << "\nSegment global pos: " << chamber->toGlobal((*segment).localPosition());
00140
00141
00142 double chiSquare = ((*segment).chi2()/(*segment).degreesOfFreedom());
00143
00144 if(chiSquare > theMaxChi2_) continue;
00145
00146
00147 if(!((*segment).phiSegment())){
00148 LogTrace("Calibration") << "No phi segment";
00149 }
00150 LocalPoint phiSeg2DPosInCham;
00151 LocalVector phiSeg2DDirInCham;
00152
00153 bool segmNoisy = false;
00154 map<DTSuperLayerId,vector<DTRecHit1D> > hitsBySLMap;
00155
00156 if((*segment).hasPhi()){
00157 const DTChamberRecSegment2D* phiSeg = (*segment).phiSegment();
00158 phiSeg2DPosInCham = phiSeg->localPosition();
00159 phiSeg2DDirInCham = phiSeg->localDirection();
00160
00161 vector<DTRecHit1D> phiHits = phiSeg->specificRecHits();
00162 for(vector<DTRecHit1D>::const_iterator hit = phiHits.begin();
00163 hit != phiHits.end(); ++hit) {
00164 DTWireId wireId = (*hit).wireId();
00165 DTSuperLayerId slId = wireId.superlayerId();
00166 hitsBySLMap[slId].push_back(*hit);
00167
00168
00169 if(checkNoisyChannels_) {
00170 bool isNoisy = false;
00171 bool isFEMasked = false;
00172 bool isTDCMasked = false;
00173 bool isTrigMask = false;
00174 bool isDead = false;
00175 bool isNohv = false;
00176 statusMap->cellStatus(wireId, isNoisy, isFEMasked, isTDCMasked, isTrigMask, isDead, isNohv);
00177 if(isNoisy) {
00178 LogTrace("Calibration") << "Wire: " << wireId << " is noisy, skipping!";
00179 segmNoisy = true;
00180 }
00181 }
00182 }
00183 }
00184
00185
00186 LocalVector zSeg2DDirInCham;
00187 LocalPoint zSeg2DPosInCham;
00188 if((*segment).hasZed()) {
00189 const DTSLRecSegment2D* zSeg = (*segment).zSegment();
00190 const DTSuperLayer* sl = chamber->superLayer(zSeg->superLayerId());
00191 zSeg2DPosInCham = chamber->toLocal(sl->toGlobal((*zSeg).localPosition()));
00192 zSeg2DDirInCham = chamber->toLocal(sl->toGlobal((*zSeg).localDirection()));
00193 hitsBySLMap[zSeg->superLayerId()] = zSeg->specificRecHits();
00194
00195
00196 vector<DTRecHit1D> zHits = zSeg->specificRecHits();
00197 for(vector<DTRecHit1D>::const_iterator hit = zHits.begin();
00198 hit != zHits.end(); ++hit) {
00199 DTWireId wireId = (*hit).wireId();
00200 if(checkNoisyChannels_) {
00201 bool isNoisy = false;
00202 bool isFEMasked = false;
00203 bool isTDCMasked = false;
00204 bool isTrigMask = false;
00205 bool isDead = false;
00206 bool isNohv = false;
00207 statusMap->cellStatus(wireId, isNoisy, isFEMasked, isTDCMasked, isTrigMask, isDead, isNohv);
00208 if(isNoisy) {
00209 LogTrace("Calibration") << "Wire: " << wireId << " is noisy, skipping!";
00210 segmNoisy = true;
00211 }
00212 }
00213 }
00214 }
00215
00216 if (segmNoisy) continue;
00217
00218 LocalPoint segment4DLocalPos = (*segment).localPosition();
00219 LocalVector segment4DLocalDir = (*segment).localDirection();
00220 if(fabs(atan(segment4DLocalDir.y()/segment4DLocalDir.z())* 180./Geom::pi()) > theMaxZAngle_) continue;
00221 if(fabs(atan(segment4DLocalDir.x()/segment4DLocalDir.z())* 180./Geom::pi()) > theMaxPhiAngle_) continue;
00222
00223 if((*segment).hasPhi()) {
00224
00225 if(segment->phiSegment()->ist0Valid()){
00226 (theT0SegHistoMap_[*chamberIdIt])[0]->Fill(segment->phiSegment()->t0());
00227 }
00228 }
00229 if((*segment).hasZed()){
00230
00231 if(segment->zSegment()->ist0Valid()){
00232 (theT0SegHistoMap_[*chamberIdIt])[1]->Fill(segment->zSegment()->t0());
00233 }
00234 }
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 }
00248 }
00249 }
00250
00251 void DTTTrigOffsetCalibration::endJob() {
00252 theFile_->cd();
00253
00254 LogVerbatim("Calibration") << "[DTTTrigOffsetCalibration]Writing histos to file!" << endl;
00255
00256 for(ChamberHistosMap::const_iterator itChHistos = theT0SegHistoMap_.begin(); itChHistos != theT0SegHistoMap_.end(); ++itChHistos){
00257 for(vector<TH1F*>::const_iterator itHist = (*itChHistos).second.begin();
00258 itHist != (*itChHistos).second.end(); ++itHist) (*itHist)->Write();
00259 }
00260
00261 if(doTTrigCorrection_){
00262
00263 DTTtrig* tTrig = new DTTtrig();
00264
00265 for(ChamberHistosMap::const_iterator itChHistos = theT0SegHistoMap_.begin(); itChHistos != theT0SegHistoMap_.end(); ++itChHistos){
00266 DTChamberId chId = itChHistos->first;
00267
00268 vector<DTSuperLayerId> slIds;
00269 slIds.push_back(DTSuperLayerId(chId,1));
00270 slIds.push_back(DTSuperLayerId(chId,3));
00271 if(chId.station() != 4) slIds.push_back(DTSuperLayerId(chId,2));
00272
00273 for(vector<DTSuperLayerId>::const_iterator itSl = slIds.begin(); itSl != slIds.end(); ++itSl){
00274
00275 float ttrigMean = 0;
00276 float ttrigSigma = 0;
00277 float kFactor = 0;
00278 tTrigMap->get(*itSl,ttrigMean,ttrigSigma,kFactor,DTTimeUnits::ns);
00279
00280
00281 float ttrigMeanNew = ttrigMean;
00282 float ttrigSigmaNew = ttrigSigma;
00283 float t0SegMean = (itSl->superLayer() != 2)?itChHistos->second[0]->GetMean():itChHistos->second[1]->GetMean();
00284
00285 float kFactorNew = (kFactor*ttrigSigma+t0SegMean)/ttrigSigma;
00286
00287 tTrig->set(*itSl,ttrigMeanNew,ttrigSigmaNew,kFactorNew,DTTimeUnits::ns);
00288 }
00289 }
00290 LogVerbatim("Calibration")<< "[DTTTrigOffsetCalibration]Writing ttrig object to DB!" << endl;
00291
00292 string tTrigRecord = "DTTtrigRcd";
00293 DTCalibDBUtils::writeToDB(tTrigRecord, tTrig);
00294 }
00295 }
00296
00297
00298 void DTTTrigOffsetCalibration::bookHistos(DTChamberId chId) {
00299
00300 LogTrace("Calibration") << " Booking histos for Chamber: " << chId;
00301
00302
00303 stringstream wheel; wheel << chId.wheel();
00304 stringstream station; station << chId.station();
00305 stringstream sector; sector << chId.sector();
00306
00307 string chHistoName =
00308 "_W" + wheel.str() +
00309 "_St" + station.str() +
00310 "_Sec" + sector.str();
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330 vector<TH1F*> histos;
00331
00332 histos.push_back(new TH1F(("hRPhiSegT0"+chHistoName).c_str(), "t0 from Phi segments", 250, -60., 60.));
00333 if(chId.station() != 4) histos.push_back(new TH1F(("hRZSegT0"+chHistoName).c_str(), "t0 from Z segments", 250, -60., 60.));
00334
00335 theT0SegHistoMap_[chId] = histos;
00336 }