CMS 3D CMS Logo

SiStripDigitizer.cc
Go to the documentation of this file.
1 // File: SiStripDigitizerAlgorithm.cc
2 // Description: Class for digitization.
3 
4 // Modified 15/May/2013 mark.grimes@bristol.ac.uk - Modified so that the digi-sim link has the correct
5 // index for the sim hits stored. It was previously always set to zero (I won't mention that it was
6 // me who originally wrote that).
7 
8 // system include files
9 #include <memory>
10 
12 
13 #include "SiStripDigitizer.h"
16 
21 
22 // user include files
25 
28 
32 
34 //needed for the geometry:
44 //needed for the magnetic field:
47 
48 //Data Base infromations
52 
53 //Random Number
57 
59  : gainLabel(conf.getParameter<std::string>("Gain")),
60  hitsProducer(conf.getParameter<std::string>("hitsProducer")),
61  trackerContainers(conf.getParameter<std::vector<std::string>>("ROUList")),
62  ZSDigi(conf.getParameter<edm::ParameterSet>("DigiModeList").getParameter<std::string>("ZSDigi")),
63  SCDigi(conf.getParameter<edm::ParameterSet>("DigiModeList").getParameter<std::string>("SCDigi")),
64  VRDigi(conf.getParameter<edm::ParameterSet>("DigiModeList").getParameter<std::string>("VRDigi")),
65  PRDigi(conf.getParameter<edm::ParameterSet>("DigiModeList").getParameter<std::string>("PRDigi")),
66  geometryType(conf.getParameter<std::string>("GeometryType")),
67  useConfFromDB(conf.getParameter<bool>("TrackerConfigurationFromDB")),
68  zeroSuppression(conf.getParameter<bool>("ZeroSuppression")),
69  makeDigiSimLinks_(conf.getUntrackedParameter<bool>("makeDigiSimLinks", false)) {
70  const std::string alias("simSiStripDigis");
71 
72  mixMod.produces<edm::DetSetVector<SiStripDigi>>(ZSDigi).setBranchAlias(ZSDigi);
73  mixMod.produces<edm::DetSetVector<SiStripRawDigi>>(SCDigi).setBranchAlias(alias + SCDigi);
74  mixMod.produces<edm::DetSetVector<SiStripRawDigi>>(VRDigi).setBranchAlias(alias + VRDigi);
75  mixMod.produces<edm::DetSetVector<SiStripRawDigi>>(PRDigi).setBranchAlias(alias + PRDigi);
76  mixMod.produces<edm::DetSetVector<StripDigiSimLink>>().setBranchAlias(alias + "siStripDigiSimLink");
77  mixMod.produces<std::vector<std::pair<int, std::bitset<6>>>>("AffectedAPVList").setBranchAlias(alias + "AffectedAPV");
78  for (auto const& trackerContainer : trackerContainers) {
79  edm::InputTag tag(hitsProducer, trackerContainer);
80  iC.consumes<std::vector<PSimHit>>(edm::InputTag(hitsProducer, trackerContainer));
81  }
83  if (!rng.isAvailable()) {
84  throw cms::Exception("Configuration")
85  << "SiStripDigitizer requires the RandomNumberGeneratorService\n"
86  "which is not present in the configuration file. You must add the service\n"
87  "in the configuration file or remove the modules that require it.";
88  }
89  theDigiAlgo.reset(new SiStripDigitizerAlgorithm(conf));
90 }
91 
92 // Virtual destructor needed.
94 
95 void SiStripDigitizer::accumulateStripHits(edm::Handle<std::vector<PSimHit>> hSimHits,
96  const TrackerTopology* tTopo,
97  size_t globalSimHitIndex,
98  const unsigned int tofBin) {
99  // globalSimHitIndex is the index the sim hit will have when it is put in a collection
100  // of sim hits for all crossings. This is only used when creating digi-sim links if
101  // configured to do so.
102 
103  if (hSimHits.isValid()) {
104  std::set<unsigned int> detIds;
105  std::vector<PSimHit> const& simHits = *hSimHits.product();
106  for (std::vector<PSimHit>::const_iterator it = simHits.begin(), itEnd = simHits.end(); it != itEnd;
107  ++it, ++globalSimHitIndex) {
108  unsigned int detId = (*it).detUnitId();
109  if (detIds.insert(detId).second) {
110  // The insert succeeded, so this detector element has not yet been processed.
111  assert(detectorUnits[detId]);
112  if (detectorUnits[detId]->type().isTrackerStrip()) { // this test can be removed and replaced by stripdet!=0
113  auto stripdet = detectorUnits[detId];
114  //access to magnetic field in global coordinates
115  GlobalVector bfield = pSetup->inTesla(stripdet->surface().position());
116  LogDebug("Digitizer ") << "B-field(T) at " << stripdet->surface().position()
117  << "(cm): " << pSetup->inTesla(stripdet->surface().position());
118  theDigiAlgo->accumulateSimHits(it, itEnd, globalSimHitIndex, tofBin, stripdet, bfield, tTopo, randomEngine_);
119  }
120  }
121  } // end of loop over sim hits
122  }
123 }
124 
125 // Functions that gets called by framework every event
127  //Retrieve tracker topology from geometry
129  iSetup.get<TrackerTopologyRcd>().get(tTopoHand);
130  const TrackerTopology* tTopo = tTopoHand.product();
131 
132  // Step A: Get Inputs
133  for (auto const& trackerContainer : trackerContainers) {
135  edm::InputTag tag(hitsProducer, trackerContainer);
136  unsigned int tofBin = StripDigiSimLink::LowTof;
137  if (trackerContainer.find(std::string("HighTof")) != std::string::npos)
138  tofBin = StripDigiSimLink::HighTof;
139 
140  iEvent.getByLabel(tag, simHits);
141  accumulateStripHits(simHits, tTopo, crossingSimHitIndexOffset_[tag.encode()], tofBin);
142  // Now that the hits have been processed, I'll add the amount of hits in this crossing on to
143  // the global counter. Next time accumulateStripHits() is called it will count the sim hits
144  // as though they were on the end of this collection.
145  // Note that this is only used for creating digi-sim links (if configured to do so).
146  if (simHits.isValid())
147  crossingSimHitIndexOffset_[tag.encode()] += simHits->size();
148  }
149 }
150 
152  edm::EventSetup const& iSetup,
153  edm::StreamID const& streamID) {
155  iSetup.get<TrackerTopologyRcd>().get(tTopoHand);
156  const TrackerTopology* tTopo = tTopoHand.product();
157 
158  //Re-compute luminosity for accumulation for HIP effects
159  theDigiAlgo->calculateInstlumiScale(PileupInfo_.get());
160 
161  // Step A: Get Inputs
162  for (auto const& trackerContainer : trackerContainers) {
164  edm::InputTag tag(hitsProducer, trackerContainer);
165  unsigned int tofBin = StripDigiSimLink::LowTof;
166  if (trackerContainer.find(std::string("HighTof")) != std::string::npos)
167  tofBin = StripDigiSimLink::HighTof;
168 
169  iEvent.getByLabel(tag, simHits);
170  accumulateStripHits(simHits, tTopo, crossingSimHitIndexOffset_[tag.encode()], tofBin);
171  // Now that the hits have been processed, I'll add the amount of hits in this crossing on to
172  // the global counter. Next time accumulateStripHits() is called it will count the sim hits
173  // as though they were on the end of this collection.
174  // Note that this is only used for creating digi-sim links (if configured to do so).
175  if (simHits.isValid())
176  crossingSimHitIndexOffset_[tag.encode()] += simHits->size();
177  }
178 }
179 
181  // Make sure that the first crossing processed starts indexing the sim hits from zero.
182  // This variable is used so that the sim hits from all crossing frames have sequential
183  // indices used to create the digi-sim link (if configured to do so) rather than starting
184  // from zero for each crossing.
186  theAffectedAPVvector.clear();
187  // Step A: Get Inputs
188 
189  if (useConfFromDB) {
191  iSetup.get<SiStripDetCablingRcd>().get(detCabling);
192  detCabling->addConnected(theDetIdList);
193  }
194 
195  // Cache random number engine
197  randomEngine_ = &rng->getEngine(iEvent.streamID());
198 
199  theDigiAlgo->initializeEvent(iSetup);
200 
202  iSetup.get<IdealMagneticFieldRecord>().get(pSetup);
203 
204  // FIX THIS! We only need to clear and (re)fill detectorUnits when the geometry type IOV changes. Use ESWatcher to determine this.
205  bool changes = true;
206  if (changes) { // Replace with ESWatcher
207  detectorUnits.clear();
208  }
209  for (const auto& iu : pDD->detUnits()) {
210  unsigned int detId = iu->geographicalId().rawId();
211  if (iu->type().isTrackerStrip()) {
212  auto stripdet = dynamic_cast<StripGeomDetUnit const*>(iu);
213  assert(stripdet != nullptr);
214  if (changes) { // Replace with ESWatcher
215  detectorUnits.insert(std::make_pair(detId, stripdet));
216  }
217  theDigiAlgo->initializeDetUnit(stripdet, iSetup);
218  }
219  }
220 }
221 
223  edm::ESHandle<SiStripGain> gainHandle;
224  edm::ESHandle<SiStripNoises> noiseHandle;
225  edm::ESHandle<SiStripThreshold> thresholdHandle;
226  edm::ESHandle<SiStripPedestals> pedestalHandle;
227  iSetup.get<SiStripGainSimRcd>().get(gainLabel, gainHandle);
228  iSetup.get<SiStripNoisesRcd>().get(noiseHandle);
229  iSetup.get<SiStripThresholdRcd>().get(thresholdHandle);
230  iSetup.get<SiStripPedestalsRcd>().get(pedestalHandle);
231 
232  std::vector<edm::DetSet<SiStripDigi>> theDigiVector;
233  std::vector<edm::DetSet<SiStripRawDigi>> theRawDigiVector;
234  std::unique_ptr<edm::DetSetVector<StripDigiSimLink>> pOutputDigiSimLink(new edm::DetSetVector<StripDigiSimLink>);
235 
236  // Step B: LOOP on StripGeomDetUnit
237  theDigiVector.reserve(10000);
238  theDigiVector.clear();
239 
240  for (const auto& iu : pDD->detUnits()) {
241  if (useConfFromDB) {
242  //apply the cable map _before_ digitization: consider only the detis that are connected
243  if (theDetIdList.find(iu->geographicalId().rawId()) == theDetIdList.end())
244  continue;
245  }
246  auto sgd = dynamic_cast<StripGeomDetUnit const*>(iu);
247  if (sgd != nullptr) {
248  edm::DetSet<SiStripDigi> collectorZS(iu->geographicalId().rawId());
249  edm::DetSet<SiStripRawDigi> collectorRaw(iu->geographicalId().rawId());
250  edm::DetSet<StripDigiSimLink> collectorLink(iu->geographicalId().rawId());
251  theDigiAlgo->digitize(collectorZS,
252  collectorRaw,
253  collectorLink,
254  sgd,
255  gainHandle,
256  thresholdHandle,
257  noiseHandle,
258  pedestalHandle,
260  randomEngine_);
261  if (zeroSuppression) {
262  if (!collectorZS.data.empty()) {
263  theDigiVector.push_back(collectorZS);
264  if (!collectorLink.data.empty())
265  pOutputDigiSimLink->insert(collectorLink);
266  }
267  } else {
268  if (!collectorRaw.data.empty()) {
269  theRawDigiVector.push_back(collectorRaw);
270  if (!collectorLink.data.empty())
271  pOutputDigiSimLink->insert(collectorLink);
272  }
273  }
274  }
275  }
276  if (zeroSuppression) {
277  // Step C: create output collection
278  std::unique_ptr<edm::DetSetVector<SiStripRawDigi>> output_virginraw(new edm::DetSetVector<SiStripRawDigi>());
279  std::unique_ptr<edm::DetSetVector<SiStripRawDigi>> output_scopemode(new edm::DetSetVector<SiStripRawDigi>());
280  std::unique_ptr<edm::DetSetVector<SiStripRawDigi>> output_processedraw(new edm::DetSetVector<SiStripRawDigi>());
281  std::unique_ptr<edm::DetSetVector<SiStripDigi>> output(new edm::DetSetVector<SiStripDigi>(theDigiVector));
282  std::unique_ptr<std::vector<std::pair<int, std::bitset<6>>>> AffectedAPVList(
283  new std::vector<std::pair<int, std::bitset<6>>>(theAffectedAPVvector));
284  // Step D: write output to file
285  iEvent.put(std::move(output), ZSDigi);
286  iEvent.put(std::move(output_scopemode), SCDigi);
287  iEvent.put(std::move(output_virginraw), VRDigi);
288  iEvent.put(std::move(output_processedraw), PRDigi);
289  iEvent.put(std::move(AffectedAPVList), "AffectedAPVList");
290  if (makeDigiSimLinks_)
291  iEvent.put(
292  std::move(pOutputDigiSimLink)); // The previous EDProducer didn't name this collection so I won't either
293  } else {
294  // Step C: create output collection
295  std::unique_ptr<edm::DetSetVector<SiStripRawDigi>> output_virginraw(
296  new edm::DetSetVector<SiStripRawDigi>(theRawDigiVector));
297  std::unique_ptr<edm::DetSetVector<SiStripRawDigi>> output_scopemode(new edm::DetSetVector<SiStripRawDigi>());
298  std::unique_ptr<edm::DetSetVector<SiStripRawDigi>> output_processedraw(new edm::DetSetVector<SiStripRawDigi>());
299  std::unique_ptr<edm::DetSetVector<SiStripDigi>> output(new edm::DetSetVector<SiStripDigi>());
300  // Step D: write output to file
301  iEvent.put(std::move(output), ZSDigi);
302  iEvent.put(std::move(output_scopemode), SCDigi);
303  iEvent.put(std::move(output_virginraw), VRDigi);
304  iEvent.put(std::move(output_processedraw), PRDigi);
305  if (makeDigiSimLinks_)
306  iEvent.put(
307  std::move(pOutputDigiSimLink)); // The previous EDProducer didn't name this collection so I won't either
308  }
309  randomEngine_ = nullptr; // to prevent access outside event
310 }
#define LogDebug(id)
BranchAliasSetterT< ProductType > produces()
declare what type of product will make and with which optional label
const vstring trackerContainers
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
type
Definition: HCALResponse.h:21
const std::string ZSDigi
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
edm::ESHandle< TrackerGeometry > pDD
const DetContainer & detUnits() const override
Returm a vector of all GeomDet.
const bool zeroSuppression
void initializeEvent(edm::Event const &e, edm::EventSetup const &c) override
const std::string hitsProducer
void finalizeEvent(edm::Event &e, edm::EventSetup const &c) override
void accumulate(edm::Event const &e, edm::EventSetup const &c) override
std::map< unsigned int, StripGeomDetUnit const * > detectorUnits
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &)=0
Use this engine in event methods.
const bool useConfFromDB
std::string encode() const
Definition: InputTag.cc:159
std::map< uint32_t, std::vector< int > > theDetIdList
CLHEP::HepRandomEngine * randomEngine_
int iEvent
Definition: GenABIO.cc:224
void accumulateStripHits(edm::Handle< std::vector< PSimHit >>, const TrackerTopology *tTopo, size_t globalSimHitIndex, const unsigned int tofBin)
bool isValid() const
Definition: HandleBase.h:74
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:480
virtual GlobalVector inTesla(const GlobalPoint &gp) const =0
Field value ad specified global point, in Tesla.
bool isTrackerStrip(GeomDetEnumerators::SubDetector m)
~SiStripDigitizer() override
std::unique_ptr< SiStripDigitizerAlgorithm > theDigiAlgo
edm::ESHandle< MagneticField > pSetup
const std::string VRDigi
const std::string PRDigi
const std::string geometryType
std::vector< std::pair< int, std::bitset< 6 > > > theAffectedAPVvector
HLT enums.
bool getByLabel(edm::InputTag const &tag, edm::Handle< T > &result) const
T get() const
Definition: EventSetup.h:71
StreamID streamID() const
Definition: Event.h:95
std::map< std::string, size_t > crossingSimHitIndexOffset_
Offset to add to the index of each sim hit to account for which crossing it&#39;s in. ...
SiStripDigitizer(const edm::ParameterSet &conf, edm::ProducerBase &mixMod, edm::ConsumesCollector &iC)
void addConnected(std::map< uint32_t, std::vector< int >> &) const
const bool makeDigiSimLinks_
Whether or not to create the association to sim truth collection. Set in configuration.
T const * product() const
Definition: ESHandle.h:86
const std::string gainLabel
def move(src, dest)
Definition: eostools.py:511
std::unique_ptr< PileupMixingContent > PileupInfo_
const std::string SCDigi