CMS 3D CMS Logo

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 //
74 // constants, enums and typedefs
75 //
76 
77 //
78 // static data member definitions
79 //
80 
81 //
82 // constructors and destructor
83 //
84 //using namespace std;
85 
86 
87 namespace cms
88 {
90  firstInitializeEvent_(true),
91  firstFinalizeEvent_(true),
92  _pixeldigialgo(),
93  hitsProducer(iConfig.getParameter<std::string>("hitsProducer")),
94  trackerContainers(iConfig.getParameter<std::vector<std::string> >("RoutList")),
95  geometryType(iConfig.getParameter<std::string>("PixGeometryType")),
96  pilotBlades(iConfig.exists("enablePilotBlades")?iConfig.getParameter<bool>("enablePilotBlades"):false),
97  NumberOfEndcapDisks(iConfig.exists("NumPixelEndcap")?iConfig.getParameter<int>("NumPixelEndcap"):2)
98  {
99  edm::LogInfo ("PixelDigitizer ") <<"Enter the Pixel Digitizer";
100 
101  const std::string alias ("simSiPixelDigis");
102 
103  mixMod.produces<edm::DetSetVector<PixelDigi> >().setBranchAlias(alias);
104  mixMod.produces<edm::DetSetVector<PixelDigiSimLink> >().setBranchAlias(alias + "siPixelDigiSimLink");
105  for(auto const& trackerContainer : trackerContainers) {
106  edm::InputTag tag(hitsProducer, trackerContainer);
107  iC.consumes<std::vector<PSimHit> >(edm::InputTag(hitsProducer, trackerContainer));
108  }
110  if ( ! rng.isAvailable()) {
111  throw cms::Exception("Configuration")
112  << "SiPixelDigitizer requires the RandomNumberGeneratorService\n"
113  "which is not present in the configuration file. You must add the service\n"
114  "in the configuration file or remove the modules that require it.";
115  }
116 
117  _pixeldigialgo.reset(new SiPixelDigitizerAlgorithm(iConfig));
118  }
119 
121  edm::LogInfo ("PixelDigitizer ") <<"Destruct the Pixel Digitizer";
122  }
123 
124 
125  //
126  // member functions
127  //
128 
129  void
130  SiPixelDigitizer::accumulatePixelHits(edm::Handle<std::vector<PSimHit> > hSimHits,
131  size_t globalSimHitIndex,
132  const unsigned int tofBin,
133  edm::EventSetup const& iSetup) {
134  if(hSimHits.isValid()) {
135  std::set<unsigned int> detIds;
136  std::vector<PSimHit> const& simHits = *hSimHits.product();
138  iSetup.get<TrackerTopologyRcd>().get(tTopoHand);
139  const TrackerTopology *tTopo=tTopoHand.product();
140  for(std::vector<PSimHit>::const_iterator it = simHits.begin(), itEnd = simHits.end(); it != itEnd; ++it, ++globalSimHitIndex) {
141  unsigned int detId = (*it).detUnitId();
142  if(detIds.insert(detId).second) {
143  // The insert succeeded, so this detector element has not yet been processed.
144  assert(detectorUnits[detId]);
145  if(detectorUnits[detId] && detectorUnits[detId]->type().isTrackerPixel()) { // this test could be avoided and changed into a check of pixdet!=0
146  std::map<unsigned int, PixelGeomDetUnit const *>::iterator itDet = detectorUnits.find(detId);
147  if (itDet == detectorUnits.end()) continue;
148  auto pixdet = itDet->second;
149  assert(pixdet !=nullptr);
150  //access to magnetic field in global coordinates
151  GlobalVector bfield = pSetup->inTesla(pixdet->surface().position());
152  LogDebug ("PixelDigitizer ") << "B-field(T) at " << pixdet->surface().position() << "(cm): "
153  << pSetup->inTesla(pixdet->surface().position());
154  _pixeldigialgo->accumulateSimHits(it, itEnd, globalSimHitIndex, tofBin, pixdet, bfield, tTopo, randomEngine_);
155  }
156  }
157  }
158  }
159  }
160 
161  void
164  _pixeldigialgo->init(iSetup);
165  firstInitializeEvent_ = false;
166  }
167 
168  // Make sure that the first crossing processed starts indexing the sim hits from zero.
169  // This variable is used so that the sim hits from all crossing frames have sequential
170  // indices used to create the digi-sim link (if configured to do so) rather than starting
171  // from zero for each crossing.
173 
174  // Cache random number engine
176  randomEngine_ = &rng->getEngine(e.streamID());
177 
178  _pixeldigialgo->initializeEvent();
180  iSetup.get<IdealMagneticFieldRecord>().get(pSetup);
182  iSetup.get<TrackerTopologyRcd>().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( const auto& iu : pDD->detUnits()) {
189  unsigned int detId = iu->geographicalId().rawId();
190  if(iu->type().isTrackerPixel()) {
191  auto pixdet = dynamic_cast<const PixelGeomDetUnit*>(iu);
192  assert(pixdet != nullptr);
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, 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, 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
248  iSetup.get<TrackerTopologyRcd>().get(tTopoHand);
249  const TrackerTopology *tTopo=tTopoHand.product();
250 
251  std::vector<edm::DetSet<PixelDigi> > theDigiVector;
252  std::vector<edm::DetSet<PixelDigiSimLink> > theDigiLinkVector;
253 
255  if (firstFinalizeEvent_) {
256  const unsigned int bunchspace = PileupInfo_->getMix_bunchSpacing();
257  _pixeldigialgo->init_DynIneffDB(iSetup, bunchspace);
258  firstFinalizeEvent_ = false;
259  }
260  _pixeldigialgo->calculateInstlumiFactor(PileupInfo_);
261 
262  for( const auto& iu : pDD->detUnits()) {
263 
264  if(iu->type().isTrackerPixel()) {
265 
266  //
267 
268  edm::DetSet<PixelDigi> collector(iu->geographicalId().rawId());
269  edm::DetSet<PixelDigiSimLink> linkcollector(iu->geographicalId().rawId());
270 
271 
272  _pixeldigialgo->digitize(dynamic_cast<const PixelGeomDetUnit*>(iu),
273  collector.data,
274  linkcollector.data,
275  tTopo,
276  randomEngine_);
277  if(!collector.data.empty()) {
278  theDigiVector.push_back(std::move(collector));
279  }
280  if(!linkcollector.data.empty()) {
281  theDigiLinkVector.push_back(std::move(linkcollector));
282  }
283  }
284  }
285 
286  // Step C: create collection with the cache vector of DetSet
287  std::unique_ptr<edm::DetSetVector<PixelDigi> >
288  output(new edm::DetSetVector<PixelDigi>(theDigiVector) );
289  std::unique_ptr<edm::DetSetVector<PixelDigiSimLink> >
290  outputlink(new edm::DetSetVector<PixelDigiSimLink>(theDigiLinkVector) );
291 
292  // Step D: write output to file
293  iEvent.put(std::move(output));
294  iEvent.put(std::move(outputlink));
295 
296  randomEngine_ = nullptr; // to prevent access outside event
297  }
298 }// end namespace cms::
299 
#define LogDebug(id)
BranchAliasSetterT< ProductType > produces()
declare what type of product will make and with which optional label
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
type
Definition: HCALResponse.h:21
void finalizeEvent(edm::Event &e, edm::EventSetup const &c) override
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:137
const DetContainer & detUnits() const override
Returm a vector of all GeomDet.
std::map< unsigned int, PixelGeomDetUnit const * > detectorUnits
edm::ESHandle< MagneticField > pSetup
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &)=0
Use this engine in event methods.
std::string encode() const
Definition: InputTag.cc:166
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. ...
bunchspace
in terms of 25 ns
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, edm::EventSetup const &c)
edm::ESHandle< TrackerGeometry > pDD
bool isValid() const
Definition: HandleBase.h:74
std::unique_ptr< SiPixelDigitizerAlgorithm > _pixeldigialgo
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:535
virtual GlobalVector inTesla(const GlobalPoint &gp) const =0
Field value ad specified global point, in Tesla.
void accumulate(edm::Event const &e, edm::EventSetup const &c) override
Namespace of DDCMS conversion namespace.
CLHEP::HepRandomEngine * randomEngine_
const std::string geometryType
PileupMixingContent * PileupInfo_
unsigned int layer(const DetId &id) const
const vstring trackerContainers
bool getByLabel(edm::InputTag const &tag, edm::Handle< T > &result) const
T get() const
Definition: EventSetup.h:62
StreamID streamID() const
Definition: Event.h:96
const int & getMix_bunchSpacing() const
PileupMixingContent * getEventPileupInfo() override
T const * product() const
Definition: ESHandle.h:86
def move(src, dest)
Definition: eostools.py:511
bool isTrackerPixel(GeomDetEnumerators::SubDetector m)
SiPixelDigitizer(const edm::ParameterSet &conf, edm::ProducerBase &mixMod, edm::ConsumesCollector &iC)