CMS 3D CMS Logo

MuonTrackResidualAnalyzer.cc
Go to the documentation of this file.
2 
3 // Collaborating Class Header
8 
10 
16 
20 
26 
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);
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 
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 }
74 
77  delete theUpdator;
78  delete theEstimator;
79  delete theService;
80 }
81 
82 // Operations
84 
85 }
86 
88  edm::Run const & iRun,
89  edm::EventSetup const & /* iSetup */)
90 {
91  LogDebug("MuonTrackResidualAnalyzer")<<"Begin Run";
92 
93  //ibooker.showDirStructure();
94 
95  ibooker.cd();
96  InputTag algo = theMuonTrackLabel;
97  string dirName=dirName_;
98  if (algo.process()!="")
99  dirName+=algo.process()+"_";
100  if(algo.label()!="")
101  dirName+=algo.label()+"_";
102  if(algo.instance()!="")
103  dirName+=algo.instance()+"";
104  if (dirName.find("Tracks")<dirName.length()){
105  dirName.replace(dirName.find("Tracks"),6,"");
106  }
107  std::replace(dirName.begin(), dirName.end(), ':', '_');
108  ibooker.setCurrentFolder(dirName.c_str());
109 
110 
111  hDPtRef = ibooker.book1D("DeltaPtRef","P^{in}_{t}-P^{in ref}",10000,-20,20);
112 
113  // Resolution wrt the 1D Rec Hits
114  // h1DRecHitRes = new HResolution1DRecHit("TotalRec");
115 
116  // Resolution wrt the 1d Sim Hits
117  // h1DSimHitRes = new HResolution1DRecHit("TotalSim");
118 
119  hSimHitsPerTrack = ibooker.book1D("SimHitsPerTrack","Number of sim hits per track",55,0,55);
120  hSimHitsPerTrackVsEta = ibooker.book2D("SimHitsPerTrackVsEta","Number of sim hits per track VS #eta",120,-3.,3.,55,0,55);
121  hDeltaPtVsEtaSim = ibooker.book2D("DeltaPtVsEtaSim","#Delta P_{t} vs #eta gen, sim quantity",120,-3.,3.,500,-250.,250.);
122  hDeltaPtVsEtaSim2 = ibooker.book2D("DeltaPtVsEtaSim2","#Delta P_{t} vs #eta gen, sim quantity",120,-3.,3.,500,-250.,250.);
123 }
124 
126  if ( out.size() != 0 && dbe_ ) dbe_->save(out);
127 }
129  LogDebug("MuonTrackResidualAnalyzer")<<"Analyze";
130 
131  // Update the services
132  theService->update(eventSetup);
134  theMuonSimHitNumberPerEvent = 0;
135 
136  // Get the SimHit collection from the event
137  Handle<PSimHitContainer> dtSimHits;
138  event.getByToken(theDTSimHitToken, dtSimHits);
139 
140  Handle<PSimHitContainer> cscSimHits;
141  event.getByToken(theCSCSimHitToken, cscSimHits);
142 
143  Handle<PSimHitContainer> rpcSimHits;
144  event.getByToken(theRPCSimHitToken, rpcSimHits);
145 
147 
148  // FIXME Add the tracker one??
149 
150  // Map simhits per DetId
151  map<DetId, const PSimHit* > muonSimHitsPerId =
152  mapMuSimHitsPerId(dtSimHits,cscSimHits,rpcSimHits);
153 
154  hSimHitsPerTrack->Fill(theMuonSimHitNumberPerEvent);
155 
156  double etaSim=0;
157 
158  if(theDataType.label() == "SimData"){
159 
160  // Get the SimTrack collection from the event
161  event.getByToken(theDataTypeToken,simTracks);
162 
163  // Loop over the Sim tracks
164  SimTrackContainer::const_iterator simTrack;
165 
166  for (simTrack = simTracks->begin(); simTrack != simTracks->end(); ++simTrack)
167  if (abs((*simTrack).type()) == 13){
168  hSimHitsPerTrackVsEta->Fill((*simTrack).momentum().eta(),theMuonSimHitNumberPerEvent);
169  etaSim = (*simTrack).momentum().eta();
170  theSimTkId = (*simTrack).trackId();
171 
172  }
173  }
174 
175 
176  // Get the RecTrack collection from the event
178  event.getByToken(theMuonTrackToken, muonTracks);
179 
180  reco::TrackCollection::const_iterator muonTrack;
181 
182  // Loop over the Rec tracks
183  for (muonTrack = muonTracks->begin(); muonTrack != muonTracks->end(); ++muonTrack) {
184 
185  reco::TransientTrack track(*muonTrack,&*theService->magneticField(),theService->trackingGeometry());
186 
187  TrajectoryStateOnSurface outerTSOS = track.outermostMeasurementState();
188  TrajectoryStateOnSurface innerTSOS = track.innermostMeasurementState();
189 
191 
192  // SimHit Energy loss analysis
193  double momAtEntry = -150., momAtExit = -150.;
194 
195  if(theSimHitContainer.size()>1){
196 
197  const GeomDet *geomDetA = theService->trackingGeometry()->idToDet(DetId(theSimHitContainer.front()->detUnitId()));
198  double distA = geomDetA->toGlobal(theSimHitContainer.front()->localPosition()).mag();
199 
200  const GeomDet *geomDetB = theService->trackingGeometry()->idToDet(DetId(theSimHitContainer.back()->detUnitId()));
201  double distB = geomDetB->toGlobal(theSimHitContainer.back()->localPosition()).mag();
202 
203  LogTrace("MuonTrackResidualAnalyzer")<<"Inner SimHit: "<<theSimHitContainer.front()->particleType()
204  <<" Pt: "<<theSimHitContainer.front()->momentumAtEntry().perp()
205  <<" E: "<<theSimHitContainer.front()->momentumAtEntry().perp()
206  <<" R: "<<distA<<endl;
207  LogTrace("MuonTrackResidualAnalyzer")<<"Outer SimHit: "<<theSimHitContainer.back()->particleType()
208  <<" Pt: "<<theSimHitContainer.back()->momentumAtEntry().perp()
209  <<" E: "<<theSimHitContainer.front()->momentumAtEntry().perp()
210  <<" R: "<<distB<<endl;
211 
212  momAtEntry = theSimHitContainer.front()->momentumAtEntry().perp();
213  momAtExit = theSimHitContainer.back()->momentumAtEntry().perp();
214  }
215 
216  trackingRecHit_iterator rhFirst = track.recHitsBegin();
217  trackingRecHit_iterator rhLast = track.recHitsEnd()-1;
218  map<DetId,const PSimHit*>::const_iterator itFirst = muonSimHitsPerId.find((*rhFirst)->geographicalId());
219  map<DetId,const PSimHit*>::const_iterator itLast = muonSimHitsPerId.find((*rhLast)->geographicalId());
220 
221  double momAtEntry2 = -150, momAtExit2 = -150.;
222  if (itFirst != muonSimHitsPerId.end() )
223  momAtEntry2 = itFirst->second->momentumAtEntry().perp();
224  else {
225  LogDebug("MuonTrackResidualAnalyzer")<<"No first sim hit found";
226  ++rhFirst;
227  itFirst = muonSimHitsPerId.find((*rhFirst)->geographicalId());
228  if (itFirst != muonSimHitsPerId.end() )
229  momAtEntry2 = itFirst->second->momentumAtEntry().perp();
230  else{
231  LogDebug("MuonTrackResidualAnalyzer")<<"No second sim hit found";
232  // continue;
233  }
234  }
235 
236  if (itLast != muonSimHitsPerId.end() )
237  momAtExit2 = itLast->second->momentumAtEntry().perp();
238  else {
239  LogDebug("MuonTrackResidualAnalyzer")<<"No last sim hit found";
240  --rhLast;
241  itLast = muonSimHitsPerId.find((*rhLast)->geographicalId());
242  if (itLast != muonSimHitsPerId.end() )
243  momAtExit2 = itLast->second->momentumAtEntry().perp();
244  else{
245  LogDebug("MuonTrackResidualAnalyzer")<<"No last but one sim hit found";
246  // continue;
247  }
248  }
249 
250  if(etaSim){
251  if(momAtEntry >=0 && momAtExit >= 0)
252  hDeltaPtVsEtaSim->Fill(etaSim,momAtEntry-momAtExit);
253  if(momAtEntry2 >=0 && momAtExit2 >= 0)
254  hDeltaPtVsEtaSim2->Fill(etaSim,momAtEntry2-momAtExit2);
255  }
256  else
257  LogDebug("MuonTrackResidualAnalyzer")<<"NO SimTrack'eta";
258  //
259 
260  // computeResolution(trajectoryBW,muonSimHitsPerId,h1DSimHitRes);
261  // computeResolution(smoothed,muonSimHitsPerId,h1DSimHitRes);
262 
263  }
264 }
265 
267  switch(theEtaRange){
268  case all:
269  return ( abs(eta) <= 2.4 ) ? true : false;
270  case barrel:
271  return ( abs(eta) < 1.1 ) ? true : false;
272  case endcap:
273  return ( abs(eta) >= 1.1 && abs(eta) <= 2.4 ) ? true : false;
274  default:
275  {LogDebug("MuonTrackResidualAnalyzer")<<"No correct Eta range selected!! "; return false;}
276  }
277 }
278 
279 // map the muon simhits by id
280 map<DetId,const PSimHit*>
282  Handle<PSimHitContainer> cscSimhits,
283  Handle<PSimHitContainer> rpcSimhits) {
284 
286 
287  map<DetId,const PSimHit*> hitIdMap;
288  theSimHitContainer.clear();
289 
290  mapMuSimHitsPerId(dtSimhits,hitIdMap);
291  mapMuSimHitsPerId(cscSimhits,hitIdMap);
292  mapMuSimHitsPerId(rpcSimhits,hitIdMap);
293 
294  if(theSimHitContainer.size() >1)
295  stable_sort(theSimHitContainer.begin(),theSimHitContainer.end(),RadiusComparatorInOut(theService->trackingGeometry()));
296 
297  LogDebug("MuonTrackResidualAnalyzer")<<"Sim Hit list";
298  int count=1;
299  for(vector<const PSimHit*>::const_iterator it = theSimHitContainer.begin();
300  it != theSimHitContainer.end(); ++it){
301  LogTrace("MuonTrackResidualAnalyzer")<<count
302  << " "
303  << " Process Type: " << (*it)->processType()
304  << " "
305  << debug.dumpMuonId(DetId( (*it)->detUnitId() ))<<endl;
306  }
307 
308  return hitIdMap;
309 }
310 
311 
313  map<DetId,const PSimHit*> &hitIdMap){
314 
315  for(PSimHitContainer::const_iterator simhit = simhits->begin();
316  simhit != simhits->end(); ++simhit) {
317 
318  if ( abs(simhit->particleType()) != 13 && theSimTkId != simhit->trackId()) continue;
319 
320  theSimHitContainer.push_back(&*simhit);
321  DetId id = DetId(simhit->detUnitId());
322 
323  if(id.subdetId() == MuonSubdetId::DT){
324  DTLayerId lId(id.rawId());
325  id = DetId(lId.rawId());
326  }
327 
328  map<DetId,const PSimHit*>::const_iterator it = hitIdMap.find(id);
329 
330  if (it == hitIdMap.end() )
331  hitIdMap[id] = &*simhit;
332  else
333  LogDebug("MuonTrackResidualAnalyzer")<<"TWO muons in the same sensible volume!!";
334 
335  ++theMuonSimHitNumberPerEvent;
336  }
337 }
338 
339 
341  map<DetId,const PSimHit*> &hitIdMap,
343 
345 
346  for(Trajectory::DataContainer::const_iterator datum = data.begin();
347  datum != data.end(); ++datum){
348 
349  GlobalPoint fitPoint = datum->updatedState().globalPosition();
350 
351  // FIXME!
352  // double errX = datum->updatedState().cartesianError().matrix()[0][0];
353  // double errY = datum->updatedState().cartesianError().matrix()[1][1];
354  // double errZ = datum->updatedState().cartesianError().matrix()[2][2];
355  //
356  double errX = datum->updatedState().localError().matrix()(3,3);
357  double errY = datum->updatedState().localError().matrix()(4,4);
358  double errZ = 1.;
359 
360  map<DetId,const PSimHit*>::const_iterator it = hitIdMap.find(datum->recHit()->geographicalId());
361 
362 
363  if (it == hitIdMap.end() ) continue; // FIXME! Put a counter
364 
365  const PSimHit* simhit = it->second;
366 
367  LocalPoint simHitPoint = simhit->localPosition();
368 
369  const GeomDet* geomDet = theService->trackingGeometry()->idToDet(DetId(simhit->detUnitId()));
370 
371  LocalPoint fitLocalPoint = geomDet->toLocal(fitPoint);
372 
373  LocalVector diff = fitLocalPoint-simHitPoint;
374 
375 
376  cout << "SimHit position "<< simHitPoint << endl;
377  cout << "Fit position "<< fitLocalPoint << endl;
378  cout << "Fit position2 "<< datum->updatedState().localPosition() << endl;
379  cout << "Errors on the fit position: (" << errX << "," << errY << "," << errZ << ")"<<endl;
380  cout << "Resolution on x: " << diff.x()/abs(simHitPoint.x()) << endl;
381  cout << "Resolution on y: " << diff.y()/abs(simHitPoint.y()) << endl;
382  cout << "Resolution on z: " << diff.z()/abs(simHitPoint.z()) << endl;
383 
384  cout << "Eta direction: "<< simhit->momentumAtEntry().eta() <<" eta position: " << simHitPoint.eta() << endl;
385  cout << "Phi direction: "<< simhit->momentumAtEntry().phi() <<" phi position: " << simHitPoint.phi() << endl;
386 
387 
388  histos->Fill( simHitPoint.x(), simHitPoint.y(), simHitPoint.z(),
389  diff.x(), diff.y(), diff.z(),
390  errX, errY, errZ,
391  simhit->momentumAtEntry().eta(), simhit->momentumAtEntry().phi());
392  // simHitPoint.eta(), simHitPoint.phi() ); // FIXME!
393  }
394 
395 
396 }
#define LogDebug(id)
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:47
void cd(void)
Definition: DQMStore.cc:269
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
GlobalPoint toGlobal(const Local2DPoint &lp) const
Conversion to the global R.F. from the R.F. of the GeomDet.
Definition: GeomDet.h:54
Geom::Phi< T > phi() const
Definition: PV3DBase.h:69
T y() const
Definition: PV3DBase.h:63
def replace(string, replacements)
LocalPoint toLocal(const GlobalPoint &gp) const
Conversion to the R.F. of the GeomDet.
Definition: GeomDet.h:69
virtual void endRun(edm::Run const &, edm::EventSetup const &) override
std::string dumpMuonId(const DetId &id) const
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
DataContainer const & measurements() const
Definition: Trajectory.h:196
std::vector< TrajectoryMeasurement > DataContainer
Definition: Trajectory.h:44
Local3DPoint localPosition() const
Definition: PSimHit.h:44
simTrack
per collection params
T phi() const
Definition: Phi.h:41
T z() const
Definition: PV3DBase.h:64
MonitorElement * book1D(Args &&...args)
Definition: DQMStore.h:115
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
MuonTrackResidualAnalyzer(const edm::ParameterSet &ps)
Constructor.
#define LogTrace(id)
DQMStore * dbe_
std::vector< ConstRecHitPointer > ConstRecHitContainer
Definition: DetId.h:18
void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override
#define debug
Definition: HDRShower.cc:19
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:277
MonitorElement * book2D(Args &&...args)
Definition: DQMStore.h:133
std::string const & label() const
Definition: InputTag.h:36
T eta() const
Definition: PV3DBase.h:76
std::string const & process() const
Definition: InputTag.h:40
HLT enums.
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
virtual ~MuonTrackResidualAnalyzer()
Destructor.
static const int DT
Definition: MuonSubdetId.h:12
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:333
T x() const
Definition: PV3DBase.h:62
std::string const & instance() const
Definition: InputTag.h:37
void computeResolution(Trajectory &trajectory, std::map< DetId, const PSimHit * > &hitIdMap, HResolution1DRecHit *histos)
unsigned int detUnitId() const
Definition: PSimHit.h:93
TrackingRecHitCollection::base::const_iterator trackingRecHit_iterator
iterator over a vector of reference to TrackingRecHit in the same collection
Definition: event.py:1
Definition: Run.h:43