CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SiPixelDigitizer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: SiPixelDigitizer
4 // Class: SiPixelDigitizer
5 //
13 //
14 // Original Author: Michele Pioppi-INFN perugia
15 // Modifications: Freya Blekman - Cornell University
16 // Created: Mon Sep 26 11:08:32 CEST 2005
17 //
18 //
19 
20 
21 // system include files
22 #include <memory>
23 #include <set>
24 
25 // user include files
26 #include "SiPixelDigitizer.h"
28 
49 
52 
55 // user include files
58 
61 
64 
67 
68 //Random Number
73 
74 namespace CLHEP {
75  class HepRandomEngine;
76 }
77 
78 
79 //
80 // constants, enums and typedefs
81 //
82 
83 //
84 // static data member definitions
85 //
86 
87 //
88 // constructors and destructor
89 //
90 //using namespace std;
91 
92 
93 namespace cms
94 {
96  first(true),
97  _pixeldigialgo(),
98  hitsProducer(iConfig.getParameter<std::string>("hitsProducer")),
99  trackerContainers(iConfig.getParameter<std::vector<std::string> >("ROUList")),
100  geometryType(iConfig.getParameter<std::string>("GeometryType")),
101  pilotBlades(iConfig.exists("enablePilotBlades")?iConfig.getParameter<bool>("enablePilotBlades"):false),
102  NumberOfEndcapDisks(iConfig.exists("NumPixelEndcap")?iConfig.getParameter<int>("NumPixelEndcap"):2)
103  {
104  edm::LogInfo ("PixelDigitizer ") <<"Enter the Pixel Digitizer";
105 
106  const std::string alias ("simSiPixelDigis");
107 
108  mixMod.produces<edm::DetSetVector<PixelDigi> >().setBranchAlias(alias);
109  mixMod.produces<edm::DetSetVector<PixelDigiSimLink> >().setBranchAlias(alias + "siPixelDigiSimLink");
110  for(auto const& trackerContainer : trackerContainers) {
111  edm::InputTag tag(hitsProducer, trackerContainer);
112  iC.consumes<std::vector<PSimHit> >(edm::InputTag(hitsProducer, trackerContainer));
113  }
115  if ( ! rng.isAvailable()) {
116  throw cms::Exception("Configuration")
117  << "SiPixelDigitizer requires the RandomNumberGeneratorService\n"
118  "which is not present in the configuration file. You must add the service\n"
119  "in the configuration file or remove the modules that require it.";
120  }
121 
122  _pixeldigialgo.reset(new SiPixelDigitizerAlgorithm(iConfig));
123  }
124 
126  edm::LogInfo ("PixelDigitizer ") <<"Destruct the Pixel Digitizer";
127  }
128 
129 
130  //
131  // member functions
132  //
133 
134  void
135  SiPixelDigitizer::accumulatePixelHits(edm::Handle<std::vector<PSimHit> > hSimHits,
136  size_t globalSimHitIndex,
137  const unsigned int tofBin,
138  CLHEP::HepRandomEngine* engine,
139  edm::EventSetup const& iSetup) {
140  if(hSimHits.isValid()) {
141  std::set<unsigned int> detIds;
142  std::vector<PSimHit> const& simHits = *hSimHits.product();
144  iSetup.get<IdealGeometryRecord>().get(tTopoHand);
145  const TrackerTopology *tTopo=tTopoHand.product();
146  for(std::vector<PSimHit>::const_iterator it = simHits.begin(), itEnd = simHits.end(); it != itEnd; ++it, ++globalSimHitIndex) {
147  unsigned int detId = (*it).detUnitId();
148  if(detIds.insert(detId).second) {
149  // The insert succeeded, so this detector element has not yet been processed.
150  unsigned int isub = DetId(detId).subdetId();
152  std::map<unsigned int, PixelGeomDetUnit const *>::iterator itDet = detectorUnits.find(detId);
153  if (itDet == detectorUnits.end()) continue;
154  auto pixdet = itDet->second;
155  //access to magnetic field in global coordinates
156  GlobalVector bfield = pSetup->inTesla(pixdet->surface().position());
157  LogDebug ("PixelDigitizer ") << "B-field(T) at " << pixdet->surface().position() << "(cm): "
158  << pSetup->inTesla(pixdet->surface().position());
159  _pixeldigialgo->accumulateSimHits(it, itEnd, globalSimHitIndex, tofBin, pixdet, bfield, tTopo, engine);
160  }
161  }
162  }
163  }
164  }
165 
166  void
168  if(first){
169  _pixeldigialgo->init(iSetup);
170  first = false;
171  }
172  // Make sure that the first crossing processed starts indexing the sim hits from zero.
173  // This variable is used so that the sim hits from all crossing frames have sequential
174  // indices used to create the digi-sim link (if configured to do so) rather than starting
175  // from zero for each crossing.
177 
178  _pixeldigialgo->initializeEvent();
180  iSetup.get<IdealMagneticFieldRecord>().get(pSetup);
182  iSetup.get<IdealGeometryRecord>().get(tTopoHand);
183  const TrackerTopology *tTopo=tTopoHand.product();
184 
185  // FIX THIS! We only need to clear and (re)fill this map when the geometry type IOV changes. Use ESWatcher to determine this.
186  if(true) { // Replace with ESWatcher
187  detectorUnits.clear();
188  for(TrackingGeometry::DetUnitContainer::const_iterator iu = pDD->detUnits().begin(); iu != pDD->detUnits().end(); ++iu) {
189  unsigned int detId = (*iu)->geographicalId().rawId();
190  DetId idet=DetId(detId);
191  unsigned int isub=idet.subdetId();
193  auto pixdet = dynamic_cast<const PixelGeomDetUnit*>((*iu));
194  assert(pixdet != 0);
195  if (isub==PixelSubdetector::PixelEndcap) {
196  unsigned int disk = tTopo->pxfDisk(detId);
197  //if using pilot blades, then allowing it for current detector only
198  if ((disk == 3)&&((!pilotBlades)&&(NumberOfEndcapDisks == 2))) continue;
199  }
200  detectorUnits.insert(std::make_pair(detId, pixdet));
201  }
202  }
203  }
204  }
205 
206  void
208  // Step A: Get Inputs
209  for(vstring::const_iterator i = trackerContainers.begin(), iEnd = trackerContainers.end(); i != iEnd; ++i) {
212 
213  iEvent.getByLabel(tag, simHits);
214  unsigned int tofBin = PixelDigiSimLink::LowTof;
215  if ((*i).find(std::string("HighTof")) != std::string::npos) tofBin = PixelDigiSimLink::HighTof;
216  accumulatePixelHits(simHits, crossingSimHitIndexOffset_[tag.encode()], tofBin, randomEngine(iEvent.streamID()), iSetup);
217  // Now that the hits have been processed, I'll add the amount of hits in this crossing on to
218  // the global counter. Next time accumulateStripHits() is called it will count the sim hits
219  // as though they were on the end of this collection.
220  // Note that this is only used for creating digi-sim links (if configured to do so).
221 // std::cout << "index offset, current hit count = " << crossingSimHitIndexOffset_[tag.encode()] << ", " << simHits->size() << std::endl;
222  if( simHits.isValid() ) crossingSimHitIndexOffset_[tag.encode()]+=simHits->size();
223  }
224  }
225 
226  void
228  // Step A: Get Inputs
229  for(vstring::const_iterator i = trackerContainers.begin(), iEnd = trackerContainers.end(); i != iEnd; ++i) {
232 
233  iEvent.getByLabel(tag, simHits);
234  unsigned int tofBin = PixelDigiSimLink::LowTof;
235  if ((*i).find(std::string("HighTof")) != std::string::npos) tofBin = PixelDigiSimLink::HighTof;
236  accumulatePixelHits(simHits, crossingSimHitIndexOffset_[tag.encode()], tofBin, randomEngine(streamID), iSetup);
237  // Now that the hits have been processed, I'll add the amount of hits in this crossing on to
238  // the global counter. Next time accumulateStripHits() is called it will count the sim hits
239  // as though they were on the end of this collection.
240  // Note that this is only used for creating digi-sim links (if configured to do so).
241 // std::cout << "index offset, current hit count = " << crossingSimHitIndexOffset_[tag.encode()] << ", " << simHits->size() << std::endl;
242  if( simHits.isValid() ) crossingSimHitIndexOffset_[tag.encode()]+=simHits->size();
243  }
244  }
245 
246  // ------------ method called to produce the data ------------
247  void
249 
251  CLHEP::HepRandomEngine* engine = &rng->getEngine(iEvent.streamID());
252 
254  iSetup.get<IdealGeometryRecord>().get(tTopoHand);
255  const TrackerTopology *tTopo=tTopoHand.product();
256 
257  std::vector<edm::DetSet<PixelDigi> > theDigiVector;
258  std::vector<edm::DetSet<PixelDigiSimLink> > theDigiLinkVector;
259 
261  _pixeldigialgo->calculateInstlumiFactor(PileupInfo_);
262 
263  for(TrackingGeometry::DetUnitContainer::const_iterator iu = pDD->detUnits().begin(); iu != pDD->detUnits().end(); iu ++){
264  DetId idet=DetId((*iu)->geographicalId().rawId());
265  unsigned int isub=idet.subdetId();
266 
268 
269  //
270 
271  edm::DetSet<PixelDigi> collector((*iu)->geographicalId().rawId());
272  edm::DetSet<PixelDigiSimLink> linkcollector((*iu)->geographicalId().rawId());
273 
274 
275  _pixeldigialgo->digitize(dynamic_cast<const PixelGeomDetUnit*>((*iu)),
276  collector.data,
277  linkcollector.data,
278  tTopo,
279  engine);
280  if(collector.data.size() > 0) {
281  theDigiVector.push_back(std::move(collector));
282  }
283  if(linkcollector.data.size() > 0) {
284  theDigiLinkVector.push_back(std::move(linkcollector));
285  }
286  }
287  }
288 
289  // Step C: create collection with the cache vector of DetSet
290  std::auto_ptr<edm::DetSetVector<PixelDigi> >
291  output(new edm::DetSetVector<PixelDigi>(theDigiVector) );
292  std::auto_ptr<edm::DetSetVector<PixelDigiSimLink> >
293  outputlink(new edm::DetSetVector<PixelDigiSimLink>(theDigiLinkVector) );
294 
295  // Step D: write output to file
296  iEvent.put(output);
297  iEvent.put(outputlink);
298  }
299 
300  CLHEP::HepRandomEngine* SiPixelDigitizer::randomEngine(edm::StreamID const& streamID) {
301  unsigned int index = streamID.value();
302  if(index >= randomEngines_.size()) {
303  randomEngines_.resize(index + 1, nullptr);
304  }
305  CLHEP::HepRandomEngine* ptr = randomEngines_[index];
306  if(!ptr) {
308  ptr = &rng->getEngine(streamID);
309  randomEngines_[index] = ptr;
310  }
311  return ptr;
312  }
313 
314 }// end namespace cms::
315 
#define LogDebug(id)
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
virtual void finalizeEvent(edm::Event &e, edm::EventSetup const &c) override
int i
Definition: DBlmapReader.cc:9
CLHEP::HepRandomEngine * randomEngine(edm::StreamID const &streamID)
std::vector< CLHEP::HepRandomEngine * > randomEngines_
unsigned int pxfDisk(const DetId &id) const
std::map< unsigned int, PixelGeomDetUnit const * > detectorUnits
edm::ESHandle< MagneticField > pSetup
std::string encode() const
Definition: InputTag.cc:164
SiPixelDigitizer(const edm::ParameterSet &conf, edm::one::EDProducerBase &mixMod, edm::ConsumesCollector &iC)
const std::string hitsProducer
int iEvent
Definition: GenABIO.cc:230
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. ...
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:113
virtual void initializeEvent(edm::Event const &e, edm::EventSetup const &c) override
void accumulatePixelHits(edm::Handle< std::vector< PSimHit > >, size_t globalSimHitIndex, const unsigned int tofBin, CLHEP::HepRandomEngine *, edm::EventSetup const &c)
bool isAvailable() const
Definition: Service.h:46
edm::ESHandle< TrackerGeometry > pDD
bool first
Definition: L1TdeRCT.cc:75
bool isValid() const
Definition: HandleBase.h:76
int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:37
std::unique_ptr< SiPixelDigitizerAlgorithm > _pixeldigialgo
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:402
virtual void accumulate(edm::Event const &e, edm::EventSetup const &c) override
Definition: DetId.h:18
unsigned int value() const
Definition: StreamID.h:46
const T & get() const
Definition: EventSetup.h:55
T const * product() const
Definition: ESHandle.h:86
tuple simHits
Definition: trackerHits.py:16
const std::string geometryType
PileupMixingContent * PileupInfo_
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &) const =0
Use this engine in event methods.
virtual PileupMixingContent * getEventPileupInfo()
const vstring trackerContainers
bool getByLabel(edm::InputTag const &tag, edm::Handle< T > &result) const
StreamID streamID() const
Definition: Event.h:72
volatile std::atomic< bool > shutdown_flag false