CMS 3D CMS Logo

DTResidualCalibration.cc
Go to the documentation of this file.
1 
2 /*
3  * See header file for a description of this class.
4  *
5  */
6 
8 
9 // Framework
15 
16 //Geometry
19 
20 //RecHit
23 
27 
28 #include "TFile.h"
29 #include "TH1F.h"
30 #include "TH2F.h"
31 
32 #include <algorithm>
33 
35  : histRange_(pset.getParameter<double>("histogramRange")),
36  segment4DLabel_(pset.getParameter<edm::InputTag>("segment4DLabel")),
37  rootBaseDir_(pset.getUntrackedParameter<std::string>("rootBaseDir", "DT/Residuals")),
38  detailedAnalysis_(pset.getUntrackedParameter<bool>("detailedAnalysis", false)) {
40  select_ = new DTSegmentSelector(pset, collector);
41 
42  LogDebug("Calibration") << "[DTResidualCalibration] Constructor called.";
43  consumes<DTRecSegment4DCollection>(edm::InputTag(segment4DLabel_));
44  std::string rootFileName = pset.getUntrackedParameter<std::string>("rootFileName", "residuals.root");
45  rootFile_ = new TFile(rootFileName.c_str(), "RECREATE");
46  rootFile_->cd();
47 
48  segmok = 0;
49  segmbad = 0;
50  nevent = 0;
51 }
52 
54  delete select_;
55  edm::LogVerbatim("Calibration") << "[DTResidualCalibration] Destructor called.";
56  edm::LogVerbatim("Calibration") << "[DTResidualCalibration] Analyzed events: " << nevent;
57  edm::LogVerbatim("Calibration") << "[DTResidualCalibration] Good segments: " << segmok;
58  edm::LogVerbatim("Calibration") << "[DTResidualCalibration] Bad segments: " << segmbad;
59 }
60 
61 void DTResidualCalibration::beginJob() { TH1::SetDefaultSumw2(true); }
62 
64  // get the geometry
66  setup.get<MuonGeometryRecord>().get(dtGeomH);
67  dtGeom_ = dtGeomH.product();
68 
69  // Loop over all the chambers
70  if (histoMapTH1F_.empty()) {
71  for (auto ch_it : dtGeom_->chambers()) {
72  // Loop over the SLs
73  for (auto sl_it : ch_it->superLayers()) {
74  DTSuperLayerId slId = (sl_it)->id();
75  bookHistos(slId);
76  if (detailedAnalysis_) {
77  for (auto layer_it : (sl_it)->layers()) {
78  DTLayerId layerId = (layer_it)->id();
79  bookHistos(layerId);
80  }
81  }
82  }
83  }
84  }
85 }
86 
88  rootFile_->cd();
89  ++nevent;
90 
91  // Get the 4D rechits from the event
93  event.getByLabel(segment4DLabel_, segments4D);
94 
95  // Loop over segments by chamber
97  for (chamberIdIt = segments4D->id_begin(); chamberIdIt != segments4D->id_end(); ++chamberIdIt) {
98  const DTChamber* chamber = dtGeom_->chamber(*chamberIdIt);
99 
100  // Get the range for the corresponding ChamberId
101  DTRecSegment4DCollection::range range = segments4D->get((*chamberIdIt));
102  // Loop over the rechits of this DetUnit
103  for (DTRecSegment4DCollection::const_iterator segment = range.first; segment != range.second; ++segment) {
104  LogTrace("Calibration") << "Segment local pos (in chamber RF): " << (*segment).localPosition()
105  << "\nSegment global pos: " << chamber->toGlobal((*segment).localPosition());
106 
107  if (!(*select_)(*segment, event, setup)) {
108  segmbad++;
109  continue;
110  }
111  segmok++;
112 
113  // Get all 1D RecHits at step 3 within the 4D segment
114  std::vector<DTRecHit1D> recHits1D_S3;
115 
116  if ((*segment).hasPhi()) {
117  const DTChamberRecSegment2D* phiSeg = (*segment).phiSegment();
118  const std::vector<DTRecHit1D>& phiRecHits = phiSeg->specificRecHits();
119  std::copy(phiRecHits.begin(), phiRecHits.end(), back_inserter(recHits1D_S3));
120  }
121 
122  if ((*segment).hasZed()) {
123  const DTSLRecSegment2D* zSeg = (*segment).zSegment();
124  const std::vector<DTRecHit1D>& zRecHits = zSeg->specificRecHits();
125  std::copy(zRecHits.begin(), zRecHits.end(), back_inserter(recHits1D_S3));
126  }
127 
128  // Loop over 1D RecHit inside 4D segment
129  for (std::vector<DTRecHit1D>::const_iterator recHit1D = recHits1D_S3.begin(); recHit1D != recHits1D_S3.end();
130  ++recHit1D) {
131  const DTWireId wireId = recHit1D->wireId();
132 
133  float segmDistance = segmentToWireDistance(*recHit1D, *segment);
134  if (segmDistance > 2.1)
135  LogTrace("Calibration") << "WARNING: segment-wire distance: " << segmDistance;
136  else
137  LogTrace("Calibration") << "segment-wire distance: " << segmDistance;
138 
139  float residualOnDistance = DTRecHitSegmentResidual().compute(dtGeom_, *recHit1D, *segment);
140  LogTrace("Calibration") << "Wire Id " << wireId << " residual on distance: " << residualOnDistance;
141 
142  fillHistos(wireId.superlayerId(), segmDistance, residualOnDistance);
143  if (detailedAnalysis_)
144  fillHistos(wireId.layerId(), segmDistance, residualOnDistance);
145  }
146  }
147  }
148 }
149 
151  // Get the layer and the wire position
152  const DTWireId wireId = recHit1D.wireId();
153  const DTLayer* layer = dtGeom_->layer(wireId);
154  float wireX = layer->specificTopology().wirePosition(wireId.wire());
155 
156  // Extrapolate the segment to the z of the wire
157  // Get wire position in chamber RF
158  // (y and z must be those of the hit to be coherent in the transf. of RF in case of rotations of the layer alignment)
159  LocalPoint wirePosInLay(wireX, recHit1D.localPosition().y(), recHit1D.localPosition().z());
160  GlobalPoint wirePosGlob = layer->toGlobal(wirePosInLay);
161  const DTChamber* chamber = dtGeom_->chamber(wireId.layerId().chamberId());
162  LocalPoint wirePosInChamber = chamber->toLocal(wirePosGlob);
163 
164  // Segment position at Wire z in chamber local frame
165  LocalPoint segPosAtZWire =
166  segment.localPosition() + segment.localDirection() * wirePosInChamber.z() / cos(segment.localDirection().theta());
167 
168  // Compute the distance of the segment from the wire
169  int sl = wireId.superlayer();
170  float segmDistance = -1;
171  if (sl == 1 || sl == 3)
172  segmDistance = fabs(wirePosInChamber.x() - segPosAtZWire.x());
173  else if (sl == 2)
174  segmDistance = fabs(segPosAtZWire.y() - wirePosInChamber.y());
175 
176  return segmDistance;
177 }
178 
180  LogDebug("Calibration") << "[DTResidualCalibration] Writing histos to file.";
181  rootFile_->cd();
182  rootFile_->Write();
183  rootFile_->Close();
184 
185  /*std::map<DTSuperLayerId, TH1F* >::const_iterator itSlHistos = histoMapTH1F_.begin();
186  std::map<DTSuperLayerId, TH1F* >::const_iterator itSlHistos_end = histoMapTH1F_.end();
187  for(; itSlHistos != itSlHistos_end; ++itSlHistos){
188  std::vector<TH1F*>::const_iterator itHistTH1F = (*itSlHistos).second.begin();
189  std::vector<TH1F*>::const_iterator itHistTH1F_end = (*itSlHistos).second.end();
190  for(; itHistTH1F != itHistTH1F_end; ++itHistTH1F) (*itHistTH1F)->Write();
191 
192  std::vector<TH2F*>::const_iterator itHistTH2F = histoMapTH2F_[(*itSlHistos).first].begin();
193  std::vector<TH2F*>::const_iterator itHistTH2F_end = histoMapTH2F_[(*itSlHistos).first].end();
194  for(; itHistTH2F != itHistTH2F_end; ++itHistTH2F) (*itHistTH2F)->Write();
195  }*/
196 }
197 
199  TH1AddDirectorySentry addDir;
200  rootFile_->cd();
201 
202  LogDebug("Calibration") << "[DTResidualCalibration] Booking histos for SL: " << slId;
203 
204  // Compose the chamber name
205  // Define the step
206  int step = 3;
207 
208  std::string wheelStr = std::to_string(slId.wheel());
209  std::string stationStr = std::to_string(slId.station());
210  std::string sectorStr = std::to_string(slId.sector());
211 
212  std::string slHistoName = "_STEP" + std::to_string(step) + "_W" + wheelStr + "_St" + stationStr + "_Sec" + sectorStr +
213  "_SL" + std::to_string(slId.superlayer());
214 
215  LogDebug("Calibration") << "Accessing " << rootBaseDir_;
216  TDirectory* baseDir = rootFile_->GetDirectory(rootBaseDir_.c_str());
217  if (!baseDir)
218  baseDir = rootFile_->mkdir(rootBaseDir_.c_str());
219  LogDebug("Calibration") << "Accessing " << ("Wheel" + wheelStr);
220  TDirectory* wheelDir = baseDir->GetDirectory(("Wheel" + wheelStr).c_str());
221  if (!wheelDir)
222  wheelDir = baseDir->mkdir(("Wheel" + wheelStr).c_str());
223  LogDebug("Calibration") << "Accessing " << ("Station" + stationStr);
224  TDirectory* stationDir = wheelDir->GetDirectory(("Station" + stationStr).c_str());
225  if (!stationDir)
226  stationDir = wheelDir->mkdir(("Station" + stationStr).c_str());
227  LogDebug("Calibration") << "Accessing " << ("Sector" + sectorStr);
228  TDirectory* sectorDir = stationDir->GetDirectory(("Sector" + sectorStr).c_str());
229  if (!sectorDir)
230  sectorDir = stationDir->mkdir(("Sector" + sectorStr).c_str());
231 
232  sectorDir->cd();
233 
234  // Create the monitor elements
235  TH1F* histosTH1F = new TH1F(("hResDist" + slHistoName).c_str(),
236  "Residuals on the distance from wire (rec_hit - segm_extr) (cm)",
237  200,
238  -histRange_,
239  histRange_);
240  TH2F* histosTH2F = new TH2F(("hResDistVsDist" + slHistoName).c_str(),
241  "Residuals on the dist. (cm) from wire (rec_hit - segm_extr) vs dist. (cm)",
242  100,
243  0,
244  2.5,
245  200,
246  -histRange_,
247  histRange_);
248  histoMapTH1F_[slId] = histosTH1F;
249  histoMapTH2F_[slId] = histosTH2F;
250 }
251 
253  TH1AddDirectorySentry addDir;
254  rootFile_->cd();
255 
256  LogDebug("Calibration") << "[DTResidualCalibration] Booking histos for layer: " << layerId;
257 
258  // Compose the chamber name
259  std::string wheelStr = std::to_string(layerId.wheel());
260  std::string stationStr = std::to_string(layerId.station());
261  std::string sectorStr = std::to_string(layerId.sector());
262  std::string superLayerStr = std::to_string(layerId.superlayer());
263  std::string layerStr = std::to_string(layerId.layer());
264  // Define the step
265  int step = 3;
266 
267  std::string layerHistoName = "_STEP" + std::to_string(step) + "_W" + wheelStr + "_St" + stationStr + "_Sec" +
268  sectorStr + "_SL" + superLayerStr + "_Layer" + layerStr;
269 
270  LogDebug("Calibration") << "Accessing " << rootBaseDir_;
271  TDirectory* baseDir = rootFile_->GetDirectory(rootBaseDir_.c_str());
272  if (!baseDir)
273  baseDir = rootFile_->mkdir(rootBaseDir_.c_str());
274  LogDebug("Calibration") << "Accessing " << ("Wheel" + wheelStr);
275  TDirectory* wheelDir = baseDir->GetDirectory(("Wheel" + wheelStr).c_str());
276  if (!wheelDir)
277  wheelDir = baseDir->mkdir(("Wheel" + wheelStr).c_str());
278  LogDebug("Calibration") << "Accessing " << ("Station" + stationStr);
279  TDirectory* stationDir = wheelDir->GetDirectory(("Station" + stationStr).c_str());
280  if (!stationDir)
281  stationDir = wheelDir->mkdir(("Station" + stationStr).c_str());
282  LogDebug("Calibration") << "Accessing " << ("Sector" + sectorStr);
283  TDirectory* sectorDir = stationDir->GetDirectory(("Sector" + sectorStr).c_str());
284  if (!sectorDir)
285  sectorDir = stationDir->mkdir(("Sector" + sectorStr).c_str());
286  LogDebug("Calibration") << "Accessing " << ("SL" + superLayerStr);
287  TDirectory* superLayerDir = sectorDir->GetDirectory(("SL" + superLayerStr).c_str());
288  if (!superLayerDir)
289  superLayerDir = sectorDir->mkdir(("SL" + superLayerStr).c_str());
290 
291  superLayerDir->cd();
292  // Create histograms
293  TH1F* histosTH1F = new TH1F(("hResDist" + layerHistoName).c_str(),
294  "Residuals on the distance from wire (rec_hit - segm_extr) (cm)",
295  200,
296  -histRange_,
297  histRange_);
298  TH2F* histosTH2F = new TH2F(("hResDistVsDist" + layerHistoName).c_str(),
299  "Residuals on the dist. (cm) from wire (rec_hit - segm_extr) vs dist. (cm)",
300  100,
301  0,
302  2.5,
303  200,
304  -histRange_,
305  histRange_);
306  histoMapPerLayerTH1F_[layerId] = histosTH1F;
307  histoMapPerLayerTH2F_[layerId] = histosTH2F;
308 }
309 
310 // Fill a set of histograms for a given SL
311 void DTResidualCalibration::fillHistos(DTSuperLayerId slId, float distance, float residualOnDistance) {
312  histoMapTH1F_[slId]->Fill(residualOnDistance);
313  histoMapTH2F_[slId]->Fill(distance, residualOnDistance);
314 }
315 
316 // Fill a set of histograms for a given layer
317 void DTResidualCalibration::fillHistos(DTLayerId layerId, float distance, float residualOnDistance) {
318  histoMapPerLayerTH1F_[layerId]->Fill(residualOnDistance);
319  histoMapPerLayerTH2F_[layerId]->Fill(distance, residualOnDistance);
320 }
#define LogDebug(id)
LocalPoint localPosition() const override
Return the 3-dimensional local position.
Definition: DTRecHit1D.h:47
std::map< DTSuperLayerId, TH1F * > histoMapTH1F_
T getUntrackedParameter(std::string const &, T const &) const
float wirePosition(int wireNumber) const
Returns the x position in the layer of a given wire number.
Definition: DTTopology.cc:59
std::map< DTLayerId, TH2F * > histoMapPerLayerTH2F_
DTSegmentSelector * select_
const std::vector< const DTChamber * > & chambers() const
Return a vector of all Chamber.
Definition: DTGeometry.cc:84
LocalPoint localPosition() const override
Local position in Chamber frame.
std::pair< const_iterator, const_iterator > range
iterator range
Definition: RangeMap.h:50
void fillHistos(DTSuperLayerId slId, float distance, float residualOnDistance)
void bookHistos(DTSuperLayerId slId)
const DTChamber * chamber(const DTChamberId &id) const
Return a DTChamber given its id.
Definition: DTGeometry.cc:90
LocalVector localDirection() const override
Local direction in Chamber frame.
DTResidualCalibration(const edm::ParameterSet &pset)
Constructor.
DTChamberId chamberId() const
Return the corresponding ChamberId.
GlobalPoint toGlobal(const Local2DPoint &lp) const
Conversion to the global R.F. from the R.F. of the GeomDet.
Definition: GeomDet.h:49
T y() const
Definition: PV3DBase.h:60
LocalPoint toLocal(const GlobalPoint &gp) const
Conversion to the R.F. of the GeomDet.
Definition: GeomDet.h:58
identifier iterator
Definition: RangeMap.h:130
int layer() const
Return the layer number.
Definition: DTLayerId.h:42
DTSuperLayerId superlayerId() const
Return the corresponding SuperLayerId.
Definition: DTLayerId.h:45
Geom::Theta< T > theta() const
Definition: PV3DBase.h:72
C::const_iterator const_iterator
constant access iterator type
Definition: RangeMap.h:43
const DTTopology & specificTopology() const
Definition: DTLayer.cc:37
void beginRun(const edm::Run &, const edm::EventSetup &) override
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
T z() const
Definition: PV3DBase.h:61
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
#define LogTrace(id)
std::vector< DTRecHit1D > specificRecHits() const
Access to specific components.
~DTResidualCalibration() override
Destructor.
int wire() const
Return the wire number.
Definition: DTWireId.h:42
int superlayer() const
Return the superlayer number (deprecated method name)
std::map< DTSuperLayerId, TH2F * > histoMapTH2F_
void analyze(const edm::Event &event, const edm::EventSetup &setup) override
float compute(const DTGeometry *, const DTRecHit1D &, const DTRecSegment4D &)
const DTGeometry * dtGeom_
DTLayerId layerId() const
Return the corresponding LayerId.
Definition: DTWireId.h:45
HLT enums.
int sector() const
Definition: DTChamberId.h:49
T get() const
Definition: EventSetup.h:73
step
Definition: StallMonitor.cc:94
const DTLayer * layer(const DTLayerId &id) const
Return a layer given its id.
Definition: DTGeometry.cc:96
float segmentToWireDistance(const DTRecHit1D &recHit1D, const DTRecSegment4D &segment)
std::map< DTLayerId, TH1F * > histoMapPerLayerTH1F_
int station() const
Return the station number.
Definition: DTChamberId.h:42
int wheel() const
Return the wheel number.
Definition: DTChamberId.h:39
T x() const
Definition: PV3DBase.h:59
T const * product() const
Definition: ESHandle.h:86
Definition: event.py:1
Definition: Run.h:45
DTWireId wireId() const
Return the wireId.
Definition: DTRecHit1D.h:76