CMS 3D CMS Logo

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