CMS 3D CMS Logo

MuonTruth.cc
Go to the documentation of this file.
6 
8  const edm::EventSetup &setup,
9  const edm::ParameterSet &conf,
11  : theDigiSimLinks(nullptr),
12  theWireDigiSimLinks(nullptr),
13  linksTag(conf.getParameter<edm::InputTag>("CSClinksTag")),
14  wireLinksTag(conf.getParameter<edm::InputTag>("CSCwireLinksTag")),
15  // CrossingFrame used or not ?
16  crossingframe(conf.getParameter<bool>("crossingframe")),
17  CSCsimHitsTag(conf.getParameter<edm::InputTag>("CSCsimHitsTag")),
18  CSCsimHitsXFTag(conf.getParameter<edm::InputTag>("CSCsimHitsXFTag")),
19  geomToken_(iC.esConsumes<CSCGeometry, MuonGeometryRecord>()),
21  linksToken_(iC.consumes<DigiSimLinks>(linksTag)),
22  wireLinksToken_(iC.consumes<DigiSimLinks>(wireLinksTag)) {
24 }
25 
27  : theDigiSimLinks(nullptr),
28  theWireDigiSimLinks(nullptr),
29  linksTag(conf.getParameter<edm::InputTag>("CSClinksTag")),
30  wireLinksTag(conf.getParameter<edm::InputTag>("CSCwireLinksTag")),
31  // CrossingFrame used or not ?
32  crossingframe(conf.getParameter<bool>("crossingframe")),
33  CSCsimHitsTag(conf.getParameter<edm::InputTag>("CSCsimHitsTag")),
34  CSCsimHitsXFTag(conf.getParameter<edm::InputTag>("CSCsimHitsXFTag")),
35  linksToken_(iC.consumes<DigiSimLinks>(linksTag)),
36  wireLinksToken_(iC.consumes<DigiSimLinks>(wireLinksTag)) {
37  if (crossingframe) {
39  } else if (!CSCsimHitsTag.label().empty()) {
41  }
42 }
43 
45  LogTrace("MuonTruth") << "getting CSC Strip DigiSimLink collection - " << linksTag;
46  const edm::Handle<DigiSimLinks> &digiSimLinks = event.getHandle(linksToken_);
47  theDigiSimLinks = digiSimLinks.product();
48 
49  LogTrace("MuonTruth") << "getting CSC Wire DigiSimLink collection - " << wireLinksTag;
50  const edm::Handle<DigiSimLinks> &wireDigiSimLinks = event.getHandle(wireLinksToken_);
51  theWireDigiSimLinks = wireDigiSimLinks.product();
52 
53  // get CSC Geometry to use CSCLayer methods
54  const edm::ESHandle<CSCGeometry> &mugeom = setup.getHandle(geomToken_);
55  cscgeom = mugeom.product();
56 
57  // get CSC Bad Chambers (ME4/2)
58  const edm::ESHandle<CSCBadChambers> &badChambers = setup.getHandle(badToken_);
59  cscBadChambers = badChambers.product();
60 
61  theSimHitMap.clear();
62 
63  if (crossingframe) {
64  LogTrace("MuonTruth") << "getting CrossingFrame<PSimHit> collection - " << CSCsimHitsXFTag;
65  const edm::Handle<CrossingFrame<PSimHit>> &cf = event.getHandle(simHitsXFToken_);
66 
67  std::unique_ptr<MixCollection<PSimHit>> CSCsimhits(new MixCollection<PSimHit>(cf.product()));
68  LogTrace("MuonTruth") << "... size = " << CSCsimhits->size();
69 
70  for (MixCollection<PSimHit>::MixItr hitItr = CSCsimhits->begin(); hitItr != CSCsimhits->end(); ++hitItr) {
71  theSimHitMap[hitItr->detUnitId()].push_back(*hitItr);
72  }
73 
74  } else if (!CSCsimHitsTag.label().empty()) {
75  LogTrace("MuonTruth") << "getting PSimHit collection - " << CSCsimHitsTag;
76  const edm::Handle<edm::PSimHitContainer> &CSCsimhits = event.getHandle(simHitsToken_);
77  LogTrace("MuonTruth") << "... size = " << CSCsimhits->size();
78 
79  for (edm::PSimHitContainer::const_iterator hitItr = CSCsimhits->begin(); hitItr != CSCsimhits->end(); ++hitItr) {
80  theSimHitMap[hitItr->detUnitId()].push_back(*hitItr);
81  }
82  }
83 }
84 
86  if (theChargeMap.empty())
87  return 0.;
88 
89  float muonCharge = 0.;
90  for (std::map<SimHitIdpr, float>::const_iterator chargeMapItr = theChargeMap.begin();
91  chargeMapItr != theChargeMap.end();
92  ++chargeMapItr) {
93  if (abs(particleType(chargeMapItr->first)) == 13) {
94  muonCharge += chargeMapItr->second;
95  }
96  }
97 
98  return muonCharge / theTotalCharge;
99 }
100 
101 std::vector<PSimHit> MuonTruth::simHits() {
102  std::vector<PSimHit> result;
103  for (std::map<SimHitIdpr, float>::const_iterator chargeMapItr = theChargeMap.begin();
104  chargeMapItr != theChargeMap.end();
105  ++chargeMapItr) {
106  std::vector<PSimHit> trackHits = hitsFromSimTrack(chargeMapItr->first);
107  result.insert(result.end(), trackHits.begin(), trackHits.end());
108  }
109 
110  return result;
111 }
112 
113 std::vector<PSimHit> MuonTruth::muonHits() {
114  std::vector<PSimHit> result;
115  std::vector<PSimHit> allHits = simHits();
116  std::vector<PSimHit>::const_iterator hitItr = allHits.begin(), lastHit = allHits.end();
117 
118  for (; hitItr != lastHit; ++hitItr) {
119  if (abs((*hitItr).particleType()) == 13) {
120  result.push_back(*hitItr);
121  }
122  }
123  return result;
124 }
125 
126 std::vector<PSimHit> MuonTruth::hitsFromSimTrack(MuonTruth::SimHitIdpr truthId) {
127  std::vector<PSimHit> result;
128 
129  auto found = theSimHitMap.find(theDetId);
130  if (found != theSimHitMap.end()) {
131  for (auto const &hit : found->second) {
132  unsigned int hitTrack = hit.trackId();
133  EncodedEventId hitEvId = hit.eventId();
134 
135  if (hitTrack == truthId.first && hitEvId == truthId.second) {
136  result.push_back(hit);
137  }
138  }
139  }
140  return result;
141 }
142 
144  int result = 0;
145  const std::vector<PSimHit> &hits = hitsFromSimTrack(truthId);
146  if (!hits.empty()) {
147  result = hits[0].particleType();
148  }
149  return result;
150 }
151 
153  theChargeMap.clear();
154  theTotalCharge = 0.;
155  theDetId = recHit.geographicalId().rawId();
156 
157  int nchannels = recHit.nStrips();
158  const CSCLayerGeometry *laygeom = cscgeom->layer(recHit.cscDetId())->geometry();
159 
160  for (int idigi = 0; idigi < nchannels; ++idigi) {
161  // strip and readout channel numbers may differ in ME1/1A
162  int istrip = recHit.channels(idigi);
163  int channel = laygeom->channel(istrip);
164  float weight = recHit.adcs(idigi, 0); // DL: I think this is wrong before and after...seems to
165  // assume one time binadcContainer[idigi];
166 
168 
169  if (layerLinks != theDigiSimLinks->end()) {
170  addChannel(*layerLinks, channel, weight);
171  }
172  }
173 }
174 
175 void MuonTruth::analyze(const CSCStripDigi &stripDigi, int rawDetIdCorrespondingToCSCLayer) {
176  theDetId = rawDetIdCorrespondingToCSCLayer;
177  theChargeMap.clear();
178  theTotalCharge = 0.;
179 
181  if (layerLinks != theDigiSimLinks->end()) {
182  addChannel(*layerLinks, stripDigi.getStrip(), 1.);
183  }
184 }
185 
186 void MuonTruth::analyze(const CSCWireDigi &wireDigi, int rawDetIdCorrespondingToCSCLayer) {
187  theDetId = rawDetIdCorrespondingToCSCLayer;
188  theChargeMap.clear();
189  theTotalCharge = 0.;
190 
192 
193  if (layerLinks != theDigiSimLinks->end()) {
194  // In the simulation digis, the channel labels for wires and strips must be
195  // distinct, therefore:
196  int wireDigiInSimulation = wireDigi.getWireGroup() + 100;
197  //
198  addChannel(*layerLinks, wireDigiInSimulation, 1.);
199  }
200 }
201 
202 void MuonTruth::addChannel(const LayerLinks &layerLinks, int channel, float weight) {
203  LayerLinks::const_iterator linkItr = layerLinks.begin(), lastLayerLink = layerLinks.end();
204 
205  for (; linkItr != lastLayerLink; ++linkItr) {
206  int linkChannel = linkItr->channel();
207  if (linkChannel == channel) {
208  float charge = linkItr->fraction() * weight;
210  // see if it's in the map
211  SimHitIdpr truthId(linkItr->SimTrackId(), linkItr->eventId());
212  std::map<SimHitIdpr, float>::const_iterator chargeMapItr = theChargeMap.find(truthId);
213  if (chargeMapItr == theChargeMap.end()) {
214  theChargeMap[truthId] = charge;
215  } else {
216  theChargeMap[truthId] += charge;
217  }
218  }
219  }
220 }
std::map< unsigned int, edm::PSimHitContainer > theSimHitMap
Definition: MuonTruth.h:80
iterator end()
Definition: DetSet.h:58
std::pair< uint32_t, EncodedEventId > SimHitIdpr
Definition: MuonTruth.h:30
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
iterator find(det_id_type id)
Definition: DetSetVector.h:264
int particleType(SimHitIdpr truthId)
Definition: MuonTruth.cc:143
T const * product() const
Definition: Handle.h:70
Definition: weight.py:1
const DigiSimLinks * theWireDigiSimLinks
Definition: MuonTruth.h:63
edm::EDGetTokenT< edm::PSimHitContainer > simHitsToken_
Definition: MuonTruth.h:78
const CSCGeometry * cscgeom
Definition: MuonTruth.h:82
std::string const & label() const
Definition: InputTag.h:36
std::map< SimHitIdpr, float > theChargeMap
Definition: MuonTruth.h:57
std::vector< PSimHit > muonHits()
Definition: MuonTruth.cc:113
const edm::ESGetToken< CSCGeometry, MuonGeometryRecord > geomToken_
Definition: MuonTruth.h:72
int getStrip() const
Definition: CSCStripDigi.h:41
int channel(int strip) const
std::vector< PSimHit > simHits()
Definition: MuonTruth.cc:101
#define LogTrace(id)
T const * product() const
Definition: ESHandle.h:86
const CSCBadChambers * cscBadChambers
Definition: MuonTruth.h:48
unsigned int theDetId
Definition: MuonTruth.h:60
std::vector< PSimHit > hitsFromSimTrack(SimHitIdpr truthId)
Definition: MuonTruth.cc:126
const edm::InputTag wireLinksTag
Definition: MuonTruth.h:66
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
const edm::EDGetTokenT< DigiSimLinks > wireLinksToken_
Definition: MuonTruth.h:76
const edm::InputTag CSCsimHitsXFTag
Definition: MuonTruth.h:70
const edm::InputTag linksTag
Definition: MuonTruth.h:65
void analyze(const CSCRecHit2D &recHit)
Definition: MuonTruth.cc:152
iterator end()
Return the off-the-end iterator.
Definition: DetSetVector.h:325
iterator begin()
Definition: DetSet.h:57
float theTotalCharge
Definition: MuonTruth.h:58
const edm::InputTag CSCsimHitsTag
Definition: MuonTruth.h:69
MuonTruth(const edm::Event &, const edm::EventSetup &, const edm::ParameterSet &, edm::ConsumesCollector &)
Definition: MuonTruth.cc:7
const edm::EDGetTokenT< DigiSimLinks > linksToken_
Definition: MuonTruth.h:75
HLT enums.
edm::EDGetTokenT< CrossingFrame< PSimHit > > simHitsXFToken_
Definition: MuonTruth.h:77
std::vector< PSimHit > PSimHitContainer
float muonFraction()
analyze() must be called before any of the following
Definition: MuonTruth.cc:85
const DigiSimLinks * theDigiSimLinks
Definition: MuonTruth.h:62
collection_type::const_iterator const_iterator
Definition: DetSet.h:31
collection_type::const_iterator const_iterator
Definition: DetSetVector.h:102
const CSCLayer * layer(CSCDetId id) const
Return the layer corresponding to given DetId.
Definition: CSCGeometry.cc:105
const bool crossingframe
Definition: MuonTruth.h:68
void addChannel(const LayerLinks &layerLinks, int channel, float weight=1.)
Definition: MuonTruth.cc:202
const edm::ESGetToken< CSCBadChambers, CSCBadChambersRcd > badToken_
Definition: MuonTruth.h:73
Definition: event.py:1
int getWireGroup() const
default
Definition: CSCWireDigi.h:22
void initEvent(const edm::Event &, const edm::EventSetup &)
Definition: MuonTruth.cc:44