CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MuonTrackResidualAnalyzer.cc
Go to the documentation of this file.
2 
3 // Collaborating Class Header
8 
10 
16 
21 
27 
28 
29 #include "TFile.h"
30 #include "TH1F.h"
31 #include "TH2F.h"
32 #include "TDirectory.h"
33 
34 using namespace std;
35 using namespace edm;
36 
39 
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  theSeedCollectionLabel = pset.getParameter<InputTag>("MuonSeed");
47 
48  cscSimHitLabel = pset.getParameter<InputTag>("CSCSimHit");
49  dtSimHitLabel = pset.getParameter<InputTag>("DTSimHit");
50  rpcSimHitLabel = pset.getParameter<InputTag>("RPCSimHit");
51 
53  out = pset.getUntrackedParameter<string>("rootFileName");
54  dirName_ = pset.getUntrackedParameter<std::string>("dirName");
55 
56  // Sim or Real
57  theDataType = pset.getParameter<InputTag>("DataType");
58  if(theDataType.label() != "RealData" && theDataType.label() != "SimData")
59  LogDebug("MuonTrackResidualAnalyzer")<<"Error in Data Type!!";
60 
61  theEtaRange = (EtaRange) pset.getParameter<int>("EtaRange");
62 
63  theUpdator = new KFUpdator();
64  theEstimator = new Chi2MeasurementEstimator(100000.);
65 
66  theMuonSimHitNumberPerEvent = 0;
67 }
68 
71  delete theUpdator;
72  delete theEstimator;
73  delete theService;
74 }
75 
76 // Operations
78  LogDebug("MuonTrackResidualAnalyzer")<<"Begin Job";
79 
81 
82  dbe_->cd();
83  InputTag algo = theMuonTrackLabel;
84  string dirName=dirName_;
85  if (algo.process()!="")
86  dirName+=algo.process()+"_";
87  if(algo.label()!="")
88  dirName+=algo.label()+"_";
89  if(algo.instance()!="")
90  dirName+=algo.instance()+"";
91  if (dirName.find("Tracks")<dirName.length()){
92  dirName.replace(dirName.find("Tracks"),6,"");
93  }
94  std::replace(dirName.begin(), dirName.end(), ':', '_');
95  dbe_->setCurrentFolder(dirName.c_str());
96 
97 
98  hDPtRef = dbe_->book1D("DeltaPtRef","P^{in}_{t}-P^{in ref}",10000,-20,20);
99 
100  // Resolution wrt the 1D Rec Hits
101  // h1DRecHitRes = new HResolution1DRecHit("TotalRec");
102 
103  // Resolution wrt the 1d Sim Hits
104  // h1DSimHitRes = new HResolution1DRecHit("TotalSim");
105 
106  hSimHitsPerTrack = dbe_->book1D("SimHitsPerTrack","Number of sim hits per track",55,0,55);
107  hSimHitsPerTrackVsEta = dbe_->book2D("SimHitsPerTrackVsEta","Number of sim hits per track VS #eta",120,-3.,3.,55,0,55);
108  hDeltaPtVsEtaSim = dbe_->book2D("DeltaPtVsEtaSim","#Delta P_{t} vs #eta gen, sim quantity",120,-3.,3.,500,-250.,250.);
109  hDeltaPtVsEtaSim2 = dbe_->book2D("DeltaPtVsEtaSim2","#Delta P_{t} vs #eta gen, sim quantity",120,-3.,3.,500,-250.,250.);
110 }
111 
113  if ( out.size() != 0 && dbe_ ) dbe_->save(out);
114 }
115 
116 
118  LogDebug("MuonTrackResidualAnalyzer")<<"Analyze";
119 
120  // Update the services
121  theService->update(eventSetup);
123  theMuonSimHitNumberPerEvent = 0;
124 
125  // Get the SimHit collection from the event
126  Handle<PSimHitContainer> dtSimHits;
127  event.getByLabel(dtSimHitLabel.instance(),dtSimHitLabel.label(), dtSimHits);
128 
129  Handle<PSimHitContainer> cscSimHits;
130  event.getByLabel(cscSimHitLabel.instance(),cscSimHitLabel.label(), cscSimHits);
131 
132  Handle<PSimHitContainer> rpcSimHits;
133  event.getByLabel(rpcSimHitLabel.instance(),rpcSimHitLabel.label(), rpcSimHits);
134 
135  Handle<SimTrackContainer> simTracks;
136 
137  // FIXME Add the tracker one??
138 
139  // Map simhits per DetId
140  map<DetId, const PSimHit* > muonSimHitsPerId =
141  mapMuSimHitsPerId(dtSimHits,cscSimHits,rpcSimHits);
142 
143  hSimHitsPerTrack->Fill(theMuonSimHitNumberPerEvent);
144 
145  double etaSim=0;
146 
147  if(theDataType.label() == "SimData"){
148 
149  // Get the SimTrack collection from the event
150  event.getByLabel(theDataType.instance(),simTracks);
151 
152  // Loop over the Sim tracks
153  SimTrackContainer::const_iterator simTrack;
154 
155  for (simTrack = simTracks->begin(); simTrack != simTracks->end(); ++simTrack)
156  if (abs((*simTrack).type()) == 13){
157  hSimHitsPerTrackVsEta->Fill((*simTrack).momentum().eta(),theMuonSimHitNumberPerEvent);
158  etaSim = (*simTrack).momentum().eta();
159  theSimTkId = (*simTrack).trackId();
160 
161  }
162  }
163 
164 
165  // Get the RecTrack collection from the event
167  event.getByLabel(theMuonTrackLabel, muonTracks);
168 
169  reco::TrackCollection::const_iterator muonTrack;
170 
171  // Loop over the Rec tracks
172  for (muonTrack = muonTracks->begin(); muonTrack != muonTracks->end(); ++muonTrack) {
173 
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 
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")<<"Inner SimHit: "<<theSimHitContainer.front()->particleType()
193  <<" Pt: "<<theSimHitContainer.front()->momentumAtEntry().perp()
194  <<" E: "<<theSimHitContainer.front()->momentumAtEntry().perp()
195  <<" R: "<<distA<<endl;
196  LogTrace("MuonTrackResidualAnalyzer")<<"Outer SimHit: "<<theSimHitContainer.back()->particleType()
197  <<" Pt: "<<theSimHitContainer.back()->momentumAtEntry().perp()
198  <<" E: "<<theSimHitContainer.front()->momentumAtEntry().perp()
199  <<" 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  }
245  else
246  LogDebug("MuonTrackResidualAnalyzer")<<"NO SimTrack'eta";
247  //
248 
249  // computeResolution(trajectoryBW,muonSimHitsPerId,h1DSimHitRes);
250  // computeResolution(smoothed,muonSimHitsPerId,h1DSimHitRes);
251 
252  }
253 }
254 
256  switch(theEtaRange){
257  case all:
258  return ( abs(eta) <= 2.4 ) ? true : false;
259  case barrel:
260  return ( abs(eta) < 1.1 ) ? true : false;
261  case endcap:
262  return ( abs(eta) >= 1.1 && abs(eta) <= 2.4 ) ? true : false;
263  default:
264  {LogDebug("MuonTrackResidualAnalyzer")<<"No correct Eta range selected!! "; return false;}
265  }
266 }
267 
268 // map the muon simhits by id
269 map<DetId,const PSimHit*>
271  Handle<PSimHitContainer> cscSimhits,
272  Handle<PSimHitContainer> rpcSimhits) {
273 
275 
276  map<DetId,const PSimHit*> hitIdMap;
277  theSimHitContainer.clear();
278 
279  mapMuSimHitsPerId(dtSimhits,hitIdMap);
280  mapMuSimHitsPerId(cscSimhits,hitIdMap);
281  mapMuSimHitsPerId(rpcSimhits,hitIdMap);
282 
283  if(theSimHitContainer.size() >1)
284  stable_sort(theSimHitContainer.begin(),theSimHitContainer.end(),RadiusComparatorInOut(theService->trackingGeometry()));
285 
286  LogDebug("MuonTrackResidualAnalyzer")<<"Sim Hit list";
287  int count=1;
288  for(vector<const PSimHit*>::const_iterator it = theSimHitContainer.begin();
289  it != theSimHitContainer.end(); ++it){
290  LogTrace("MuonTrackResidualAnalyzer")<<count
291  << " "
292  << " Process Type: " << (*it)->processType()
293  << " "
294  << debug.dumpMuonId(DetId( (*it)->detUnitId() ))<<endl;
295  }
296 
297  return hitIdMap;
298 }
299 
300 
302  map<DetId,const PSimHit*> &hitIdMap){
303 
304  for(PSimHitContainer::const_iterator simhit = simhits->begin();
305  simhit != simhits->end(); ++simhit) {
306 
307  if ( abs(simhit->particleType()) != 13 && theSimTkId != simhit->trackId()) continue;
308 
309  theSimHitContainer.push_back(&*simhit);
310  DetId id = DetId(simhit->detUnitId());
311 
312  if(id.subdetId() == MuonSubdetId::DT){
313  DTLayerId lId(id.rawId());
314  id = DetId(lId.rawId());
315  }
316 
317  map<DetId,const PSimHit*>::const_iterator it = hitIdMap.find(id);
318 
319  if (it == hitIdMap.end() )
320  hitIdMap[id] = &*simhit;
321  else
322  LogDebug("MuonTrackResidualAnalyzer")<<"TWO muons in the same sensible volume!!";
323 
324  ++theMuonSimHitNumberPerEvent;
325  }
326 }
327 
328 
330  map<DetId,const PSimHit*> &hitIdMap,
332 
334 
335  for(Trajectory::DataContainer::const_iterator datum = data.begin();
336  datum != data.end(); ++datum){
337 
338  GlobalPoint fitPoint = datum->updatedState().globalPosition();
339 
340  // FIXME!
341  // double errX = datum->updatedState().cartesianError().matrix()[0][0];
342  // double errY = datum->updatedState().cartesianError().matrix()[1][1];
343  // double errZ = datum->updatedState().cartesianError().matrix()[2][2];
344  //
345  double errX = datum->updatedState().localError().matrix()(3,3);
346  double errY = datum->updatedState().localError().matrix()(4,4);
347  double errZ = 1.;
348 
349  map<DetId,const PSimHit*>::const_iterator it = hitIdMap.find(datum->recHit()->geographicalId());
350 
351 
352  if (it == hitIdMap.end() ) continue; // FIXME! Put a counter
353 
354  const PSimHit* simhit = it->second;
355 
356  LocalPoint simHitPoint = simhit->localPosition();
357 
358  const GeomDet* geomDet = theService->trackingGeometry()->idToDet(DetId(simhit->detUnitId()));
359 
360  LocalPoint fitLocalPoint = geomDet->toLocal(fitPoint);
361 
362  LocalVector diff = fitLocalPoint-simHitPoint;
363 
364 
365  cout << "SimHit position "<< simHitPoint << endl;
366  cout << "Fit position "<< fitLocalPoint << endl;
367  cout << "Fit position2 "<< datum->updatedState().localPosition() << endl;
368  cout << "Errors on the fit position: (" << errX << "," << errY << "," << errZ << ")"<<endl;
369  cout << "Resolution on x: " << diff.x()/abs(simHitPoint.x()) << endl;
370  cout << "Resolution on y: " << diff.y()/abs(simHitPoint.y()) << endl;
371  cout << "Resolution on z: " << diff.z()/abs(simHitPoint.z()) << endl;
372 
373  cout << "Eta direction: "<< simhit->momentumAtEntry().eta() <<" eta position: " << simHitPoint.eta() << endl;
374  cout << "Phi direction: "<< simhit->momentumAtEntry().phi() <<" phi position: " << simHitPoint.phi() << endl;
375 
376 
377  histos->Fill( simHitPoint.x(), simHitPoint.y(), simHitPoint.z(),
378  diff.x(), diff.y(), diff.z(),
379  errX, errY, errZ,
380  simhit->momentumAtEntry().eta(), simhit->momentumAtEntry().phi());
381  // simHitPoint.eta(), simHitPoint.phi() ); // FIXME!
382  }
383 
384 
385 }
#define LogDebug(id)
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
std::map< DetId, const PSimHit * > mapMuSimHitsPerId(edm::Handle< edm::PSimHitContainer > dtSimhits, edm::Handle< edm::PSimHitContainer > cscSimhits, edm::Handle< edm::PSimHitContainer > rpcSimhits)
MonitorElement * book1D(const char *name, const char *title, int nchX, double lowX, double highX)
Book 1D histogram.
Definition: DQMStore.cc:722
LocalVector momentumAtEntry() const
The momentum of the track that produced the hit, at entry point.
Definition: PSimHit.h:47
void cd(void)
go to top directory (ie. root)
Definition: DQMStore.cc:411
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:47
void save(const std::string &filename, const std::string &path="", const std::string &pattern="", const std::string &rewrite="", SaveReferenceTag ref=SaveWithReference, int minStatus=dqm::qstatus::STATUS_OK, const std::string &fileupdate="RECREATE")
Definition: DQMStore.cc:2118
Geom::Phi< T > phi() const
Definition: PV3DBase.h:69
T y() const
Definition: PV3DBase.h:63
#define abs(x)
Definition: mlp_lapack.h:159
def replace
Definition: linker.py:10
LocalPoint toLocal(const GlobalPoint &gp) const
Conversion to the R.F. of the GeomDet.
Definition: GeomDet.h:62
T eta() const
MuonTrackResidualAnalyzer(const edm::ParameterSet &pset)
Constructor.
std::string dumpMuonId(const DetId &id) const
uint32_t rawId() const
get the raw id
Definition: DetId.h:45
DataContainer const & measurements() const
Definition: Trajectory.h:215
std::vector< TrajectoryMeasurement > DataContainer
Definition: Trajectory.h:42
Local3DPoint localPosition() const
Definition: PSimHit.h:44
T phi() const
Definition: Phi.h:41
T z() const
Definition: PV3DBase.h:64
tuple result
Definition: query.py:137
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)
DQMStore * dbe_
void analyze(const edm::Event &event, const edm::EventSetup &eventSetup)
tuple out
Definition: dbtoconf.py:99
std::vector< ConstRecHitPointer > ConstRecHitContainer
Definition: DetId.h:20
std::string const & label() const
Definition: InputTag.h:42
T eta() const
Definition: PV3DBase.h:76
std::string const & process() const
Definition: InputTag.h:46
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
virtual ~MuonTrackResidualAnalyzer()
Destructor.
tuple cout
Definition: gather_cfg.py:121
static const int DT
Definition: MuonSubdetId.h:14
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:340
void showDirStructure(void) const
Definition: DQMStore.cc:2766
#define debug
Definition: MEtoEDMFormat.h:34
MonitorElement * book2D(const char *name, const char *title, int nchX, double lowX, double highX, int nchY, double lowY, double highY)
Book 2D histogram.
Definition: DQMStore.cc:850
T x() const
Definition: PV3DBase.h:62
std::string const & instance() const
Definition: InputTag.h:43
void computeResolution(Trajectory &trajectory, std::map< DetId, const PSimHit * > &hitIdMap, HResolution1DRecHit *histos)
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:434
unsigned int detUnitId() const
Definition: PSimHit.h:93