CMS 3D CMS Logo

PreMixingSiPixelWorker.cc
Go to the documentation of this file.
10 
11 //Data Formats
17 
21 
29 
30 #include "CLHEP/Random/RandFlat.h"
31 
34 
36 
37 #include <map>
38 #include <memory>
39 
41 public:
43  ~PreMixingSiPixelWorker() override = default;
44 
45  void initializeEvent(edm::Event const& e, edm::EventSetup const& c) override;
46  void addSignals(edm::Event const& e, edm::EventSetup const& es) override;
47  void addPileups(PileUpEventPrincipal const&, edm::EventSetup const& es) override;
48  void put(edm::Event& e, edm::EventSetup const& iSetup, std::vector<PileupSummaryInfo> const& ps, int bs) override;
49 
50 private:
51  edm::InputTag pixeldigi_collectionSig_; // secondary name given to collection of SiPixel digis
52  edm::InputTag pixeldigi_collectionPile_; // secondary name given to collection of SiPixel digis
53  edm::InputTag pixeldigi_extraInfo_; // secondary name given to collection of SiPixel digis
54  std::string PixelDigiCollectionDM_; // secondary name to be given to new SiPixel digis
55 
61 
63 
64  //
65  // Internal typedefs
66 
67  typedef int Amplitude;
68  typedef std::map<int, Amplitude, std::less<int>> signal_map_type; // from Digi.Skel.
69  typedef std::map<uint32_t, signal_map_type> signalMaps;
70 
71  typedef std::multimap<int, PixelDigi>
72  OneDetectorMap; // maps by pixel ID for later combination - can have duplicate pixels
73  typedef std::map<uint32_t, OneDetectorMap> SiGlobalIndex; // map to all data for each detector ID
74  typedef std::multimap<int, PixelSimHitExtraInfo> OneExtraInfoMap;
75  typedef std::map<uint32_t, OneExtraInfoMap> SiPixelExtraInfo;
76 
79 
80  bool firstInitializeEvent_ = true;
81  bool firstFinalizeEvent_ = true;
83 };
84 
85 // Constructor
87  edm::ProducesCollector producesCollector,
89  : tTopoToken_(iC.esConsumes()),
90  pDDToken_(iC.esConsumes(edm::ESInputTag("", ps.getParameter<std::string>("PixGeometryType")))),
91  digitizer_(ps, iC) {
92  // declare the products to produce
93 
94  pixeldigi_collectionSig_ = ps.getParameter<edm::InputTag>("pixeldigiCollectionSig");
95  pixeldigi_collectionPile_ = ps.getParameter<edm::InputTag>("pixeldigiCollectionPile");
96  pixeldigi_extraInfo_ = ps.getParameter<edm::InputTag>("pixeldigiExtraCollectionPile");
97  PixelDigiCollectionDM_ = ps.getParameter<std::string>("PixelDigiCollectionDM");
98  applyLateReweighting_ = ps.getParameter<bool>("applyLateReweighting");
99  LogDebug("PreMixingSiPixelWorker") << "applyLateReweighting_ in PreMixingSiPixelWorker " << applyLateReweighting_;
100 
104 
107 
108  // clear local storage for this event
109  SiHitStorage_.clear();
110  SiHitExtraStorage_.clear();
111 }
112 
113 // Need an event initialization
114 
116  if (firstInitializeEvent_) {
117  digitizer_.init(iSetup);
118  firstInitializeEvent_ = false;
119  }
121 }
122 
124  // fill in maps of hits
125 
126  LogDebug("PreMixingSiPixelWorker") << "===============> adding MC signals for " << e.id();
127 
129 
130  if (e.getByToken(PixelDigiToken_, input)) {
131  //loop on all detsets (detectorIDs) inside the input collection
133  for (; DSViter != input->end(); DSViter++) {
134 #ifdef DEBUG
135  LogDebug("PreMixingSiPixelWorker") << "Processing DetID " << DSViter->id;
136 #endif
137 
138  uint32_t detID = DSViter->id;
139  edm::DetSet<PixelDigi>::const_iterator begin = (DSViter->data).begin();
140  edm::DetSet<PixelDigi>::const_iterator end = (DSViter->data).end();
142 
143  OneDetectorMap LocalMap;
144 
145  for (icopy = begin; icopy != end; icopy++) {
146  LocalMap.insert(OneDetectorMap::value_type((icopy->channel()), *icopy));
147  }
148 
149  SiHitStorage_.insert(SiGlobalIndex::value_type(detID, LocalMap));
150  }
151  }
152 } // end of addSiPixelSignals
153 
155  LogDebug("PreMixingSiPixelWorker") << "\n===============> adding pileups from event " << pep.principal().id()
156  << " for bunchcrossing " << pep.bunchCrossing();
157 
158  // fill in maps of hits; same code as addSignals, except now applied to the pileup events
159 
161  pep.getByLabel(pixeldigi_collectionPile_, inputHandle);
162 
163  // added for the Late CR
165  pep.getByLabel(pixeldigi_extraInfo_, pixelAddInfo);
166  const TrackerTopology* tTopo = &es.getData(tTopoToken_);
167  auto const& pDD = es.getData(pDDToken_);
169  CLHEP::HepRandomEngine* engine = &rng->getEngine(pep.principal().streamID());
170 
171  if (inputHandle.isValid()) {
172  const auto& input = *inputHandle;
173 
174  bool loadExtraInformation = false;
175 
176  if (pixelAddInfo.isValid() && applyLateReweighting_) {
177  // access the extra information
178  loadExtraInformation = true;
179  // Iterate on detector units
181  for (detIdIter = pixelAddInfo->begin(); detIdIter != pixelAddInfo->end(); detIdIter++) {
182  uint32_t detid = detIdIter->id; // = rawid
183  OneExtraInfoMap LocalExtraMap;
185  for (di = detIdIter->data.begin(); di != detIdIter->data.end(); di++) {
186  LocalExtraMap.insert(OneExtraInfoMap::value_type((di->hitIndex()), *di));
187  }
188  SiHitExtraStorage_.insert(SiPixelExtraInfo::value_type(detid, LocalExtraMap));
189  } // end loop on detIdIter
190  } // end if applyLateReweighting_
191  else if (!pixelAddInfo.isValid() && applyLateReweighting_) {
192  edm::LogError("PreMixingSiPixelWorker") << " Problem in accessing the Extra Pixel SimHit Collection !!!! ";
193  edm::LogError("PreMixingSiPixelWorker") << " The Late Charge Reweighting can not be applied ";
194  throw cms::Exception("PreMixingSiPixelWorker")
195  << " Problem in accessing the Extra Pixel SimHit Collection for Late Charge Reweighting \n";
196  }
197 
198  //loop on all detsets (detectorIDs) inside the input collection
200  for (; DSViter != input.end(); DSViter++) {
201 #ifdef DEBUG
202  LogDebug("PreMixingSiPixelWorker") << "Pileups: Processing DetID " << DSViter->id;
203 #endif
204 
205  uint32_t detID = DSViter->id;
206  edm::DetSet<PixelDigi>::const_iterator begin = (DSViter->data).begin();
207  edm::DetSet<PixelDigi>::const_iterator end = (DSViter->data).end();
209 
210  // find correct local map (or new one) for this detector ID
211 
212  SiGlobalIndex::const_iterator itest;
213 
214  itest = SiHitStorage_.find(detID);
215 
216  std::vector<PixelDigi> TempDigis;
217  for (icopy = begin; icopy != end; icopy++) {
218  TempDigis.push_back(*icopy);
219  }
220  if (loadExtraInformation) {
221  // apply the Late Charge Reweighthing on Pile-up digi
222  SiPixelExtraInfo::const_iterator jtest;
223  jtest = SiHitExtraStorage_.find(detID);
224  OneExtraInfoMap LocalSimHitExtraMap = jtest->second;
225  std::vector<PixelSimHitExtraInfo> TempSimExtra;
226  for (auto& iLocal : LocalSimHitExtraMap) {
227  TempSimExtra.push_back(iLocal.second);
228  }
229 
230  for (const auto& iu : pDD.detUnits()) {
231  if (iu->type().isTrackerPixel()) {
232  uint32_t detIDinLoop = iu->geographicalId().rawId();
233  if (detIDinLoop == detID) {
235  dynamic_cast<const PixelGeomDetUnit*>(iu), TempDigis, TempSimExtra, tTopo, engine);
236  break;
237  }
238  }
239  }
240  }
241 
242  if (itest != SiHitStorage_.end()) { // this detID already has hits, add to existing map
243 
244  OneDetectorMap LocalMap = itest->second;
245 
246  // fill in local map with extra channels
247  for (unsigned int ij = 0; ij < TempDigis.size(); ij++) {
248  LocalMap.insert(OneDetectorMap::value_type((TempDigis[ij].channel()), TempDigis[ij]));
249  }
250 
251  SiHitStorage_[detID] = LocalMap;
252 
253  } else { // fill local storage with this information, put in global collection
254 
255  OneDetectorMap LocalMap;
256 
257  for (unsigned int ij = 0; ij < TempDigis.size(); ij++) {
258  LocalMap.insert(OneDetectorMap::value_type((TempDigis[ij].channel()), TempDigis[ij]));
259  }
260 
261  SiHitStorage_.insert(SiGlobalIndex::value_type(detID, LocalMap));
262  }
263  }
264  }
265 }
266 
268  edm::EventSetup const& iSetup,
269  std::vector<PileupSummaryInfo> const& ps,
270  int bs) {
271  // collection of Digis to put in the event
272 
273  std::vector<edm::DetSet<PixelDigi>> vPixelDigi;
274 
275  // loop through our collection of detectors, merging hits and putting new ones in the output
276  signalMaps signal;
277 
278  // big loop over Detector IDs:
279 
280  for (SiGlobalIndex::const_iterator IDet = SiHitStorage_.begin(); IDet != SiHitStorage_.end(); IDet++) {
281  uint32_t detID = IDet->first;
282 
283  OneDetectorMap LocalMap = IDet->second;
284 
285  signal_map_type Signals;
286  Signals.clear();
287 
288  //counter variables
289  int formerPixel = -1;
290  int currentPixel;
291  int ADCSum = 0;
292 
293  OneDetectorMap::const_iterator iLocalchk;
294 
295  for (OneDetectorMap::const_iterator iLocal = LocalMap.begin(); iLocal != LocalMap.end(); ++iLocal) {
296  currentPixel = iLocal->first;
297 
298  if (currentPixel == formerPixel) { // we have to add these digis together
299  ADCSum += (iLocal->second).adc();
300  } else {
301  if (formerPixel != -1) { // ADC info stolen from SiStrips...
302  if (ADCSum > 511)
303  ADCSum = 255;
304  else if (ADCSum > 253 && ADCSum < 512)
305  ADCSum = 254;
306 
307  Signals.insert(std::make_pair(formerPixel, ADCSum));
308  }
309  // save pointers for next iteration
310  formerPixel = currentPixel;
311  ADCSum = (iLocal->second).adc();
312  }
313 
314  iLocalchk = iLocal;
315  if ((++iLocalchk) == LocalMap.end()) { //make sure not to lose the last one
316  if (ADCSum > 511)
317  ADCSum = 255;
318  else if (ADCSum > 253 && ADCSum < 512)
319  ADCSum = 254;
320  Signals.insert(std::make_pair(formerPixel, ADCSum));
321  }
322 
323  } // end of loop over one detector
324 
325  // stick this into the global vector of detector info
326  signal.insert(std::make_pair(detID, Signals));
327 
328  } // end of big loop over all detector IDs
329 
330  // put the collection of digis in the event
331  edm::LogInfo("PreMixingSiPixelWorker") << "total # Merged Pixels: " << signal.size();
332 
333  std::vector<edm::DetSet<PixelDigi>> theDigiVector;
334 
335  // Load inefficiency constants (1st pass), set pileup information.
336  if (firstFinalizeEvent_) {
337  digitizer_.init_DynIneffDB(iSetup);
338  firstFinalizeEvent_ = false;
339  }
340 
343 
345  CLHEP::HepRandomEngine* engine = &rng->getEngine(e.streamID());
346 
347  auto const& pDD = iSetup.getData(pDDToken_);
348  const TrackerTopology* tTopo = &iSetup.getData(tTopoToken_);
349 
351  std::unique_ptr<PixelFEDChannelCollection> PixelFEDChannelCollection_ = digitizer_.chooseScenario(ps, engine);
352  if (PixelFEDChannelCollection_ == nullptr) {
353  throw cms::Exception("NullPointerError") << "PixelFEDChannelCollection not set in chooseScenario function.\n";
354  }
355  e.put(std::move(PixelFEDChannelCollection_), PixelDigiCollectionDM_);
356  }
357 
358  for (const auto& iu : pDD.detUnits()) {
359  if (iu->type().isTrackerPixel()) {
360  edm::DetSet<PixelDigi> collector(iu->geographicalId().rawId());
361  edm::DetSet<PixelDigiSimLink> linkcollector(
362  iu->geographicalId().rawId()); // ignored as DigiSimLinks are combined separately
363  std::vector<PixelDigiAddTempInfo> tempcollector; // to be ignored ?
364 
366  dynamic_cast<const PixelGeomDetUnit*>(iu), collector.data, linkcollector.data, tempcollector, tTopo, engine);
367  if (!collector.data.empty()) {
368  theDigiVector.push_back(std::move(collector));
369  }
370  }
371  }
372 
373  e.put(std::make_unique<edm::DetSetVector<PixelDigi>>(theDigiVector), PixelDigiCollectionDM_);
374 
375  // clear local storage for this event
376  SiHitStorage_.clear();
377  SiHitExtraStorage_.clear();
378 }
379 
void init(const edm::EventSetup &es)
const edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > tTopoToken_
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
void put(edm::Event &e, edm::EventSetup const &iSetup, std::vector< PileupSummaryInfo > const &ps, int bs) override
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
edm::EDGetTokenT< edm::DetSetVector< PixelSimHitExtraInfo > > PixelDigiPExtraToken_
std::multimap< int, PixelDigi > OneDetectorMap
edm::EDGetTokenT< edm::DetSetVector< PixelDigi > > PixelDigiPToken_
void addPileups(PileUpEventPrincipal const &, edm::EventSetup const &es) override
ProductRegistryHelper::BranchAliasSetterT< ProductType > produces()
PreMixingSiPixelWorker(const edm::ParameterSet &ps, edm::ProducesCollector, edm::ConsumesCollector &&iC)
std::map< uint32_t, OneExtraInfoMap > SiPixelExtraInfo
Log< level::Error, false > LogError
StreamID streamID() const
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &)=0
Use this engine in event methods.
static std::string const input
Definition: EdmProvDump.cc:50
edm::EventPrincipal const & principal()
~PreMixingSiPixelWorker() override=default
void digitize(const PixelGeomDetUnit *pixdet, std::vector< PixelDigi > &digis, std::vector< PixelDigiSimLink > &simlinks, std::vector< PixelDigiAddTempInfo > &newClass_Digi_extra, const TrackerTopology *tTopo, CLHEP::HepRandomEngine *)
edm::EDGetTokenT< edm::DetSetVector< PixelDigi > > PixelDigiToken_
SiPixelDigitizerAlgorithm digitizer_
void init_DynIneffDB(const edm::EventSetup &)
std::unique_ptr< PixelFEDChannelCollection > chooseScenario(PileupMixingContent *puInfo, CLHEP::HepRandomEngine *)
iterator end()
Return the off-the-end iterator.
Definition: DetSetVector.h:316
SiPixelExtraInfo SiHitExtraStorage_
void addSignals(edm::Event const &e, edm::EventSetup const &es) override
const edm::ESGetToken< TrackerGeometry, TrackerDigiGeometryRecord > pDDToken_
void setSimAccumulator(const std::map< uint32_t, std::map< int, int > > &signalMap)
std::multimap< int, PixelSimHitExtraInfo > OneExtraInfoMap
Log< level::Info, false > LogInfo
void initializeEvent(edm::Event const &e, edm::EventSetup const &c) override
void calculateInstlumiFactor(PileupMixingContent *puInfo)
bool isValid() const
Definition: HandleBase.h:70
std::map< uint32_t, signal_map_type > signalMaps
void lateSignalReweight(const PixelGeomDetUnit *pixdet, std::vector< PixelDigi > &digis, std::vector< PixelSimHitExtraInfo > &newClass_Sim_extra, const TrackerTopology *tTopo, CLHEP::HepRandomEngine *engine)
void insert(detset const &s)
Insert the given DetSet.
Definition: DetSetVector.h:211
HLT enums.
iterator begin()
Return an iterator to the first DetSet.
Definition: DetSetVector.h:305
std::map< int, Amplitude, std::less< int > > signal_map_type
collection_type::const_iterator const_iterator
Definition: DetSet.h:31
collection_type::const_iterator const_iterator
Definition: DetSetVector.h:102
EventID const & id() const
#define DEFINE_PREMIXING_WORKER(TYPE)
def move(src, dest)
Definition: eostools.py:511
uint16_t *__restrict__ uint16_t const *__restrict__ adc
bool getByLabel(edm::InputTag const &tag, edm::Handle< T > &result) const
#define LogDebug(id)
std::map< uint32_t, OneDetectorMap > SiGlobalIndex