CMS 3D CMS Logo

Phase2TrackerDigitizer.cc
Go to the documentation of this file.
1 //
2 //
3 // Package: Phase2TrackerDigitizer
4 // Class: Phase2TrackerDigitizer
5 //
6 // *\class SiPhase2TrackerDigitizer Phase2TrackerDigitizer.cc SimTracker/SiPhase2Digitizer/src/Phase2TrackerDigitizer.cc
7 //
8 // Author: Suchandra Dutta, Suvankar Roy Chowdhury, Subir Sarkar
9 // Date: January 29, 2016
10 // Description: <one line class summary>
11 //
12 // Implementation:
13 // <Notes on implementation>
14 //
15 #include <memory>
16 #include <set>
17 #include <iostream>
18 
27 
33 
37 
44 
47 
50 
52 
54 
55 // Random Number
58 
59 namespace cms {
60 
62  edm::ProducesCollector producesCollector,
64  : first_(true),
65  hitsProducer_(iConfig.getParameter<std::string>("hitsProducer")),
66  trackerContainers_(iConfig.getParameter<std::vector<std::string> >("ROUList")),
67  pDDToken_(iC.esConsumes(edm::ESInputTag("", iConfig.getParameter<std::string>("GeometryType")))),
68  pSetupToken_(iC.esConsumes()),
69  tTopoToken_(iC.esConsumes()),
70  isOuterTrackerReadoutAnalog_(iConfig.getParameter<bool>("isOTreadoutAnalog")),
71  premixStage1_(iConfig.getParameter<bool>("premixStage1")),
72  makeDigiSimLinks_(
73  iConfig.getParameter<edm::ParameterSet>("AlgorithmCommon").getUntrackedParameter<bool>("makeDigiSimLinks")) {
74  const std::string alias1("simSiPixelDigis");
75  producesCollector.produces<edm::DetSetVector<PixelDigi> >("Pixel").setBranchAlias(alias1);
77  producesCollector.produces<edm::DetSetVector<PixelDigiSimLink> >("Pixel").setBranchAlias(alias1);
78 
79  if (!iConfig.getParameter<bool>("isOTreadoutAnalog")) {
80  const std::string alias2("simSiTrackerDigis");
81  if (premixStage1_) {
82  // Premixing exploits the ADC field of PixelDigi to store the collected charge
83  // But we still want everything else to be treated like for Phase2TrackerDigi
84  producesCollector.produces<edm::DetSetVector<PixelDigi> >("Tracker").setBranchAlias(alias2);
85  } else {
86  producesCollector.produces<edm::DetSetVector<Phase2TrackerDigi> >("Tracker").setBranchAlias(alias2);
87  }
89  producesCollector.produces<edm::DetSetVector<PixelDigiSimLink> >("Tracker").setBranchAlias(alias2);
90  }
91  // creating algorithm objects and pushing them into the map
92  algomap_[AlgorithmType::InnerPixel] = std::make_unique<PixelDigitizerAlgorithm>(iConfig, iC);
93  algomap_[AlgorithmType::InnerPixel3D] = std::make_unique<Pixel3DDigitizerAlgorithm>(iConfig, iC);
94  algomap_[AlgorithmType::PixelinPS] = std::make_unique<PSPDigitizerAlgorithm>(iConfig, iC);
95  algomap_[AlgorithmType::StripinPS] = std::make_unique<PSSDigitizerAlgorithm>(iConfig, iC);
96  algomap_[AlgorithmType::TwoStrip] = std::make_unique<SSDigitizerAlgorithm>(iConfig, iC);
97  }
98 
100  void Phase2TrackerDigitizer::accumulatePixelHits(edm::Handle<std::vector<PSimHit> > hSimHits,
101  size_t globalSimHitIndex,
102  const uint32_t tofBin) {
103  if (hSimHits.isValid()) {
104  std::set<uint32_t> detIds;
105  auto const& simHits = *(hSimHits.product());
106  for (auto it = std::begin(simHits), itEnd = std::end(simHits); it != itEnd; ++it, ++globalSimHitIndex) {
107  uint32_t detId_raw = (*it).detUnitId();
108  auto fiter = detectorUnits_.find(detId_raw);
109  if (fiter == detectorUnits_.end())
110  continue;
111 
112  if (detIds.insert(detId_raw).second) {
113  // The insert succeeded, so this detector element has not yet been processed.
114  const Phase2TrackerGeomDetUnit* phase2det = fiter->second;
115 
116  // access to magnetic field in global coordinates
117  GlobalVector bfield = pSetup_->inTesla(phase2det->surface().position());
118  LogDebug("PixelDigitizer") << "B-field(T) at " << phase2det->surface().position()
119  << " (cm): " << pSetup_->inTesla(phase2det->surface().position());
120 
121  auto kiter = algomap_.find(getAlgoType(detId_raw));
122  if (kiter != algomap_.end())
123  kiter->second->accumulateSimHits(it, itEnd, globalSimHitIndex, tofBin, phase2det, bfield);
124  else
125  edm::LogInfo("Phase2TrackerDigitizer") << "Unsupported algorithm: ";
126  }
127  }
128  }
129  }
130 
133  if (!rng.isAvailable()) {
134  throw cms::Exception("Configuration")
135  << "Phase2TrackerDigitizer requires the RandomNumberGeneratorService\n"
136  "which is not present in the configuration file. You must add the service\n"
137  "in the configuration file or remove the modules that require it.";
138  }
139 
140  pSetup_ = &iSetup.getData(pSetupToken_);
141  tTopo_ = &iSetup.getData(tTopoToken_);
142 
143  if (theTkDigiGeomWatcher_.check(iSetup)) {
144  pDD_ = &iSetup.getData(pDDToken_);
145 
146  // reset cache
148  detectorUnits_.clear();
149  for (auto const& det_u : pDD_->detUnits()) {
150  uint32_t rawId = det_u->geographicalId().rawId();
151  if (DetId(rawId).det() == DetId::Detector::Tracker) {
152  const Phase2TrackerGeomDetUnit* pixdet = dynamic_cast<const Phase2TrackerGeomDetUnit*>(det_u);
153  assert(pixdet);
154  detectorUnits_.emplace(rawId, pixdet);
155  }
156  }
157  }
158 
159  // Must initialize all the algorithms
160  for (auto const& el : algomap_) {
161  if (first_)
162  el.second->init(iSetup);
163 
164  el.second->initializeEvent(rng->getEngine(e.streamID()));
165  }
166  first_ = false;
167  // Make sure that the first crossing processed starts indexing the sim hits from zero.
168  // This variable is used so that the sim hits from all crossing frames have sequential
169  // indices used to create the digi-sim link (if configured to do so) rather than starting
170  // from zero for each crossing.
172  }
174  accumulate_local<edm::Event>(iEvent, iSetup);
175  }
176 
178  edm::EventSetup const& iSetup,
179  edm::StreamID const&) {
180  accumulate_local<PileUpEventPrincipal>(iEvent, iSetup);
181  }
182 
183  template <class T>
185  for (auto const& v : trackerContainers_) {
188  iEvent.getByLabel(tag, simHits);
189 
190  //edm::EDGetTokenT< std::vector<PSimHit> > simHitToken_(consumes< std::vector<PSimHit>(tag));
191  //iEvent.getByToken(simHitToken_, simHits);
192 
193  uint32_t tofBin = PixelDigiSimLink::LowTof;
194  if (v.find(std::string("HighTof")) != std::string::npos)
195  tofBin = PixelDigiSimLink::HighTof;
197  // Now that the hits have been processed, I'll add the amount of hits in this crossing on to
198  // the global counter. Next time accumulateStripHits() is called it will count the sim hits
199  // as though they were on the end of this collection.
200  // Note that this is only used for creating digi-sim links (if configured to do so).
201  if (simHits.isValid())
202  crossingSimHitIndexOffset_[tag.encode()] += simHits->size();
203  }
204  }
205 
206  // For premixing
207  void Phase2TrackerDigitizer::loadAccumulator(const std::map<uint32_t, std::map<int, float> >& accumulator) {
208  for (const auto& detMap : accumulator) {
209  AlgorithmType algoType = getAlgoType(detMap.first);
210  auto& algo = *(algomap_.at(algoType));
211  algo.loadAccumulator(detMap.first, detMap.second);
212  }
213  }
214 
216  //Decide if we want analog readout for Outer Tracker.
219  if (premixStage1_)
220  addOuterTrackerCollection<PixelDigi>(iEvent, iSetup);
221  else
222  addOuterTrackerCollection<Phase2TrackerDigi>(iEvent, iSetup);
223  }
224  }
226  // get mType either from the geometry or from our cache (faster)
228  auto itr = moduleTypeCache_.find(detId_raw);
229  if (itr != moduleTypeCache_.end()) {
230  mType = itr->second;
231  } else {
232  mType = pDD_->getDetectorType(DetId(detId_raw));
233  moduleTypeCache_.emplace(detId_raw, mType);
234  }
235 
237  switch (mType) {
239  algotype = AlgorithmType::InnerPixel;
240  break;
242  algotype = AlgorithmType::InnerPixel;
243  break;
245  algotype = AlgorithmType::InnerPixel;
246  break;
248  algotype = AlgorithmType::InnerPixel;
249  break;
251  algotype = AlgorithmType::InnerPixel3D;
252  break;
254  algotype = AlgorithmType::InnerPixel3D;
255  break;
257  algotype = AlgorithmType::PixelinPS;
258  break;
260  algotype = AlgorithmType::StripinPS;
261  break;
263  algotype = AlgorithmType::TwoStrip;
264  break;
265  default:
266  edm::LogError("Phase2TrackerDigitizer") << "ERROR - Wrong Detector Type, No Algorithm available ";
267  }
268 
269  return algotype;
270  }
272  const edm::EventSetup& iSetup,
273  const bool ot_analog) {
274  std::vector<edm::DetSet<PixelDigi> > digiVector;
275  std::vector<edm::DetSet<PixelDigiSimLink> > digiLinkVector;
276  for (auto const& det_u : pDD_->detUnits()) {
277  uint32_t rawId = det_u->geographicalId().rawId();
278  auto algotype = getAlgoType(rawId);
279  auto fiter = algomap_.find(algotype);
280  if (fiter == algomap_.end())
281  continue;
282 
283  // Decide if we want analog readout for Outer Tracker.
284  if (!ot_analog && algotype != AlgorithmType::InnerPixel && algotype != AlgorithmType::InnerPixel3D)
285  continue;
286 
287  std::map<int, digitizerUtility::DigiSimInfo> digi_map;
288  fiter->second->digitize(dynamic_cast<const Phase2TrackerGeomDetUnit*>(det_u), digi_map, tTopo_);
289 
290  edm::DetSet<PixelDigi> collector(rawId);
291  edm::DetSet<PixelDigiSimLink> linkcollector(rawId);
292  for (auto const& digi_p : digi_map) {
293  digitizerUtility::DigiSimInfo info = digi_p.second;
294  const auto& ip = PixelDigi::channelToPixel(digi_p.first);
295  collector.data.emplace_back(ip.first, ip.second, info.sig_tot);
296  for (auto const& sim_p : info.simInfoList) {
297  linkcollector.data.emplace_back(digi_p.first,
298  sim_p.second->trackId(),
299  sim_p.second->hitIndex(),
300  sim_p.second->tofBin(),
301  sim_p.second->eventId(),
302  sim_p.first);
303  }
304  }
305  if (!collector.data.empty())
306  digiVector.push_back(std::move(collector));
307  if (!linkcollector.data.empty())
308  digiLinkVector.push_back(std::move(linkcollector));
309  }
310 
311  // Step C: create collection with the cache vector of DetSet
312  auto output = std::make_unique<edm::DetSetVector<PixelDigi> >(digiVector);
313  auto outputlink = std::make_unique<edm::DetSetVector<PixelDigiSimLink> >(digiLinkVector);
314 
315  // Step D: write output to file
316  iEvent.put(std::move(output), "Pixel");
317  if (makeDigiSimLinks_)
318  iEvent.put(std::move(outputlink), "Pixel");
319  }
320 } // namespace cms
321 namespace {
322  void addToCollector(edm::DetSet<PixelDigi>& collector, const int channel, const digitizerUtility::DigiSimInfo& info) {
323  // For premixing stage1 the channel must be decoded with PixelDigi
324  // so that when the row and column are inserted to PixelDigi the
325  // coded channel stays the same (so that it can then be decoded
326  // with Phase2TrackerDigi in stage2).
327  const auto& ip = PixelDigi::channelToPixel(channel);
328  collector.data.emplace_back(ip.first, ip.second, info.sig_tot);
329  }
330  void addToCollector(edm::DetSet<Phase2TrackerDigi>& collector,
331  const int channel,
333  const auto& ip = Phase2TrackerDigi::channelToPixel(channel);
334  collector.data.emplace_back(ip.first, ip.second, info.ot_bit);
335  }
336 } // namespace
337 namespace cms {
338  template <typename DigiType>
340  std::vector<edm::DetSet<DigiType> > digiVector;
341  std::vector<edm::DetSet<PixelDigiSimLink> > digiLinkVector;
342  for (auto const& det_u : pDD_->detUnits()) {
343  uint32_t rawId = det_u->geographicalId().rawId();
344  auto algotype = getAlgoType(rawId);
345 
346  auto fiter = algomap_.find(algotype);
347  if (fiter == algomap_.end() || algotype == AlgorithmType::InnerPixel || algotype == AlgorithmType::InnerPixel3D)
348  continue;
349 
350  std::map<int, digitizerUtility::DigiSimInfo> digi_map;
351  fiter->second->digitize(dynamic_cast<const Phase2TrackerGeomDetUnit*>(det_u), digi_map, tTopo_);
352 
353  edm::DetSet<DigiType> collector(rawId);
354  edm::DetSet<PixelDigiSimLink> linkcollector(rawId);
355  for (auto const& digi_p : digi_map) {
356  digitizerUtility::DigiSimInfo info = digi_p.second;
357  addToCollector(collector, digi_p.first, info);
358  for (auto const& sim_p : info.simInfoList) {
359  linkcollector.data.emplace_back(digi_p.first,
360  sim_p.second->trackId(),
361  sim_p.second->hitIndex(),
362  sim_p.second->tofBin(),
363  sim_p.second->eventId(),
364  sim_p.first);
365  }
366  }
367 
368  if (!collector.data.empty())
369  digiVector.push_back(std::move(collector));
370  if (!linkcollector.data.empty())
371  digiLinkVector.push_back(std::move(linkcollector));
372  }
373 
374  // Step C: create collection with the cache vector of DetSet
375  auto output = std::make_unique<edm::DetSetVector<DigiType> >(digiVector);
376  auto outputlink = std::make_unique<edm::DetSetVector<PixelDigiSimLink> >(digiLinkVector);
377 
378  // Step D: write output to file
379  iEvent.put(std::move(output), "Tracker");
380  if (makeDigiSimLinks_)
381  iEvent.put(std::move(outputlink), "Tracker");
382  }
383 } // namespace cms
384 
387 
edm::ESWatcher< TrackerDigiGeometryRecord > theTkDigiGeomWatcher_
Phase2TrackerDigitizer(const edm::ParameterSet &iConfig, edm::ProducesCollector, edm::ConsumesCollector &iC)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
static const TGPicture * info(bool iBackgroundIsBlack)
const TrackerTopology * tTopo_
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
void accumulate(edm::Event const &e, edm::EventSetup const &c) override
static std::pair< int, int > channelToPixel(int ch)
Definition: PixelDigi.h:69
void accumulatePixelHits(edm::Handle< std::vector< PSimHit > >, size_t globalSimHitIndex, const uint32_t tofBin)
void finalizeEvent(edm::Event &e, edm::EventSetup const &c) override
ProductRegistryHelper::BranchAliasSetterT< ProductType > produces()
AlgorithmType getAlgoType(uint32_t idet)
virtual GlobalVector inTesla(const GlobalPoint &gp) const =0
Field value ad specified global point, in Tesla.
void initializeEvent(edm::Event const &e, edm::EventSetup const &c) override
const edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > tTopoToken_
const DetContainer & detUnits() const override
Returm a vector of all GeomDet.
void accumulate_local(T const &iEvent, edm::EventSetup const &iSetup)
Log< level::Error, false > LogError
assert(be >=bs)
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &)=0
Use this engine in event methods.
int iEvent
Definition: GenABIO.cc:224
ModuleType getDetectorType(DetId) const
std::unordered_map< uint32_t, TrackerGeometry::ModuleType > ModuleTypeCache
void loadAccumulator(const std::map< uint32_t, std::map< int, float > > &accumulator)
Namespace of DDCMS conversion namespace.
Log< level::Info, false > LogInfo
std::map< uint32_t, const Phase2TrackerGeomDetUnit * > detectorUnits_
void addOuterTrackerCollection(edm::Event &iEvent, const edm::EventSetup &iSetup)
Definition: DetId.h:17
const Plane & surface() const
The nominal surface of the GeomDet.
Definition: GeomDet.h:37
const PositionType & position() const
const edm::ESGetToken< MagneticField, IdealMagneticFieldRecord > pSetupToken_
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:57
collection_type data
Definition: DetSet.h:80
std::map< AlgorithmType, std::unique_ptr< Phase2TrackerDigitizerAlgorithm > > algomap_
static std::pair< unsigned int, unsigned int > channelToPixel(unsigned int ch)
HLT enums.
#define DEFINE_DIGI_ACCUMULATOR(type)
const edm::ESGetToken< TrackerGeometry, TrackerDigiGeometryRecord > pDDToken_
void addPixelCollection(edm::Event &iEvent, const edm::EventSetup &iSetup, const bool ot_analog)
bool isAvailable() const
Definition: Service.h:40
Definition: output.py:1
long double T
def move(src, dest)
Definition: eostools.py:511
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. ...
#define LogDebug(id)