CMS 3D CMS Logo

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