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 
25 
28 
29 #include <string>
30 #include <sstream>
31 #include "TFile.h"
32 #include "TH1F.h"
33 
34 using namespace std;
35 using namespace edm;
36 
38  : theRecHits4DLabel_(pset.getParameter<InputTag>("recHits4DLabel")),
39  doTTrigCorrection_(pset.getUntrackedParameter<bool>("doT0SegCorrection", false)),
40  theCalibChamber_(pset.getUntrackedParameter<string>("calibChamber", "All")),
41  ttrigToken_(
42  esConsumes<edm::Transition::BeginRun>(edm::ESInputTag("", pset.getUntrackedParameter<string>("dbLabel")))),
43  dtGeomToken_(esConsumes()) {
44  LogVerbatim("Calibration") << "[DTTTrigOffsetCalibration] Constructor called!";
45 
47  select_ = new DTSegmentSelector(pset, collector);
48 
49  // the root file which will contain the histos
50  string rootFileName = pset.getUntrackedParameter<string>("rootFileName", "DTT0SegHistos.root");
51  rootFile_ = new TFile(rootFileName.c_str(), "RECREATE");
52  rootFile_->cd();
53 }
54 
56  if (doTTrigCorrection_) {
58  tTrig = setup.getHandle(ttrigToken_);
59  tTrigMap_ = &setup.getData(ttrigToken_);
60  LogVerbatim("Calibration") << "[DTTTrigOffsetCalibration]: TTrig version: " << tTrig->version() << endl;
61  }
62 }
63 
65  rootFile_->Close();
66  LogVerbatim("Calibration") << "[DTTTrigOffsetCalibration] Destructor called!";
67 }
68 
70  rootFile_->cd();
71  DTChamberId chosenChamberId;
72 
73  if (theCalibChamber_ != "All") {
74  stringstream linestr;
75  int selWheel, selStation, selSector;
76  linestr << theCalibChamber_;
77  linestr >> selWheel >> selStation >> selSector;
78  chosenChamberId = DTChamberId(selWheel, selStation, selSector);
79  LogVerbatim("Calibration") << " chosen chamber " << chosenChamberId << endl;
80  }
81 
82  // Get the DT Geometry
83  ESHandle<DTGeometry> dtGeom;
84  dtGeom = eventSetup.getHandle(dtGeomToken_);
85 
86  // Get the rechit collection from the event
88  event.getByLabel(theRecHits4DLabel_, all4DSegments);
89 
90  // Loop over segments by chamber
92  for (chamberIdIt = all4DSegments->id_begin(); chamberIdIt != all4DSegments->id_end(); ++chamberIdIt) {
93  // Get the chamber from the setup
94  const DTChamber* chamber = dtGeom->chamber(*chamberIdIt);
95  LogTrace("Calibration") << "Chamber Id: " << *chamberIdIt;
96 
97  // Book histos
98  if (theT0SegHistoMap_.find(*chamberIdIt) == theT0SegHistoMap_.end()) {
99  bookHistos(*chamberIdIt);
100  }
101 
102  // Calibrate just the chosen chamber/s
103  if ((theCalibChamber_ != "All") && ((*chamberIdIt) != chosenChamberId))
104  continue;
105 
106  // Get the range for the corresponding ChamberId
107  DTRecSegment4DCollection::range range = all4DSegments->get((*chamberIdIt));
108 
109  // Loop over the rechits of this DetUnit
110  for (DTRecSegment4DCollection::const_iterator segment = range.first; segment != range.second; ++segment) {
111  LogTrace("Calibration") << "Segment local pos (in chamber RF): " << (*segment).localPosition()
112  << "\nSegment global pos: " << chamber->toGlobal((*segment).localPosition());
113 
114  if (!(*select_)(*segment, event, eventSetup))
115  continue;
116 
117  // Fill t0-seg values
118  if ((*segment).hasPhi()) {
119  //if( segment->phiSegment()->ist0Valid() ){
120  if ((segment->phiSegment()->t0()) != 0.00) {
121  (theT0SegHistoMap_[*chamberIdIt])[0]->Fill(segment->phiSegment()->t0());
122  }
123  }
124  if ((*segment).hasZed()) {
125  //if( segment->zSegment()->ist0Valid() ){
126  if ((segment->zSegment()->t0()) != 0.00) {
127  (theT0SegHistoMap_[*chamberIdIt])[1]->Fill(segment->zSegment()->t0());
128  }
129  }
130  } // DTRecSegment4DCollection::const_iterator segment
131  } // DTRecSegment4DCollection::id_iterator chamberIdIt
132 } // DTTTrigOffsetCalibration::analyze
133 
135  rootFile_->cd();
136 
137  LogVerbatim("Calibration") << "[DTTTrigOffsetCalibration] Writing histos to file!" << endl;
138 
139  for (ChamberHistosMap::const_iterator itChHistos = theT0SegHistoMap_.begin(); itChHistos != theT0SegHistoMap_.end();
140  ++itChHistos) {
141  for (vector<TH1F*>::const_iterator itHist = (*itChHistos).second.begin(); itHist != (*itChHistos).second.end();
142  ++itHist)
143  (*itHist)->Write();
144  }
145 
146  if (doTTrigCorrection_) {
147  // Create the object to be written to DB
148  DTTtrig* tTrig = new DTTtrig();
149 
150  for (ChamberHistosMap::const_iterator itChHistos = theT0SegHistoMap_.begin(); itChHistos != theT0SegHistoMap_.end();
151  ++itChHistos) {
152  DTChamberId chId = itChHistos->first;
153  // Get SuperLayerId's for each ChamberId
154  vector<DTSuperLayerId> slIds;
155  slIds.push_back(DTSuperLayerId(chId, 1));
156  slIds.push_back(DTSuperLayerId(chId, 3));
157  if (chId.station() != 4)
158  slIds.push_back(DTSuperLayerId(chId, 2));
159 
160  for (vector<DTSuperLayerId>::const_iterator itSl = slIds.begin(); itSl != slIds.end(); ++itSl) {
161  // Get old values from DB
162  float ttrigMean = 0;
163  float ttrigSigma = 0;
164  float kFactor = 0;
165  tTrigMap_->get(*itSl, ttrigMean, ttrigSigma, kFactor, DTTimeUnits::ns);
166  //FIXME: verify if values make sense
167  // Set new values
168  float ttrigMeanNew = ttrigMean;
169  float ttrigSigmaNew = ttrigSigma;
170  float t0SegMean =
171  (itSl->superLayer() != 2) ? itChHistos->second[0]->GetMean() : itChHistos->second[1]->GetMean();
172 
173  float kFactorNew = (kFactor * ttrigSigma + t0SegMean) / ttrigSigma;
174 
175  tTrig->set(*itSl, ttrigMeanNew, ttrigSigmaNew, kFactorNew, DTTimeUnits::ns);
176  }
177  }
178  LogVerbatim("Calibration") << "[DTTTrigOffsetCalibration] Writing ttrig object to DB!" << endl;
179  // Write the object to DB
180  string tTrigRecord = "DTTtrigRcd";
181  DTCalibDBUtils::writeToDB(tTrigRecord, tTrig);
182  }
183 }
184 
185 // Book a set of histograms for a given Chamber
187  LogTrace("Calibration") << " Booking histos for Chamber: " << chId;
188 
189  // Compose the chamber name
192  std::string sector = std::to_string(chId.sector());
193 
194  string chHistoName = "_W" + wheel + "_St" + station + "_Sec" + sector;
195 
196  vector<TH1F*> histos;
197  // Note the order matters
198  histos.push_back(new TH1F(("hRPhiSegT0" + chHistoName).c_str(), "t0 from Phi segments", 500, -60., 60.));
199  if (chId.station() != 4)
200  histos.push_back(new TH1F(("hRZSegT0" + chHistoName).c_str(), "t0 from Z segments", 500, -60., 60.));
201 
202  theT0SegHistoMap_[chId] = histos;
203 }
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:71
#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