CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DTResidualCalibration.cc
Go to the documentation of this file.
1 
2 /*
3  * See header file for a description of this class.
4  *
5  * $Date: 2012/02/02 13:30:02 $
6  * $Revision: 1.4 $
7  */
8 
10 
11 // Framework
17 
18 //Geometry
21 
22 //RecHit
25 
29 
30 #include "TFile.h"
31 #include "TH1F.h"
32 #include "TH2F.h"
33 
34 #include <algorithm>
35 
37  select_(pset),
38  segment4DLabel_(pset.getParameter<edm::InputTag>("segment4DLabel")),
39  rootBaseDir_(pset.getUntrackedParameter<std::string>("rootBaseDir","DT/Residuals")),
40  detailedAnalysis_(pset.getUntrackedParameter<bool>("detailedAnalysis",false)) {
41 
42  edm::LogVerbatim("Calibration") << "[DTResidualCalibration] Constructor called.";
43 
44  std::string rootFileName = pset.getUntrackedParameter<std::string>("rootFileName","residuals.root");
45  rootFile_ = new TFile(rootFileName.c_str(), "RECREATE");
46  rootFile_->cd();
47 }
48 
50  edm::LogVerbatim("Calibration") << "[DTResidualCalibration] Destructor called.";
51 }
52 
54  TH1::SetDefaultSumw2(true);
55 }
56 
58 
59  // get the geometry
61  setup.get<MuonGeometryRecord>().get(dtGeomH);
62  dtGeom_ = dtGeomH.product();
63 
64  // Loop over all the chambers
65  if(histoMapTH1F_.size() == 0) {
66  std::vector<DTChamber*>::const_iterator ch_it = dtGeom_->chambers().begin();
67  std::vector<DTChamber*>::const_iterator ch_end = dtGeom_->chambers().end();
68  for (; ch_it != ch_end; ++ch_it) {
69  std::vector<const DTSuperLayer*>::const_iterator sl_it = (*ch_it)->superLayers().begin();
70  std::vector<const DTSuperLayer*>::const_iterator sl_end = (*ch_it)->superLayers().end();
71  // Loop over the SLs
72  for(; sl_it != sl_end; ++sl_it) {
73  DTSuperLayerId slId = (*sl_it)->id();
74  bookHistos(slId);
75  if(detailedAnalysis_) {
76  std::vector<const DTLayer*>::const_iterator layer_it = (*sl_it)->layers().begin();
77  std::vector<const DTLayer*>::const_iterator layer_end = (*sl_it)->layers().end();
78  for(; layer_it != layer_end; ++layer_it) {
79  DTLayerId layerId = (*layer_it)->id();
80  bookHistos(layerId);
81  }
82  }
83  }
84  }
85  }
86 }
87 
89  rootFile_->cd();
90 
91  // Get the 4D rechits from the event
93  event.getByLabel(segment4DLabel_, segment4Ds);
94 
95  // Loop over segments by chamber
97  for(chamberIdIt = segment4Ds->id_begin(); chamberIdIt != segment4Ds->id_end(); ++chamberIdIt){
98 
99  const DTChamber* chamber = dtGeom_->chamber(*chamberIdIt);
100 
101  // Get the range for the corresponding ChamberId
102  DTRecSegment4DCollection::range range = segment4Ds->get((*chamberIdIt));
103  // Loop over the rechits of this DetUnit
104  for(DTRecSegment4DCollection::const_iterator segment = range.first;
105  segment != range.second; ++segment){
106 
107  LogTrace("Calibration") << "Segment local pos (in chamber RF): " << (*segment).localPosition()
108  << "\nSegment global pos: " << chamber->toGlobal((*segment).localPosition());
109 
110  if( !select_(*segment, event, setup) ) continue;
111 
112  // Get all 1D RecHits at step 3 within the 4D segment
113  std::vector<DTRecHit1D> recHits1D_S3;
114 
115  if( (*segment).hasPhi() ){
116  const DTChamberRecSegment2D* phiSeg = (*segment).phiSegment();
117  const std::vector<DTRecHit1D>& phiRecHits = phiSeg->specificRecHits();
118  std::copy(phiRecHits.begin(), phiRecHits.end(), back_inserter(recHits1D_S3));
119  }
120 
121  if( (*segment).hasZed() ){
122  const DTSLRecSegment2D* zSeg = (*segment).zSegment();
123  const std::vector<DTRecHit1D>& zRecHits = zSeg->specificRecHits();
124  std::copy(zRecHits.begin(), zRecHits.end(), back_inserter(recHits1D_S3));
125  }
126 
127  // Loop over 1D RecHit inside 4D segment
128  for(std::vector<DTRecHit1D>::const_iterator recHit1D = recHits1D_S3.begin();
129  recHit1D != recHits1D_S3.end(); ++recHit1D) {
130  const DTWireId wireId = recHit1D->wireId();
131 
132  float segmDistance = segmentToWireDistance(*recHit1D,*segment);
133  if(segmDistance > 2.1) LogTrace("Calibration") << "WARNING: segment-wire distance: " << segmDistance;
134  else LogTrace("Calibration") << "segment-wire distance: " << segmDistance;
135 
136  float residualOnDistance = DTRecHitSegmentResidual().compute(dtGeom_,*recHit1D,*segment);
137  LogTrace("Calibration") << "Wire Id " << wireId << " residual on distance: " << residualOnDistance;
138 
139  fillHistos(wireId.superlayerId(), segmDistance, residualOnDistance);
140  if(detailedAnalysis_) fillHistos(wireId.layerId(), segmDistance, residualOnDistance);
141  }
142  }
143  }
144 
145 }
146 
148 
149  // Get the layer and the wire position
150  const DTWireId wireId = recHit1D.wireId();
151  const DTLayer* layer = dtGeom_->layer(wireId);
152  float wireX = layer->specificTopology().wirePosition(wireId.wire());
153 
154  // Extrapolate the segment to the z of the wire
155  // Get wire position in chamber RF
156  // (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)
157  LocalPoint wirePosInLay(wireX,recHit1D.localPosition().y(),recHit1D.localPosition().z());
158  GlobalPoint wirePosGlob = layer->toGlobal(wirePosInLay);
159  const DTChamber* chamber = dtGeom_->chamber(wireId.layerId().chamberId());
160  LocalPoint wirePosInChamber = chamber->toLocal(wirePosGlob);
161 
162  // Segment position at Wire z in chamber local frame
163  LocalPoint segPosAtZWire = segment.localPosition() + segment.localDirection()*wirePosInChamber.z()/cos(segment.localDirection().theta());
164 
165  // Compute the distance of the segment from the wire
166  int sl = wireId.superlayer();
167  float segmDistance = -1;
168  if(sl == 1 || sl == 3) segmDistance = fabs(wirePosInChamber.x() - segPosAtZWire.x());
169  else if(sl == 2) segmDistance = fabs(segPosAtZWire.y() - wirePosInChamber.y());
170 
171  return segmDistance;
172 }
173 
175 
176  edm::LogVerbatim("Calibration") << "[DTResidualCalibration] Writing histos to file.";
177  rootFile_->cd();
178  rootFile_->Write();
179  rootFile_->Close();
180 
181  /*std::map<DTSuperLayerId, std::vector<TH1F*> >::const_iterator itSlHistos = histoMapTH1F_.begin();
182  std::map<DTSuperLayerId, std::vector<TH1F*> >::const_iterator itSlHistos_end = histoMapTH1F_.end();
183  for(; itSlHistos != itSlHistos_end; ++itSlHistos){
184  std::vector<TH1F*>::const_iterator itHistTH1F = (*itSlHistos).second.begin();
185  std::vector<TH1F*>::const_iterator itHistTH1F_end = (*itSlHistos).second.end();
186  for(; itHistTH1F != itHistTH1F_end; ++itHistTH1F) (*itHistTH1F)->Write();
187 
188  std::vector<TH2F*>::const_iterator itHistTH2F = histoMapTH2F_[(*itSlHistos).first].begin();
189  std::vector<TH2F*>::const_iterator itHistTH2F_end = histoMapTH2F_[(*itSlHistos).first].end();
190  for(; itHistTH2F != itHistTH2F_end; ++itHistTH2F) (*itHistTH2F)->Write();
191  }*/
192 
193 }
194 
196  TH1AddDirectorySentry addDir;
197  rootFile_->cd();
198 
199  edm::LogVerbatim("Calibration") << "[DTResidualCalibration] Booking histos for SL: " << slId;
200 
201  // Compose the chamber name
202  std::stringstream wheelStr; wheelStr << slId.wheel();
203  std::stringstream stationStr; stationStr << slId.station();
204  std::stringstream sectorStr; sectorStr << slId.sector();
205  std::stringstream superLayerStr; superLayerStr << slId.superlayer();
206  // Define the step
207  int step = 3;
208  std::stringstream stepStr; stepStr << step;
209 
210  std::string slHistoName =
211  "_STEP" + stepStr.str() +
212  "_W" + wheelStr.str() +
213  "_St" + stationStr.str() +
214  "_Sec" + sectorStr.str() +
215  "_SL" + superLayerStr.str();
216 
217  edm::LogVerbatim("Calibration") << "Accessing " << rootBaseDir_;
218  TDirectory* baseDir = rootFile_->GetDirectory(rootBaseDir_.c_str());
219  if(!baseDir) baseDir = rootFile_->mkdir(rootBaseDir_.c_str());
220  edm::LogVerbatim("Calibration") << "Accessing " << ("Wheel" + wheelStr.str());
221  TDirectory* wheelDir = baseDir->GetDirectory(("Wheel" + wheelStr.str()).c_str());
222  if(!wheelDir) wheelDir = baseDir->mkdir(("Wheel" + wheelStr.str()).c_str());
223  edm::LogVerbatim("Calibration") << "Accessing " << ("Station" + stationStr.str());
224  TDirectory* stationDir = wheelDir->GetDirectory(("Station" + stationStr.str()).c_str());
225  if(!stationDir) stationDir = wheelDir->mkdir(("Station" + stationStr.str()).c_str());
226  edm::LogVerbatim("Calibration") << "Accessing " << ("Sector" + sectorStr.str());
227  TDirectory* sectorDir = stationDir->GetDirectory(("Sector" + sectorStr.str()).c_str());
228  if(!sectorDir) sectorDir = stationDir->mkdir(("Sector" + sectorStr.str()).c_str());
229 
230  /*std::string dirName = rootBaseDir_ + "/Wheel" + wheelStr.str() +
231  "/Station" + stationStr.str() +
232  "/Sector" + sectorStr.str();
233 
234  TDirectory* dir = rootFile_->GetDirectory(dirName.c_str());
235  if(!dir) dir = rootFile_->mkdir(dirName.c_str());
236  dir->cd();*/
237  sectorDir->cd();
238  // Create the monitor elements
239  std::vector<TH1F*> histosTH1F;
240  histosTH1F.push_back(new TH1F(("hResDist"+slHistoName).c_str(),
241  "Residuals on the distance from wire (rec_hit - segm_extr) (cm)",
242  200, -0.4, 0.4));
243  std::vector<TH2F*> histosTH2F;
244  histosTH2F.push_back(new TH2F(("hResDistVsDist"+slHistoName).c_str(),
245  "Residuals on the dist. (cm) from wire (rec_hit - segm_extr) vs dist. (cm)",
246  100, 0, 2.5, 200, -0.4, 0.4));
247  histoMapTH1F_[slId] = histosTH1F;
248  histoMapTH2F_[slId] = histosTH2F;
249 }
250 
252  TH1AddDirectorySentry addDir;
253  rootFile_->cd();
254 
255  edm::LogVerbatim("Calibration") << "[DTResidualCalibration] Booking histos for layer: " << layerId;
256 
257  // Compose the chamber name
258  std::stringstream wheelStr; wheelStr << layerId.wheel();
259  std::stringstream stationStr; stationStr << layerId.station();
260  std::stringstream sectorStr; sectorStr << layerId.sector();
261  std::stringstream superLayerStr; superLayerStr << layerId.superlayer();
262  std::stringstream layerStr; layerStr << layerId.layer();
263  // Define the step
264  int step = 3;
265  std::stringstream stepStr; stepStr << step;
266 
267  std::string layerHistoName =
268  "_STEP" + stepStr.str() +
269  "_W" + wheelStr.str() +
270  "_St" + stationStr.str() +
271  "_Sec" + sectorStr.str() +
272  "_SL" + superLayerStr.str() +
273  "_Layer" + layerStr.str();
274 
275  edm::LogVerbatim("Calibration") << "Accessing " << rootBaseDir_;
276  TDirectory* baseDir = rootFile_->GetDirectory(rootBaseDir_.c_str());
277  if(!baseDir) baseDir = rootFile_->mkdir(rootBaseDir_.c_str());
278  edm::LogVerbatim("Calibration") << "Accessing " << ("Wheel" + wheelStr.str());
279  TDirectory* wheelDir = baseDir->GetDirectory(("Wheel" + wheelStr.str()).c_str());
280  if(!wheelDir) wheelDir = baseDir->mkdir(("Wheel" + wheelStr.str()).c_str());
281  edm::LogVerbatim("Calibration") << "Accessing " << ("Station" + stationStr.str());
282  TDirectory* stationDir = wheelDir->GetDirectory(("Station" + stationStr.str()).c_str());
283  if(!stationDir) stationDir = wheelDir->mkdir(("Station" + stationStr.str()).c_str());
284  edm::LogVerbatim("Calibration") << "Accessing " << ("Sector" + sectorStr.str());
285  TDirectory* sectorDir = stationDir->GetDirectory(("Sector" + sectorStr.str()).c_str());
286  if(!sectorDir) sectorDir = stationDir->mkdir(("Sector" + sectorStr.str()).c_str());
287  edm::LogVerbatim("Calibration") << "Accessing " << ("SL" + superLayerStr.str());
288  TDirectory* superLayerDir = sectorDir->GetDirectory(("SL" + superLayerStr.str()).c_str());
289  if(!superLayerDir) superLayerDir = sectorDir->mkdir(("SL" + superLayerStr.str()).c_str());
290 
291  superLayerDir->cd();
292  // Create histograms
293  std::vector<TH1F*> histosTH1F;
294  histosTH1F.push_back(new TH1F(("hResDist"+layerHistoName).c_str(),
295  "Residuals on the distance from wire (rec_hit - segm_extr) (cm)",
296  200, -0.4, 0.4));
297  std::vector<TH2F*> histosTH2F;
298  histosTH2F.push_back(new TH2F(("hResDistVsDist"+layerHistoName).c_str(),
299  "Residuals on the dist. (cm) from wire (rec_hit - segm_extr) vs dist. (cm)",
300  100, 0, 2.5, 200, -0.4, 0.4));
301  histoMapPerLayerTH1F_[layerId] = histosTH1F;
302  histoMapPerLayerTH2F_[layerId] = histosTH2F;
303 }
304 
305 // Fill a set of histograms for a given SL
307  float distance,
308  float residualOnDistance) {
309  std::vector<TH1F*> const& histosTH1F = histoMapTH1F_[slId];
310  std::vector<TH2F*> const& histosTH2F = histoMapTH2F_[slId];
311  histosTH1F[0]->Fill(residualOnDistance);
312  histosTH2F[0]->Fill(distance, residualOnDistance);
313 }
314 
315 // Fill a set of histograms for a given layer
317  float distance,
318  float residualOnDistance) {
319  std::vector<TH1F*> const& histosTH1F = histoMapPerLayerTH1F_[layerId];
320  std::vector<TH2F*> const& histosTH2F = histoMapPerLayerTH2F_[layerId];
321  histosTH1F[0]->Fill(residualOnDistance);
322  histosTH2F[0]->Fill(distance, residualOnDistance);
323 }
324 
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:88
std::pair< const_iterator, const_iterator > range
iterator range
Definition: RangeMap.h:52
void fillHistos(DTSuperLayerId slId, float distance, float residualOnDistance)
void bookHistos(DTSuperLayerId slId)
std::map< DTLayerId, std::vector< TH1F * > > histoMapPerLayerTH1F_
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:47
virtual ~DTResidualCalibration()
Destructor.
T y() const
Definition: PV3DBase.h:63
LocalPoint toLocal(const GlobalPoint &gp) const
Conversion to the R.F. of the GeomDet.
Definition: GeomDet.h:62
identifier iterator
Definition: RangeMap.h:138
int layer() const
Return the layer number.
Definition: DTLayerId.h:55
DTSuperLayerId superlayerId() const
Return the corresponding SuperLayerId.
Definition: DTLayerId.h:61
Geom::Theta< T > theta() const
Definition: PV3DBase.h:75
void analyze(const edm::Event &event, const edm::EventSetup &setup)
const DTLayer * layer(DTLayerId id) const
Return a layer given its id.
Definition: DTGeometry.cc:112
virtual LocalVector localDirection() const
Local direction in Chamber frame.
void beginRun(const edm::Run &, const edm::EventSetup &)
C::const_iterator const_iterator
constant access iterator type
Definition: RangeMap.h:45
const DTTopology & specificTopology() const
Definition: DTLayer.cc:44
std::map< DTSuperLayerId, std::vector< TH1F * > > histoMapTH1F_
std::map< DTLayerId, std::vector< TH2F * > > histoMapPerLayerTH2F_
T z() const
Definition: PV3DBase.h:64
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
std::map< DTSuperLayerId, std::vector< TH2F * > > histoMapTH2F_
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
virtual LocalPoint localPosition() const
Local position in Chamber frame.
#define LogTrace(id)
std::vector< DTRecHit1D > specificRecHits() const
Access to specific components.
const std::vector< DTChamber * > & chambers() const
Return a vector of all Chamber.
Definition: DTGeometry.cc:87
virtual LocalPoint localPosition() const
Return the 3-dimensional local position.
Definition: DTRecHit1D.h:62
int wire() const
Return the wire number.
Definition: DTWireId.h:58
int superlayer() const
Return the superlayer number (deprecated method name)
float compute(const DTGeometry *, const DTRecHit1D &, const DTRecSegment4D &)
const T & get() const
Definition: EventSetup.h:55
T const * product() const
Definition: ESHandle.h:62
const DTChamber * chamber(DTChamberId id) const
Return a DTChamber given its id.
Definition: DTGeometry.cc:102
const DTGeometry * dtGeom_
DTLayerId layerId() const
Return the corresponding LayerId.
Definition: DTWireId.h:64
int sector() const
Definition: DTChamberId.h:63
float segmentToWireDistance(const DTRecHit1D &recHit1D, const DTRecSegment4D &segment)
int station() const
Return the station number.
Definition: DTChamberId.h:53
int wheel() const
Return the wheel number.
Definition: DTChamberId.h:47
T x() const
Definition: PV3DBase.h:62
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")
Definition: Run.h:36
DTWireId wireId() const
Return the wireId.
Definition: DTRecHit1D.h:109