CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/src/CalibMuon/DTCalibration/plugins/DTTTrigOffsetCalibration.cc

Go to the documentation of this file.
00001 
00002 /*
00003  *  See header file for a description of this class.
00004  *
00005  *  $Date: 2010/06/28 09:48:01 $
00006  *  $Revision: 1.6 $
00007  *  \author A. Vilela Pereira
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 /* C++ Headers */
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   // the root file which will contain the histos
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   // Do t0-seg correction to ttrig
00053   doTTrigCorrection_ = pset.getUntrackedParameter<bool>("doT0SegCorrection", false);
00054 
00055   // Chamber/s to calibrate
00056   theCalibChamber_ =  pset.getUntrackedParameter<string>("calibChamber", "All");
00057 
00058   // the name of the 4D rec hits collection
00059   theRecHits4DLabel_ = pset.getParameter<InputTag>("recHits4DLabel");
00060 
00061   //get the switch to check the noisy channels
00062   checkNoisyChannels_ = pset.getParameter<bool>("checkNoisyChannels");
00063 
00064   // get maximum chi2 value 
00065   theMaxChi2_ =  pset.getParameter<double>("maxChi2");
00066 
00067   // Maximum incidence angle for Phi SL 
00068   theMaxPhiAngle_ =  pset.getParameter<double>("maxAnglePhi");
00069 
00070   // Maximum incidence angle for Theta SL 
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   // Get the DT Geometry
00102   ESHandle<DTGeometry> dtGeom;
00103   eventSetup.get<MuonGeometryRecord>().get(dtGeom);
00104 
00105   // Get the rechit collection from the event
00106   Handle<DTRecSegment4DCollection> all4DSegments;
00107   event.getByLabel(theRecHits4DLabel_, all4DSegments); 
00108 
00109   // Get the map of noisy channels
00110   ESHandle<DTStatusFlag> statusMap;
00111   if(checkNoisyChannels_) eventSetup.get<DTStatusFlagRcd>().get(statusMap);
00112 
00113   // Loop over segments by chamber
00114   DTRecSegment4DCollection::id_iterator chamberIdIt;
00115   for (chamberIdIt = all4DSegments->id_begin();
00116        chamberIdIt != all4DSegments->id_end();
00117        ++chamberIdIt){
00118 
00119     // Get the chamber from the setup
00120     const DTChamber* chamber = dtGeom->chamber(*chamberIdIt);
00121     LogTrace("Calibration") << "Chamber Id: " << *chamberIdIt;
00122 
00123     // Book histos
00124     if(theT0SegHistoMap_.find(*chamberIdIt) == theT0SegHistoMap_.end()){
00125       bookHistos(*chamberIdIt);
00126     }
00127    
00128     // Calibrate just the chosen chamber/s    
00129     if((theCalibChamber_ != "All") && ((*chamberIdIt) != chosenChamberId)) continue;
00130 
00131     // Get the range for the corresponding ChamberId
00132     DTRecSegment4DCollection::range range = all4DSegments->get((*chamberIdIt));
00133 
00134     // Loop over the rechits of this DetUnit
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       //get the segment chi2
00142       double chiSquare = ((*segment).chi2()/(*segment).degreesOfFreedom());
00143       // cut on the segment chi2 
00144       if(chiSquare > theMaxChi2_) continue;
00145 
00146       // get the Phi 2D segment and plot the angle in the chamber RF
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();  // phiSeg lives in the chamber RF
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           // Check for noisy channels to skip them
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       // get the Theta 2D segment and plot the angle in the chamber RF
00186       LocalVector zSeg2DDirInCham;
00187       LocalPoint zSeg2DPosInCham;
00188       if((*segment).hasZed()) {
00189         const DTSLRecSegment2D* zSeg = (*segment).zSegment();  // zSeg lives in the SL RF
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         // Check for noisy channels to skip them
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; // cut on the angle
00221       if(fabs(atan(segment4DLocalDir.x()/segment4DLocalDir.z())* 180./Geom::pi()) > theMaxPhiAngle_) continue; // cut on the angle
00222       // Fill t0-seg values
00223       if((*segment).hasPhi()) {
00224         //if((segment->phiSegment()->t0()) != 0.00){
00225         if(segment->phiSegment()->ist0Valid()){
00226           (theT0SegHistoMap_[*chamberIdIt])[0]->Fill(segment->phiSegment()->t0());
00227         }
00228       }
00229       if((*segment).hasZed()){
00230         //if((segment->zSegment()->t0()) != 0.00){
00231         if(segment->zSegment()->ist0Valid()){
00232           (theT0SegHistoMap_[*chamberIdIt])[1]->Fill(segment->zSegment()->t0());
00233         }
00234       }
00235       
00236       // Fill t0-seg values
00237     //      if((*segment).hasPhi()) (theT0SegHistoMap_[*chamberIdIt])[0]->Fill(segment->phiSegment()->t0());
00238     //  if((*segment).hasZed()) (theT0SegHistoMap_[*chamberIdIt])[1]->Fill(segment->zSegment()->t0());
00239       //if((*segment).hasZed() && (*segment).hasPhi()) {}
00240 
00241       /*//loop over the segments 
00242       for(map<DTSuperLayerId,vector<DTRecHit1D> >::const_iterator slIdAndHits = hitsBySLMap.begin(); slIdAndHits != hitsBySLMap.end();  ++slIdAndHits) {
00243         if (slIdAndHits->second.size() < 3) continue;
00244         DTSuperLayerId slId =  slIdAndHits->first;
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     // Create the object to be written to DB
00263     DTTtrig* tTrig = new DTTtrig();
00264 
00265     for(ChamberHistosMap::const_iterator itChHistos = theT0SegHistoMap_.begin(); itChHistos != theT0SegHistoMap_.end(); ++itChHistos){
00266       DTChamberId chId = itChHistos->first;
00267       // Get SuperLayerId's for each ChamberId
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         // Get old values from DB
00275         float ttrigMean = 0;
00276         float ttrigSigma = 0;
00277         float kFactor = 0;
00278         tTrigMap->get(*itSl,ttrigMean,ttrigSigma,kFactor,DTTimeUnits::ns);
00279         //FIXME: verify if values make sense
00280         // Set new values
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     // Write the object to DB
00292     string tTrigRecord = "DTTtrigRcd";
00293     DTCalibDBUtils::writeToDB(tTrigRecord, tTrig);
00294   } 
00295 }
00296 
00297 // Book a set of histograms for a given Chamber
00298 void DTTTrigOffsetCalibration::bookHistos(DTChamberId chId) {
00299 
00300   LogTrace("Calibration") << "   Booking histos for Chamber: " << chId;
00301 
00302   // Compose the chamber name
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   /*// Define the step
00313   stringstream Step; Step << step;
00314 
00315   string chHistoName =
00316     "_STEP" + Step.str() +
00317     "_W" + wheel.str() +
00318     "_St" + station.str() +
00319     "_Sec" + sector.str();
00320 
00321   theDbe->setCurrentFolder("DT/DTCalibValidation/Wheel" + wheel.str() +
00322                            "/Station" + station.str() +
00323                            "/Sector" + sector.str());
00324   // Create the monitor elements
00325   vector<MonitorElement *> histos;
00326   // Note hte order matters
00327   histos.push_back(theDbe->book1D("hRPhiSegT0"+chHistoName, "t0 from Phi segments", 200, -25., 25.));
00328   histos.push_back(theDbe->book1D("hRZSegT0"+chHistoName, "t0 from Z segments", 200, -25., 25.));*/
00329 
00330   vector<TH1F*> histos;
00331   // Note the order matters
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 }