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 
28 
34 
38 
45 
48 
51 
53 
55 
56 // Random Number
59 
60 namespace cms {
61 
63  edm::ProducesCollector producesCollector,
65  : first_(true),
66  hitsProducer_(iConfig.getParameter<std::string>("hitsProducer")),
67  trackerContainers_(iConfig.getParameter<std::vector<std::string> >("ROUList")),
68  pDDToken_(iC.esConsumes(edm::ESInputTag("", iConfig.getParameter<std::string>("GeometryType")))),
69  pSetupToken_(iC.esConsumes()),
70  tTopoToken_(iC.esConsumes()),
71  isOuterTrackerReadoutAnalog_(iConfig.getParameter<bool>("isOTreadoutAnalog")),
72  premixStage1_(iConfig.getParameter<bool>("premixStage1")),
73  makeDigiSimLinks_(
74  iConfig.getParameter<edm::ParameterSet>("AlgorithmCommon").getUntrackedParameter<bool>("makeDigiSimLinks")) {
75  const std::string alias1("simSiPixelDigis");
76  producesCollector.produces<edm::DetSetVector<PixelDigi> >("Pixel").setBranchAlias(alias1);
78  producesCollector.produces<edm::DetSetVector<PixelDigiSimLink> >("Pixel").setBranchAlias(alias1);
79 
80  if (!iConfig.getParameter<bool>("isOTreadoutAnalog")) {
81  const std::string alias2("simSiTrackerDigis");
82  if (premixStage1_) {
83  // Premixing exploits the ADC field of PixelDigi to store the collected charge
84  // But we still want everything else to be treated like for Phase2TrackerDigi
85  producesCollector.produces<edm::DetSetVector<PixelDigi> >("Tracker").setBranchAlias(alias2);
86  } else {
87  producesCollector.produces<edm::DetSetVector<Phase2TrackerDigi> >("Tracker").setBranchAlias(alias2);
88  }
90  producesCollector.produces<edm::DetSetVector<PixelDigiSimLink> >("Tracker").setBranchAlias(alias2);
91  }
92  // creating algorithm objects and pushing them into the map
93  algomap_[AlgorithmType::InnerPixel] = std::make_unique<PixelDigitizerAlgorithm>(iConfig, iC);
94  algomap_[AlgorithmType::InnerPixelBricked] = std::make_unique<PixelBrickedDigitizerAlgorithm>(iConfig, iC);
95  algomap_[AlgorithmType::InnerPixel3D] = std::make_unique<Pixel3DDigitizerAlgorithm>(iConfig, iC);
96  algomap_[AlgorithmType::PixelinPS] = std::make_unique<PSPDigitizerAlgorithm>(iConfig, iC);
97  algomap_[AlgorithmType::StripinPS] = std::make_unique<PSSDigitizerAlgorithm>(iConfig, iC);
98  algomap_[AlgorithmType::TwoStrip] = std::make_unique<SSDigitizerAlgorithm>(iConfig, iC);
99  }
100 
102  void Phase2TrackerDigitizer::accumulatePixelHits(edm::Handle<std::vector<PSimHit> > hSimHits,
103  size_t globalSimHitIndex,
104  const uint32_t tofBin) {
105  if (hSimHits.isValid()) {
106  std::set<uint32_t> detIds;
107  auto const& simHits = *(hSimHits.product());
108  for (auto it = std::begin(simHits), itEnd = std::end(simHits); it != itEnd; ++it, ++globalSimHitIndex) {
109  uint32_t detId_raw = (*it).detUnitId();
110  auto fiter = detectorUnits_.find(detId_raw);
111  if (fiter == detectorUnits_.end())
112  continue;
113 
114  if (detIds.insert(detId_raw).second) {
115  // The insert succeeded, so this detector element has not yet been processed.
116  const Phase2TrackerGeomDetUnit* phase2det = fiter->second;
117 
118  // access to magnetic field in global coordinates
119  GlobalVector bfield = pSetup_->inTesla(phase2det->surface().position());
120  LogDebug("PixelDigitizer") << "B-field(T) at " << phase2det->surface().position()
121  << " (cm): " << pSetup_->inTesla(phase2det->surface().position());
122 
123  auto kiter = algomap_.find(getAlgoType(detId_raw));
124  if (kiter != algomap_.end())
125  kiter->second->accumulateSimHits(it, itEnd, globalSimHitIndex, tofBin, phase2det, bfield);
126  else
127  edm::LogInfo("Phase2TrackerDigitizer") << "Unsupported algorithm: ";
128  }
129  }
130  }
131  }
132 
135  if (!rng.isAvailable()) {
136  throw cms::Exception("Configuration")
137  << "Phase2TrackerDigitizer requires the RandomNumberGeneratorService\n"
138  "which is not present in the configuration file. You must add the service\n"
139  "in the configuration file or remove the modules that require it.";
140  }
141 
142  pSetup_ = &iSetup.getData(pSetupToken_);
143  tTopo_ = &iSetup.getData(tTopoToken_);
144 
145  if (theTkDigiGeomWatcher_.check(iSetup)) {
146  pDD_ = &iSetup.getData(pDDToken_);
147 
148  // reset cache
150  detectorUnits_.clear();
151  for (auto const& det_u : pDD_->detUnits()) {
152  uint32_t rawId = det_u->geographicalId().rawId();
153  if (DetId(rawId).det() == DetId::Detector::Tracker) {
154  const Phase2TrackerGeomDetUnit* pixdet = dynamic_cast<const Phase2TrackerGeomDetUnit*>(det_u);
155  assert(pixdet);
156  detectorUnits_.emplace(rawId, pixdet);
157  }
158  }
159  }
160 
161  // Must initialize all the algorithms
162  for (auto const& el : algomap_) {
163  if (first_)
164  el.second->init(iSetup);
165 
166  el.second->initializeEvent(rng->getEngine(e.streamID()));
167  }
168  first_ = false;
169  // Make sure that the first crossing processed starts indexing the sim hits from zero.
170  // This variable is used so that the sim hits from all crossing frames have sequential
171  // indices used to create the digi-sim link (if configured to do so) rather than starting
172  // from zero for each crossing.
174  }
176  accumulate_local<edm::Event>(iEvent, iSetup);
177  }
178 
180  edm::EventSetup const& iSetup,
181  edm::StreamID const&) {
182  accumulate_local<PileUpEventPrincipal>(iEvent, iSetup);
183  }
184 
185  template <class T>
187  for (auto const& v : trackerContainers_) {
190  iEvent.getByLabel(tag, simHits);
191 
192  //edm::EDGetTokenT< std::vector<PSimHit> > simHitToken_(consumes< std::vector<PSimHit>(tag));
193  //iEvent.getByToken(simHitToken_, simHits);
194 
195  uint32_t tofBin = PixelDigiSimLink::LowTof;
196  if (v.find(std::string("HighTof")) != std::string::npos)
197  tofBin = PixelDigiSimLink::HighTof;
199  // Now that the hits have been processed, I'll add the amount of hits in this crossing on to
200  // the global counter. Next time accumulateStripHits() is called it will count the sim hits
201  // as though they were on the end of this collection.
202  // Note that this is only used for creating digi-sim links (if configured to do so).
203  if (simHits.isValid())
204  crossingSimHitIndexOffset_[tag.encode()] += simHits->size();
205  }
206  }
207 
208  // For premixing
209  void Phase2TrackerDigitizer::loadAccumulator(const std::map<uint32_t, std::map<int, float> >& accumulator) {
210  for (const auto& detMap : accumulator) {
211  AlgorithmType algoType = getAlgoType(detMap.first);
212  auto& algo = *(algomap_.at(algoType));
213  algo.loadAccumulator(detMap.first, detMap.second);
214  }
215  }
216 
218  //Decide if we want analog readout for Outer Tracker.
221  if (premixStage1_)
222  addOuterTrackerCollection<PixelDigi>(iEvent, iSetup);
223  else
224  addOuterTrackerCollection<Phase2TrackerDigi>(iEvent, iSetup);
225  }
226  }
228  // get mType either from the geometry or from our cache (faster)
230  auto itr = moduleTypeCache_.find(detId_raw);
231  if (itr != moduleTypeCache_.end()) {
232  mType = itr->second;
233  } else {
234  mType = pDD_->getDetectorType(DetId(detId_raw));
235  moduleTypeCache_.emplace(detId_raw, mType);
236  }
237 
238  auto detUnit = detectorUnits_.find(detId_raw);
239  const Phase2TrackerGeomDetUnit* pixdet = dynamic_cast<const Phase2TrackerGeomDetUnit*>(detUnit->second);
240  const Phase2TrackerTopology* topol = &pixdet->specificTopology();
242  switch (mType) {
244  algotype = AlgorithmType::InnerPixel;
245  break;
247  algotype = AlgorithmType::InnerPixel;
248  break;
250  if (topol->isBricked())
252  else
253  algotype = AlgorithmType::InnerPixel;
254  break;
256  if (topol->isBricked())
258  else
259  algotype = AlgorithmType::InnerPixel;
260  break;
262  algotype = AlgorithmType::InnerPixel3D;
263  break;
265  algotype = AlgorithmType::InnerPixel3D;
266  break;
268  algotype = AlgorithmType::PixelinPS;
269  break;
271  algotype = AlgorithmType::StripinPS;
272  break;
274  algotype = AlgorithmType::TwoStrip;
275  break;
276  default:
277  edm::LogError("Phase2TrackerDigitizer") << "ERROR - Wrong Detector Type, No Algorithm available ";
278  }
279 
280  return algotype;
281  }
283  const edm::EventSetup& iSetup,
284  const bool ot_analog) {
285  std::vector<edm::DetSet<PixelDigi> > digiVector;
286  std::vector<edm::DetSet<PixelDigiSimLink> > digiLinkVector;
287  for (auto const& det_u : pDD_->detUnits()) {
288  uint32_t rawId = det_u->geographicalId().rawId();
289  auto algotype = getAlgoType(rawId);
290  auto fiter = algomap_.find(algotype);
291  if (fiter == algomap_.end())
292  continue;
293 
294  // Decide if we want analog readout for Outer Tracker.
295  if (!ot_analog && algotype != AlgorithmType::InnerPixel && algotype != AlgorithmType::InnerPixel3D &&
297  continue;
298 
299  std::map<int, DigitizerUtility::DigiSimInfo> digi_map;
300  fiter->second->digitize(dynamic_cast<const Phase2TrackerGeomDetUnit*>(det_u), digi_map, tTopo_);
301 
302  edm::DetSet<PixelDigi> collector(rawId);
303  edm::DetSet<PixelDigiSimLink> linkcollector(rawId);
304  for (auto const& digi_p : digi_map) {
305  DigitizerUtility::DigiSimInfo info = digi_p.second;
306  const auto& ip = PixelDigi::channelToPixel(digi_p.first);
307  collector.data.emplace_back(ip.first, ip.second, info.sig_tot);
308  for (auto const& sim_p : info.simInfoList) {
309  linkcollector.data.emplace_back(digi_p.first,
310  sim_p.second->trackId(),
311  sim_p.second->hitIndex(),
312  sim_p.second->tofBin(),
313  sim_p.second->eventId(),
314  sim_p.first);
315  }
316  }
317  if (!collector.data.empty())
318  digiVector.push_back(std::move(collector));
319  if (!linkcollector.data.empty())
320  digiLinkVector.push_back(std::move(linkcollector));
321  }
322 
323  // Step C: create collection with the cache vector of DetSet
324  auto output = std::make_unique<edm::DetSetVector<PixelDigi> >(digiVector);
325  auto outputlink = std::make_unique<edm::DetSetVector<PixelDigiSimLink> >(digiLinkVector);
326 
327  // Step D: write output to file
328  iEvent.put(std::move(output), "Pixel");
329  if (makeDigiSimLinks_)
330  iEvent.put(std::move(outputlink), "Pixel");
331  }
332 } // namespace cms
333 namespace {
334  void addToCollector(edm::DetSet<PixelDigi>& collector, const int channel, const DigitizerUtility::DigiSimInfo& info) {
335  // For premixing stage1 the channel must be decoded with PixelDigi
336  // so that when the row and column are inserted to PixelDigi the
337  // coded channel stays the same (so that it can then be decoded
338  // with Phase2TrackerDigi in stage2).
339  const auto& ip = PixelDigi::channelToPixel(channel);
340  collector.data.emplace_back(ip.first, ip.second, info.sig_tot);
341  }
342  void addToCollector(edm::DetSet<Phase2TrackerDigi>& collector,
343  const int channel,
345  const auto& ip = Phase2TrackerDigi::channelToPixel(channel);
346  collector.data.emplace_back(ip.first, ip.second, info.ot_bit);
347  }
348 } // namespace
349 namespace cms {
350  template <typename DigiType>
352  std::vector<edm::DetSet<DigiType> > digiVector;
353  std::vector<edm::DetSet<PixelDigiSimLink> > digiLinkVector;
354  for (auto const& det_u : pDD_->detUnits()) {
355  uint32_t rawId = det_u->geographicalId().rawId();
356  auto algotype = getAlgoType(rawId);
357 
358  auto fiter = algomap_.find(algotype);
359  if (fiter == algomap_.end() || algotype == AlgorithmType::InnerPixel || algotype == AlgorithmType::InnerPixel3D ||
361  continue;
362 
363  std::map<int, DigitizerUtility::DigiSimInfo> digi_map;
364  fiter->second->digitize(dynamic_cast<const Phase2TrackerGeomDetUnit*>(det_u), digi_map, tTopo_);
365 
366  edm::DetSet<DigiType> collector(rawId);
367  edm::DetSet<PixelDigiSimLink> linkcollector(rawId);
368  for (auto const& digi_p : digi_map) {
369  DigitizerUtility::DigiSimInfo info = digi_p.second;
370  addToCollector(collector, digi_p.first, info);
371  for (auto const& sim_p : info.simInfoList) {
372  linkcollector.data.emplace_back(digi_p.first,
373  sim_p.second->trackId(),
374  sim_p.second->hitIndex(),
375  sim_p.second->tofBin(),
376  sim_p.second->eventId(),
377  sim_p.first);
378  }
379  }
380 
381  if (!collector.data.empty())
382  digiVector.push_back(std::move(collector));
383  if (!linkcollector.data.empty())
384  digiLinkVector.push_back(std::move(linkcollector));
385  }
386 
387  // Step C: create collection with the cache vector of DetSet
388  auto output = std::make_unique<edm::DetSetVector<DigiType> >(digiVector);
389  auto outputlink = std::make_unique<edm::DetSetVector<PixelDigiSimLink> >(digiLinkVector);
390 
391  // Step D: write output to file
392  iEvent.put(std::move(output), "Tracker");
393  if (makeDigiSimLinks_)
394  iEvent.put(std::move(outputlink), "Tracker");
395  }
396 } // namespace cms
397 
400 
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_
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
virtual bool isBricked() const =0
ModuleType getDetectorType(DetId) const
bool getData(T &iHolder) const
Definition: EventSetup.h:122
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)
virtual const PixelTopology & specificTopology() const
Returns a reference to the pixel proxy topology.
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
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)