CMS 3D CMS Logo

DTTTrigOffsetCalibration.cc
Go to the documentation of this file.
1 
2 /*
3  * See header file for a description of this class.
4  *
5  * \author A. Vilela Pereira
6  */
7 
9 
16 
19 
24 
27 
28 #include <string>
29 #include <sstream>
30 #include "TFile.h"
31 #include "TH1F.h"
32 
33 using namespace std;
34 using namespace edm;
35 
37  : theRecHits4DToken_(consumes<DTRecSegment4DCollection>(pset.getParameter<InputTag>("recHits4DLabel"))),
38  doTTrigCorrection_(pset.getUntrackedParameter<bool>("doT0SegCorrection", false)),
39  theCalibChamber_(pset.getUntrackedParameter<string>("calibChamber", "All")),
40  ttrigToken_(
41  esConsumes<edm::Transition::BeginRun>(edm::ESInputTag("", pset.getUntrackedParameter<string>("dbLabel")))),
42  dtGeomToken_(esConsumes()) {
43  LogVerbatim("Calibration") << "[DTTTrigOffsetCalibration] Constructor called!";
44 
46  select_ = new DTSegmentSelector(pset, collector);
47 
48  // the root file which will contain the histos
49  string rootFileName = pset.getUntrackedParameter<string>("rootFileName", "DTT0SegHistos.root");
50  rootFile_ = new TFile(rootFileName.c_str(), "RECREATE");
51  rootFile_->cd();
52 }
53 
55  if (doTTrigCorrection_) {
57  tTrig = setup.getHandle(ttrigToken_);
58  tTrigMap_ = &setup.getData(ttrigToken_);
59  LogVerbatim("Calibration") << "[DTTTrigOffsetCalibration]: TTrig version: " << tTrig->version() << endl;
60  }
61 }
62 
64  rootFile_->Close();
65  LogVerbatim("Calibration") << "[DTTTrigOffsetCalibration] Destructor called!";
66 }
67 
69  rootFile_->cd();
70  DTChamberId chosenChamberId;
71 
72  if (theCalibChamber_ != "All") {
73  stringstream linestr;
74  int selWheel, selStation, selSector;
75  linestr << theCalibChamber_;
76  linestr >> selWheel >> selStation >> selSector;
77  chosenChamberId = DTChamberId(selWheel, selStation, selSector);
78  LogVerbatim("Calibration") << " chosen chamber " << chosenChamberId << endl;
79  }
80 
81  // Get the DT Geometry
82  ESHandle<DTGeometry> dtGeom;
83  dtGeom = eventSetup.getHandle(dtGeomToken_);
84 
85  // Get the rechit collection from the event
86  const Handle<DTRecSegment4DCollection>& all4DSegments = event.getHandle(theRecHits4DToken_);
87 
88  // Loop over segments by chamber
90  for (chamberIdIt = all4DSegments->id_begin(); chamberIdIt != all4DSegments->id_end(); ++chamberIdIt) {
91  // Get the chamber from the setup
92  const DTChamber* chamber = dtGeom->chamber(*chamberIdIt);
93  LogTrace("Calibration") << "Chamber Id: " << *chamberIdIt;
94 
95  // Book histos
96  if (theT0SegHistoMap_.find(*chamberIdIt) == theT0SegHistoMap_.end()) {
97  bookHistos(*chamberIdIt);
98  }
99 
100  // Calibrate just the chosen chamber/s
101  if ((theCalibChamber_ != "All") && ((*chamberIdIt) != chosenChamberId))
102  continue;
103 
104  // Get the range for the corresponding ChamberId
105  DTRecSegment4DCollection::range range = all4DSegments->get((*chamberIdIt));
106 
107  // Loop over the rechits of this DetUnit
108  for (DTRecSegment4DCollection::const_iterator segment = range.first; segment != range.second; ++segment) {
109  LogTrace("Calibration") << "Segment local pos (in chamber RF): " << (*segment).localPosition()
110  << "\nSegment global pos: " << chamber->toGlobal((*segment).localPosition());
111 
112  if (!(*select_)(*segment, event, eventSetup))
113  continue;
114 
115  // Fill t0-seg values
116  if ((*segment).hasPhi()) {
117  //if( segment->phiSegment()->ist0Valid() ){
118  if ((segment->phiSegment()->t0()) != 0.00) {
119  (theT0SegHistoMap_[*chamberIdIt])[0]->Fill(segment->phiSegment()->t0());
120  }
121  }
122  if ((*segment).hasZed()) {
123  //if( segment->zSegment()->ist0Valid() ){
124  if ((segment->zSegment()->t0()) != 0.00) {
125  (theT0SegHistoMap_[*chamberIdIt])[1]->Fill(segment->zSegment()->t0());
126  }
127  }
128  } // DTRecSegment4DCollection::const_iterator segment
129  } // DTRecSegment4DCollection::id_iterator chamberIdIt
130 } // DTTTrigOffsetCalibration::analyze
131 
133  rootFile_->cd();
134 
135  LogVerbatim("Calibration") << "[DTTTrigOffsetCalibration] Writing histos to file!" << endl;
136 
137  for (ChamberHistosMap::const_iterator itChHistos = theT0SegHistoMap_.begin(); itChHistos != theT0SegHistoMap_.end();
138  ++itChHistos) {
139  for (vector<TH1F*>::const_iterator itHist = (*itChHistos).second.begin(); itHist != (*itChHistos).second.end();
140  ++itHist)
141  (*itHist)->Write();
142  }
143 
144  if (doTTrigCorrection_) {
145  // Create the object to be written to DB
146  DTTtrig* tTrig = new DTTtrig();
147 
148  for (ChamberHistosMap::const_iterator itChHistos = theT0SegHistoMap_.begin(); itChHistos != theT0SegHistoMap_.end();
149  ++itChHistos) {
150  DTChamberId chId = itChHistos->first;
151  // Get SuperLayerId's for each ChamberId
152  vector<DTSuperLayerId> slIds;
153  slIds.push_back(DTSuperLayerId(chId, 1));
154  slIds.push_back(DTSuperLayerId(chId, 3));
155  if (chId.station() != 4)
156  slIds.push_back(DTSuperLayerId(chId, 2));
157 
158  for (vector<DTSuperLayerId>::const_iterator itSl = slIds.begin(); itSl != slIds.end(); ++itSl) {
159  // Get old values from DB
160  float ttrigMean = 0;
161  float ttrigSigma = 0;
162  float kFactor = 0;
163  tTrigMap_->get(*itSl, ttrigMean, ttrigSigma, kFactor, DTTimeUnits::ns);
164  //FIXME: verify if values make sense
165  // Set new values
166  float ttrigMeanNew = ttrigMean;
167  float ttrigSigmaNew = ttrigSigma;
168  float t0SegMean =
169  (itSl->superLayer() != 2) ? itChHistos->second[0]->GetMean() : itChHistos->second[1]->GetMean();
170 
171  float kFactorNew = (kFactor * ttrigSigma + t0SegMean) / ttrigSigma;
172 
173  tTrig->set(*itSl, ttrigMeanNew, ttrigSigmaNew, kFactorNew, DTTimeUnits::ns);
174  }
175  }
176  LogVerbatim("Calibration") << "[DTTTrigOffsetCalibration] Writing ttrig object to DB!" << endl;
177  // Write the object to DB
178  string tTrigRecord = "DTTtrigRcd";
179  DTCalibDBUtils::writeToDB(tTrigRecord, tTrig);
180  }
181 }
182 
183 // Book a set of histograms for a given Chamber
185  LogTrace("Calibration") << " Booking histos for Chamber: " << chId;
186 
187  // Compose the chamber name
191 
192  string chHistoName = "_W" + wheel + "_St" + station + "_Sec" + sector;
193 
194  vector<TH1F*> histos;
195  // Note the order matters
196  histos.push_back(new TH1F(("hRPhiSegT0" + chHistoName).c_str(), "t0 from Phi segments", 500, -60., 60.));
197  if (chId.station() != 4)
198  histos.push_back(new TH1F(("hRZSegT0" + chHistoName).c_str(), "t0 from Z segments", 500, -60., 60.));
199 
200  theT0SegHistoMap_[chId] = histos;
201 }
Log< level::Info, true > LogVerbatim
int station() const
Return the station number.
Definition: DTChamberId.h:42
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
std::pair< const_iterator, const_iterator > range
iterator range
Definition: RangeMap.h:50
static void writeToDB(std::string record, const T &payload)
void beginRun(const edm::Run &run, const edm::EventSetup &setup) override
void analyze(const edm::Event &event, const edm::EventSetup &eventSetup) override
identifier iterator
Definition: RangeMap.h:130
std::string to_string(const V &value)
Definition: OMSAccess.h:77
const edm::EDGetTokenT< DTRecSegment4DCollection > theRecHits4DToken_
#define LogTrace(id)
C::const_iterator const_iterator
constant access iterator type
Definition: RangeMap.h:43
void Fill(HcalDetId &id, double val, std::vector< TH2F > &depth)
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
Transition
Definition: Transition.h:12
const edm::ESGetToken< DTGeometry, MuonGeometryRecord > dtGeomToken_
int get(int wheelId, int stationId, int sectorId, int slId, float &tTrig, float &tTrms, float &kFact, DTTimeUnits::type unit) const
get content
Definition: DTTtrig.cc:59
histos
Definition: combine.py:4
int wheel() const
Return the wheel number.
Definition: DTChamberId.h:39
HLT enums.
int sector() const
Definition: DTChamberId.h:49
const edm::ESGetToken< DTTtrig, DTTtrigRcd > ttrigToken_
const DTChamber * chamber(const DTChamberId &id) const
Return a DTChamber given its id.
Definition: DTGeometry.cc:90
DTTTrigOffsetCalibration(const edm::ParameterSet &pset)
Definition: event.py:1
Definition: Run.h:45