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 
54 // user include files
57 
60 
63 
66 
67 //Random Number
72 
73 namespace CLHEP {
74  class HepRandomEngine;
75 }
76 
77 
78 //
79 // constants, enums and typedefs
80 //
81 
82 //
83 // static data member definitions
84 //
85 
86 //
87 // constructors and destructor
88 //
89 //using namespace std;
90 
91 
92 namespace cms
93 {
95  first(true),
96  _pixeldigialgo(),
97  hitsProducer(iConfig.getParameter<std::string>("hitsProducer")),
98  trackerContainers(iConfig.getParameter<std::vector<std::string> >("RoutList")),
99  geometryType(iConfig.getParameter<std::string>("PixGeometryType")),
100  pilotBlades(iConfig.exists("enablePilotBlades")?iConfig.getParameter<bool>("enablePilotBlades"):false),
101  NumberOfEndcapDisks(iConfig.exists("NumPixelEndcap")?iConfig.getParameter<int>("NumPixelEndcap"):2)
102  {
103  edm::LogInfo ("PixelDigitizer ") <<"Enter the Pixel Digitizer";
104 
105  const std::string alias ("simSiPixelDigis");
106 
107  mixMod.produces<edm::DetSetVector<PixelDigi> >().setBranchAlias(alias);
108  mixMod.produces<edm::DetSetVector<PixelDigiSimLink> >().setBranchAlias(alias + "siPixelDigiSimLink");
109  for(auto const& trackerContainer : trackerContainers) {
110  edm::InputTag tag(hitsProducer, trackerContainer);
111  iC.consumes<std::vector<PSimHit> >(edm::InputTag(hitsProducer, trackerContainer));
112  }
114  if ( ! rng.isAvailable()) {
115  throw cms::Exception("Configuration")
116  << "SiPixelDigitizer requires the RandomNumberGeneratorService\n"
117  "which is not present in the configuration file. You must add the service\n"
118  "in the configuration file or remove the modules that require it.";
119  }
120 
121  _pixeldigialgo.reset(new SiPixelDigitizerAlgorithm(iConfig));
122  }
123 
125  edm::LogInfo ("PixelDigitizer ") <<"Destruct the Pixel Digitizer";
126  }
127 
128 
129  //
130  // member functions
131  //
132 
133  void
134  SiPixelDigitizer::accumulatePixelHits(edm::Handle<std::vector<PSimHit> > hSimHits,
135  size_t globalSimHitIndex,
136  const unsigned int tofBin,
137  CLHEP::HepRandomEngine* engine,
138  edm::EventSetup const& iSetup) {
139  if(hSimHits.isValid()) {
140  std::set<unsigned int> detIds;
141  std::vector<PSimHit> const& simHits = *hSimHits.product();
143  iSetup.get<IdealGeometryRecord>().get(tTopoHand);
144  const TrackerTopology *tTopo=tTopoHand.product();
145  for(std::vector<PSimHit>::const_iterator it = simHits.begin(), itEnd = simHits.end(); it != itEnd; ++it, ++globalSimHitIndex) {
146  unsigned int detId = (*it).detUnitId();
147  if(detIds.insert(detId).second) {
148  // The insert succeeded, so this detector element has not yet been processed.
149  assert(detectorUnits[detId]);
150  if(detectorUnits[detId] && detectorUnits[detId]->type().isTrackerPixel()) { // this test could be avoided and changed into a check of pixdet!=0
151  std::map<unsigned int, PixelGeomDetUnit const *>::iterator itDet = detectorUnits.find(detId);
152  if (itDet == detectorUnits.end()) continue;
153  auto pixdet = itDet->second;
154  assert(pixdet !=0);
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  if((*iu)->type().isTrackerPixel()) {
191  auto pixdet = dynamic_cast<const PixelGeomDetUnit*>((*iu));
192  assert(pixdet != 0);
193  if ((*iu)->subDetector()==GeomDetEnumerators::SubDetector::PixelEndcap) { // true ONLY for the phase 0 pixel deetctor
194  unsigned int disk = tTopo->layer(detId); // using the generic layer method
195  //if using pilot blades, then allowing it for current detector only
196  if ((disk == 3)&&((!pilotBlades)&&(NumberOfEndcapDisks == 2))) continue;
197  }
198  detectorUnits.insert(std::make_pair(detId, pixdet));
199  }
200  }
201  }
202  }
203 
204  void
206  // Step A: Get Inputs
207  for(vstring::const_iterator i = trackerContainers.begin(), iEnd = trackerContainers.end(); i != iEnd; ++i) {
210 
211  iEvent.getByLabel(tag, simHits);
212  unsigned int tofBin = PixelDigiSimLink::LowTof;
213  if ((*i).find(std::string("HighTof")) != std::string::npos) tofBin = PixelDigiSimLink::HighTof;
214  accumulatePixelHits(simHits, crossingSimHitIndexOffset_[tag.encode()], tofBin, randomEngine(iEvent.streamID()), iSetup);
215  // Now that the hits have been processed, I'll add the amount of hits in this crossing on to
216  // the global counter. Next time accumulateStripHits() is called it will count the sim hits
217  // as though they were on the end of this collection.
218  // Note that this is only used for creating digi-sim links (if configured to do so).
219 // std::cout << "index offset, current hit count = " << crossingSimHitIndexOffset_[tag.encode()] << ", " << simHits->size() << std::endl;
220  if( simHits.isValid() ) crossingSimHitIndexOffset_[tag.encode()]+=simHits->size();
221  }
222  }
223 
224  void
226  // Step A: Get Inputs
227  for(vstring::const_iterator i = trackerContainers.begin(), iEnd = trackerContainers.end(); i != iEnd; ++i) {
230 
231  iEvent.getByLabel(tag, simHits);
232  unsigned int tofBin = PixelDigiSimLink::LowTof;
233  if ((*i).find(std::string("HighTof")) != std::string::npos) tofBin = PixelDigiSimLink::HighTof;
234  accumulatePixelHits(simHits, crossingSimHitIndexOffset_[tag.encode()], tofBin, randomEngine(streamID), iSetup);
235  // Now that the hits have been processed, I'll add the amount of hits in this crossing on to
236  // the global counter. Next time accumulateStripHits() is called it will count the sim hits
237  // as though they were on the end of this collection.
238  // Note that this is only used for creating digi-sim links (if configured to do so).
239 // std::cout << "index offset, current hit count = " << crossingSimHitIndexOffset_[tag.encode()] << ", " << simHits->size() << std::endl;
240  if( simHits.isValid() ) crossingSimHitIndexOffset_[tag.encode()]+=simHits->size();
241  }
242  }
243 
244  // ------------ method called to produce the data ------------
245  void
247 
249  CLHEP::HepRandomEngine* engine = &rng->getEngine(iEvent.streamID());
250 
252  iSetup.get<IdealGeometryRecord>().get(tTopoHand);
253  const TrackerTopology *tTopo=tTopoHand.product();
254 
255  std::vector<edm::DetSet<PixelDigi> > theDigiVector;
256  std::vector<edm::DetSet<PixelDigiSimLink> > theDigiLinkVector;
257 
259  _pixeldigialgo->calculateInstlumiFactor(PileupInfo_);
260 
261  for(TrackingGeometry::DetUnitContainer::const_iterator iu = pDD->detUnits().begin(); iu != pDD->detUnits().end(); iu ++){
262 
263  if((*iu)->type().isTrackerPixel()) {
264 
265  //
266 
267  edm::DetSet<PixelDigi> collector((*iu)->geographicalId().rawId());
268  edm::DetSet<PixelDigiSimLink> linkcollector((*iu)->geographicalId().rawId());
269 
270 
271  _pixeldigialgo->digitize(dynamic_cast<const PixelGeomDetUnit*>((*iu)),
272  collector.data,
273  linkcollector.data,
274  tTopo,
275  engine);
276  if(collector.data.size() > 0) {
277  theDigiVector.push_back(std::move(collector));
278  }
279  if(linkcollector.data.size() > 0) {
280  theDigiLinkVector.push_back(std::move(linkcollector));
281  }
282  }
283  }
284 
285  // Step C: create collection with the cache vector of DetSet
286  std::auto_ptr<edm::DetSetVector<PixelDigi> >
287  output(new edm::DetSetVector<PixelDigi>(theDigiVector) );
288  std::auto_ptr<edm::DetSetVector<PixelDigiSimLink> >
289  outputlink(new edm::DetSetVector<PixelDigiSimLink>(theDigiLinkVector) );
290 
291  // Step D: write output to file
292  iEvent.put(output);
293  iEvent.put(outputlink);
294  }
295 
296  CLHEP::HepRandomEngine* SiPixelDigitizer::randomEngine(edm::StreamID const& streamID) {
297  unsigned int index = streamID.value();
298  if(index >= randomEngines_.size()) {
299  randomEngines_.resize(index + 1, nullptr);
300  }
301  CLHEP::HepRandomEngine* ptr = randomEngines_[index];
302  if(!ptr) {
304  ptr = &rng->getEngine(streamID);
305  randomEngines_[index] = ptr;
306  }
307  return ptr;
308  }
309 
310 }// end namespace cms::
311 
#define LogDebug(id)
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
type
Definition: HCALResponse.h:21
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_
assert(m_qm.get())
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
def move
Definition: eostools.py:508
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 isValid() const
Definition: HandleBase.h:75
std::unique_ptr< SiPixelDigitizerAlgorithm > _pixeldigialgo
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:405
virtual void accumulate(edm::Event const &e, edm::EventSetup const &c) override
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
bool isTrackerPixel(const GeomDetEnumerators::SubDetector m)
PileupMixingContent * PileupInfo_
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &) const =0
Use this engine in event methods.
unsigned int layer(const DetId &id) const
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