CMS 3D CMS Logo

MuonTrackResidualAnalyzer.cc
Go to the documentation of this file.
2 
3 // Collaborating Class Header
9 
11 
17 
21 
27 
28 #include "TFile.h"
29 #include "TH1F.h"
30 #include "TH2F.h"
31 #include "TDirectory.h"
32 
33 using namespace std;
34 using namespace edm;
35 
37 
39  pset = ps;
40  // service parameters
41  ParameterSet serviceParameters = pset.getParameter<ParameterSet>("ServiceParameters");
42  // the services
43  theService = new MuonServiceProxy(serviceParameters, consumesCollector());
44 
45  theMuonTrackLabel = pset.getParameter<InputTag>("MuonTrack");
46  theMuonTrackToken = consumes<reco::TrackCollection>(theMuonTrackLabel);
47 
48  cscSimHitLabel = pset.getParameter<InputTag>("CSCSimHit");
49  dtSimHitLabel = pset.getParameter<InputTag>("DTSimHit");
50  rpcSimHitLabel = pset.getParameter<InputTag>("RPCSimHit");
51  theCSCSimHitToken = consumes<std::vector<PSimHit> >(cscSimHitLabel);
52  theDTSimHitToken = consumes<std::vector<PSimHit> >(dtSimHitLabel);
53  theRPCSimHitToken = consumes<std::vector<PSimHit> >(rpcSimHitLabel);
54 
55  dbe_ = edm::Service<DQMStore>().operator->();
56  out = pset.getUntrackedParameter<string>("rootFileName");
57  dirName_ = pset.getUntrackedParameter<std::string>("dirName");
58  subsystemname_ = pset.getUntrackedParameter<std::string>("subSystemFolder", "YourSubsystem");
59 
60  // Sim or Real
61  theDataType = pset.getParameter<InputTag>("DataType");
62  if (theDataType.label() != "RealData" && theDataType.label() != "SimData")
63  LogDebug("MuonTrackResidualAnalyzer") << "Error in Data Type!!";
64  theDataTypeToken = consumes<edm::SimTrackContainer>(theDataType);
65 
66  theEtaRange = (EtaRange)pset.getParameter<int>("EtaRange");
67 
68  theUpdator = new KFUpdator();
69  theEstimator = new Chi2MeasurementEstimator(100000.);
70 
71  theMuonSimHitNumberPerEvent = 0;
72 }
73 
76  delete theUpdator;
77  delete theEstimator;
78  delete theService;
79 }
80 
82  edm::Run const &iRun,
83  edm::EventSetup const & /* iSetup */) {
84  LogDebug("MuonTrackResidualAnalyzer") << "Begin Run";
85 
86  ibooker.cd();
87  InputTag algo = theMuonTrackLabel;
88  string dirName = dirName_;
89  if (!algo.process().empty())
90  dirName += algo.process() + "_";
91  if (!algo.label().empty())
92  dirName += algo.label() + "_";
93  if (!algo.instance().empty())
94  dirName += algo.instance() + "";
95  if (dirName.find("Tracks") < dirName.length()) {
96  dirName.replace(dirName.find("Tracks"), 6, "");
97  }
98  std::replace(dirName.begin(), dirName.end(), ':', '_');
99  ibooker.setCurrentFolder(dirName);
100 
101  hDPtRef = ibooker.book1D("DeltaPtRef", "P^{in}_{t}-P^{in ref}", 10000, -20, 20);
102 
103  // Resolution wrt the 1D Rec Hits
104  // h1DRecHitRes = new HResolution1DRecHit("TotalRec");
105 
106  // Resolution wrt the 1d Sim Hits
107  // h1DSimHitRes = new HResolution1DRecHit("TotalSim");
108 
109  hSimHitsPerTrack = ibooker.book1D("SimHitsPerTrack", "Number of sim hits per track", 55, 0, 55);
110  hSimHitsPerTrackVsEta =
111  ibooker.book2D("SimHitsPerTrackVsEta", "Number of sim hits per track VS #eta", 120, -3., 3., 55, 0, 55);
112  hDeltaPtVsEtaSim =
113  ibooker.book2D("DeltaPtVsEtaSim", "#Delta P_{t} vs #eta gen, sim quantity", 120, -3., 3., 500, -250., 250.);
114  hDeltaPtVsEtaSim2 =
115  ibooker.book2D("DeltaPtVsEtaSim2", "#Delta P_{t} vs #eta gen, sim quantity", 120, -3., 3., 500, -250., 250.);
116 }
117 
119  if (!out.empty() && dbe_)
120  dbe_->save(out);
121 }
123  LogDebug("MuonTrackResidualAnalyzer") << "Analyze";
124 
125  // Update the services
126  theService->update(eventSetup);
128  theMuonSimHitNumberPerEvent = 0;
129 
130  // Get the SimHit collection from the event
131  Handle<PSimHitContainer> dtSimHits;
132  event.getByToken(theDTSimHitToken, dtSimHits);
133 
134  Handle<PSimHitContainer> cscSimHits;
135  event.getByToken(theCSCSimHitToken, cscSimHits);
136 
137  Handle<PSimHitContainer> rpcSimHits;
138  event.getByToken(theRPCSimHitToken, rpcSimHits);
139 
141 
142  // FIXME Add the tracker one??
143 
144  // Map simhits per DetId
145  map<DetId, const PSimHit *> muonSimHitsPerId = mapMuSimHitsPerId(dtSimHits, cscSimHits, rpcSimHits);
146 
147  hSimHitsPerTrack->Fill(theMuonSimHitNumberPerEvent);
148 
149  double etaSim = 0;
150 
151  if (theDataType.label() == "SimData") {
152  // Get the SimTrack collection from the event
153  event.getByToken(theDataTypeToken, simTracks);
154 
155  // Loop over the Sim tracks
156  SimTrackContainer::const_iterator simTrack;
157 
158  for (simTrack = simTracks->begin(); simTrack != simTracks->end(); ++simTrack)
159  if (abs((*simTrack).type()) == 13) {
160  hSimHitsPerTrackVsEta->Fill((*simTrack).momentum().eta(), theMuonSimHitNumberPerEvent);
161  etaSim = (*simTrack).momentum().eta();
162  theSimTkId = (*simTrack).trackId();
163  }
164  }
165 
166  // Get the RecTrack collection from the event
168  event.getByToken(theMuonTrackToken, muonTracks);
169 
170  reco::TrackCollection::const_iterator muonTrack;
171 
172  // Loop over the Rec tracks
173  for (muonTrack = muonTracks->begin(); muonTrack != muonTracks->end(); ++muonTrack) {
174  reco::TransientTrack track(*muonTrack, &*theService->magneticField(), theService->trackingGeometry());
175 
176  TrajectoryStateOnSurface outerTSOS = track.outermostMeasurementState();
177  TrajectoryStateOnSurface innerTSOS = track.innermostMeasurementState();
178 
180 
181  // SimHit Energy loss analysis
182  double momAtEntry = -150., momAtExit = -150.;
183 
184  if (theSimHitContainer.size() > 1) {
185  const GeomDet *geomDetA = theService->trackingGeometry()->idToDet(DetId(theSimHitContainer.front()->detUnitId()));
186  double distA = geomDetA->toGlobal(theSimHitContainer.front()->localPosition()).mag();
187 
188  const GeomDet *geomDetB = theService->trackingGeometry()->idToDet(DetId(theSimHitContainer.back()->detUnitId()));
189  double distB = geomDetB->toGlobal(theSimHitContainer.back()->localPosition()).mag();
190 
191  LogTrace("MuonTrackResidualAnalyzer")
192  << "Inner SimHit: " << theSimHitContainer.front()->particleType()
193  << " Pt: " << theSimHitContainer.front()->momentumAtEntry().perp()
194  << " E: " << theSimHitContainer.front()->momentumAtEntry().perp() << " R: " << distA << endl;
195  LogTrace("MuonTrackResidualAnalyzer")
196  << "Outer SimHit: " << theSimHitContainer.back()->particleType()
197  << " Pt: " << theSimHitContainer.back()->momentumAtEntry().perp()
198  << " E: " << theSimHitContainer.front()->momentumAtEntry().perp() << " R: " << distB << endl;
199 
200  momAtEntry = theSimHitContainer.front()->momentumAtEntry().perp();
201  momAtExit = theSimHitContainer.back()->momentumAtEntry().perp();
202  }
203 
204  trackingRecHit_iterator rhFirst = track.recHitsBegin();
205  trackingRecHit_iterator rhLast = track.recHitsEnd() - 1;
206  map<DetId, const PSimHit *>::const_iterator itFirst = muonSimHitsPerId.find((*rhFirst)->geographicalId());
207  map<DetId, const PSimHit *>::const_iterator itLast = muonSimHitsPerId.find((*rhLast)->geographicalId());
208 
209  double momAtEntry2 = -150, momAtExit2 = -150.;
210  if (itFirst != muonSimHitsPerId.end())
211  momAtEntry2 = itFirst->second->momentumAtEntry().perp();
212  else {
213  LogDebug("MuonTrackResidualAnalyzer") << "No first sim hit found";
214  ++rhFirst;
215  itFirst = muonSimHitsPerId.find((*rhFirst)->geographicalId());
216  if (itFirst != muonSimHitsPerId.end())
217  momAtEntry2 = itFirst->second->momentumAtEntry().perp();
218  else {
219  LogDebug("MuonTrackResidualAnalyzer") << "No second sim hit found";
220  // continue;
221  }
222  }
223 
224  if (itLast != muonSimHitsPerId.end())
225  momAtExit2 = itLast->second->momentumAtEntry().perp();
226  else {
227  LogDebug("MuonTrackResidualAnalyzer") << "No last sim hit found";
228  --rhLast;
229  itLast = muonSimHitsPerId.find((*rhLast)->geographicalId());
230  if (itLast != muonSimHitsPerId.end())
231  momAtExit2 = itLast->second->momentumAtEntry().perp();
232  else {
233  LogDebug("MuonTrackResidualAnalyzer") << "No last but one sim hit found";
234  // continue;
235  }
236  }
237 
238  if (etaSim) {
239  if (momAtEntry >= 0 && momAtExit >= 0)
240  hDeltaPtVsEtaSim->Fill(etaSim, momAtEntry - momAtExit);
241  if (momAtEntry2 >= 0 && momAtExit2 >= 0)
242  hDeltaPtVsEtaSim2->Fill(etaSim, momAtEntry2 - momAtExit2);
243  } else
244  LogDebug("MuonTrackResidualAnalyzer") << "NO SimTrack'eta";
245  //
246 
247  // computeResolution(trajectoryBW,muonSimHitsPerId,h1DSimHitRes);
248  // computeResolution(smoothed,muonSimHitsPerId,h1DSimHitRes);
249  }
250 }
251 
253  switch (theEtaRange) {
254  case all:
255  return (abs(eta) <= 2.4) ? true : false;
256  case barrel:
257  return (abs(eta) < 1.1) ? true : false;
258  case endcap:
259  return (abs(eta) >= 1.1 && abs(eta) <= 2.4) ? true : false;
260  default: {
261  LogDebug("MuonTrackResidualAnalyzer") << "No correct Eta range selected!! ";
262  return false;
263  }
264  }
265 }
266 
267 // map the muon simhits by id
269  Handle<PSimHitContainer> cscSimhits,
270  Handle<PSimHitContainer> rpcSimhits) {
272 
273  map<DetId, const PSimHit *> hitIdMap;
274  theSimHitContainer.clear();
275 
276  mapMuSimHitsPerId(dtSimhits, hitIdMap);
277  mapMuSimHitsPerId(cscSimhits, hitIdMap);
278  mapMuSimHitsPerId(rpcSimhits, hitIdMap);
279 
280  if (theSimHitContainer.size() > 1)
281  stable_sort(
282  theSimHitContainer.begin(), theSimHitContainer.end(), RadiusComparatorInOut(theService->trackingGeometry()));
283 
284  LogDebug("MuonTrackResidualAnalyzer") << "Sim Hit list";
285  int count = 1;
286  for (vector<const PSimHit *>::const_iterator it = theSimHitContainer.begin(); it != theSimHitContainer.end(); ++it) {
287  LogTrace("MuonTrackResidualAnalyzer")
288  << count << " "
289  << " Process Type: " << (*it)->processType() << " " << debug.dumpMuonId(DetId((*it)->detUnitId())) << endl;
290  }
291 
292  return hitIdMap;
293 }
294 
296  map<DetId, const PSimHit *> &hitIdMap) {
297  for (PSimHitContainer::const_iterator simhit = simhits->begin(); simhit != simhits->end(); ++simhit) {
298  if (abs(simhit->particleType()) != 13 && theSimTkId != simhit->trackId())
299  continue;
300 
301  theSimHitContainer.push_back(&*simhit);
302  DetId id = DetId(simhit->detUnitId());
303 
304  if (id.subdetId() == MuonSubdetId::DT) {
305  DTLayerId lId(id.rawId());
306  id = DetId(lId.rawId());
307  }
308 
309  map<DetId, const PSimHit *>::const_iterator it = hitIdMap.find(id);
310 
311  if (it == hitIdMap.end())
312  hitIdMap[id] = &*simhit;
313  else
314  LogDebug("MuonTrackResidualAnalyzer") << "TWO muons in the same sensible volume!!";
315 
316  ++theMuonSimHitNumberPerEvent;
317  }
318 }
319 
321  map<DetId, const PSimHit *> &hitIdMap,
324 
325  for (Trajectory::DataContainer::const_iterator datum = data.begin(); datum != data.end(); ++datum) {
326  GlobalPoint fitPoint = datum->updatedState().globalPosition();
327 
328  // FIXME!
329  // double errX = datum->updatedState().cartesianError().matrix()[0][0];
330  // double errY = datum->updatedState().cartesianError().matrix()[1][1];
331  // double errZ = datum->updatedState().cartesianError().matrix()[2][2];
332  //
333  double errX = datum->updatedState().localError().matrix()(3, 3);
334  double errY = datum->updatedState().localError().matrix()(4, 4);
335  double errZ = 1.;
336 
337  map<DetId, const PSimHit *>::const_iterator it = hitIdMap.find(datum->recHit()->geographicalId());
338 
339  if (it == hitIdMap.end())
340  continue; // FIXME! Put a counter
341 
342  const PSimHit *simhit = it->second;
343 
344  LocalPoint simHitPoint = simhit->localPosition();
345 
346  const GeomDet *geomDet = theService->trackingGeometry()->idToDet(DetId(simhit->detUnitId()));
347 
348  LocalPoint fitLocalPoint = geomDet->toLocal(fitPoint);
349 
350  LocalVector diff = fitLocalPoint - simHitPoint;
351 
352  cout << "SimHit position " << simHitPoint << endl;
353  cout << "Fit position " << fitLocalPoint << endl;
354  cout << "Fit position2 " << datum->updatedState().localPosition() << endl;
355  cout << "Errors on the fit position: (" << errX << "," << errY << "," << errZ << ")" << endl;
356  cout << "Resolution on x: " << diff.x() / abs(simHitPoint.x()) << endl;
357  cout << "Resolution on y: " << diff.y() / abs(simHitPoint.y()) << endl;
358  cout << "Resolution on z: " << diff.z() / abs(simHitPoint.z()) << endl;
359 
360  cout << "Eta direction: " << simhit->momentumAtEntry().eta() << " eta position: " << simHitPoint.eta() << endl;
361  cout << "Phi direction: " << simhit->momentumAtEntry().phi() << " phi position: " << simHitPoint.phi() << endl;
362 
363  histos->Fill(simHitPoint.x(),
364  simHitPoint.y(),
365  simHitPoint.z(),
366  diff.x(),
367  diff.y(),
368  diff.z(),
369  errX,
370  errY,
371  errZ,
372  simhit->momentumAtEntry().eta(),
373  simhit->momentumAtEntry().phi());
374  // simHitPoint.eta(), simHitPoint.phi() ); // FIXME!
375  }
376 }
~MuonTrackResidualAnalyzer() override
Destructor.
std::map< DetId, const PSimHit * > mapMuSimHitsPerId(edm::Handle< edm::PSimHitContainer > dtSimhits, edm::Handle< edm::PSimHitContainer > cscSimhits, edm::Handle< edm::PSimHitContainer > rpcSimhits)
void analyze(const edm::Event &event, const edm::EventSetup &eventSetup) override
def all(container)
workaround iterator generators for ROOT classes
Definition: cmstools.py:25
unsigned int detUnitId() const
Definition: PSimHit.h:97
virtual void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:36
LocalPoint toLocal(const GlobalPoint &gp) const
Conversion to the R.F. of the GeomDet.
Definition: GeomDet.h:58
Geom::Phi< T > phi() const
Definition: PV3DBase.h:66
T eta() const
Definition: PV3DBase.h:73
def replace(string, replacements)
#define LogTrace(id)
void dqmEndRun(edm::Run const &, edm::EventSetup const &) override
DataContainer const & measurements() const
Definition: Trajectory.h:178
std::vector< TrajectoryMeasurement > DataContainer
Definition: Trajectory.h:40
void computeResolution(Trajectory &trajectory, std::map< DetId, const PSimHit *> &hitIdMap, HResolution1DRecHit *histos)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
MuonTrackResidualAnalyzer(const edm::ParameterSet &ps)
Constructor.
GlobalPoint toGlobal(const Local2DPoint &lp) const
Conversion to the global R.F. from the R.F. of the GeomDet.
Definition: GeomDet.h:49
std::vector< ConstRecHitPointer > ConstRecHitContainer
Definition: DetId.h:17
void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
#define debug
Definition: HDRShower.cc:19
T1 phi() const
Definition: Phi.h:78
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
Local3DPoint localPosition() const
Definition: PSimHit.h:52
MonitorElement * book2D(TString const &name, TString const &title, int nchX, double lowX, double highX, int nchY, double lowY, double highY, FUNC onbooking=NOOP())
Definition: DQMStore.h:212
histos
Definition: combine.py:4
HLT enums.
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
static constexpr int DT
Definition: MuonSubdetId.h:11
MonitorElement * book1D(TString const &name, TString const &title, int const nchX, double const lowX, double const highX, FUNC onbooking=NOOP())
Definition: DQMStore.h:98
LocalVector momentumAtEntry() const
The momentum of the track that produced the hit, at entry point.
Definition: PSimHit.h:55
Definition: event.py:1
Definition: Run.h:45
#define LogDebug(id)