CMS 3D CMS Logo

PreMixingSiPixelWorker.cc
Go to the documentation of this file.
11 
12 //Data Formats
18 
22 
30 
31 #include "CLHEP/Random/RandFlat.h"
32 
35 
37 
38 #include <map>
39 #include <memory>
40 
42 public:
44  ~PreMixingSiPixelWorker() override = default;
45 
46  void initializeEvent(edm::Event const& e, edm::EventSetup const& c) override;
47  void addSignals(edm::Event const& e, edm::EventSetup const& es) override;
48  void addPileups(PileUpEventPrincipal const&, edm::EventSetup const& es) override;
49  void put(edm::Event &e, edm::EventSetup const& iSetup, std::vector<PileupSummaryInfo> const& ps, int bs) override;
50 
51 private:
52  edm::InputTag pixeldigi_collectionSig_ ; // secondary name given to collection of SiPixel digis
53  edm::InputTag pixeldigi_collectionPile_ ; // secondary name given to collection of SiPixel digis
54  std::string PixelDigiCollectionDM_ ; // secondary name to be given to new SiPixel digis
55 
56  edm::EDGetTokenT<edm::DetSetVector<PixelDigi> > PixelDigiToken_ ; // Token to retrieve information
58 
60 
62 
63  //
64  // Internal typedefs
65 
66  typedef int Amplitude;
67  typedef std::map<int, Amplitude, std::less<int> > signal_map_type; // from Digi.Skel.
68  typedef std::map<uint32_t, signal_map_type> signalMaps;
69 
70  typedef std::multimap<int, PixelDigi> OneDetectorMap; // maps by pixel ID for later combination - can have duplicate pixels
71  typedef std::map<uint32_t, OneDetectorMap> SiGlobalIndex; // map to all data for each detector ID
72 
73  SiGlobalIndex SiHitStorage_;
74 
76 
77  bool firstInitializeEvent_ = true;
78  bool firstFinalizeEvent_ = true;
79 };
80 
81 // Constructor
83  digitizer_(ps),
84  geometryType_(ps.getParameter<std::string>("PixGeometryType"))
85 {
86  // declare the products to produce
87 
88  pixeldigi_collectionSig_ = ps.getParameter<edm::InputTag>("pixeldigiCollectionSig");
89  pixeldigi_collectionPile_ = ps.getParameter<edm::InputTag>("pixeldigiCollectionPile");
90  PixelDigiCollectionDM_ = ps.getParameter<std::string>("PixelDigiCollectionDM");
91 
94 
95 
98 
99  // clear local storage for this event
100  SiHitStorage_.clear();
101 }
102 
103 // Need an event initialization
104 
108  digitizer_.init(iSetup);
109  firstInitializeEvent_ = false;
110  }
112 }
113 
114 
115 
117  // fill in maps of hits
118 
119  LogDebug("PreMixingSiPixelWorker")<<"===============> adding MC signals for "<<e.id();
120 
122 
123  if( e.getByToken(PixelDigiToken_,input) ) {
124 
125  //loop on all detsets (detectorIDs) inside the input collection
126  edm::DetSetVector<PixelDigi>::const_iterator DSViter=input->begin();
127  for (; DSViter!=input->end();DSViter++){
128 
129 #ifdef DEBUG
130  LogDebug("PreMixingSiPixelWorker") << "Processing DetID " << DSViter->id;
131 #endif
132 
133  uint32_t detID = DSViter->id;
137 
138  OneDetectorMap LocalMap;
139 
140  for (icopy=begin; icopy!=end; icopy++) {
141  LocalMap.insert(OneDetectorMap::value_type( (icopy->channel()), *icopy ));
142  }
143 
144  SiHitStorage_.insert( SiGlobalIndex::value_type( detID, LocalMap ) );
145  }
146 
147  }
148 } // end of addSiPixelSignals
149 
150 
151 
153 
154  LogDebug("PreMixingSiPixelWorker") <<"\n===============> adding pileups from event "<<pep.principal().id()<<" for bunchcrossing "<<pep.bunchCrossing();
155 
156  // fill in maps of hits; same code as addSignals, except now applied to the pileup events
157 
159  pep.getByLabel(pixeldigi_collectionPile_, inputHandle);
160 
161  if(inputHandle.isValid()) {
162  const auto& input = *inputHandle;
163 
164  //loop on all detsets (detectorIDs) inside the input collection
166  for (; DSViter!=input.end();DSViter++){
167 
168 #ifdef DEBUG
169  LogDebug("PreMixingSiPixelWorker") << "Pileups: Processing DetID " << DSViter->id;
170 #endif
171 
172  uint32_t detID = DSViter->id;
176 
177  // find correct local map (or new one) for this detector ID
178 
179  SiGlobalIndex::const_iterator itest;
180 
181  itest = SiHitStorage_.find(detID);
182 
183  if(itest!=SiHitStorage_.end()) { // this detID already has hits, add to existing map
184 
185  OneDetectorMap LocalMap = itest->second;
186 
187  // fill in local map with extra channels
188  for (icopy=begin; icopy!=end; icopy++) {
189  LocalMap.insert(OneDetectorMap::value_type( (icopy->channel()), *icopy ));
190  }
191 
192  SiHitStorage_[detID]=LocalMap;
193 
194  }
195  else{ // fill local storage with this information, put in global collection
196 
197  OneDetectorMap LocalMap;
198 
199  for (icopy=begin; icopy!=end; icopy++) {
200  LocalMap.insert(OneDetectorMap::value_type( (icopy->channel()), *icopy ));
201  }
202 
203  SiHitStorage_.insert( SiGlobalIndex::value_type( detID, LocalMap ) );
204  }
205 
206  }
207  }
208 }
209 
210 
211 
212 void PreMixingSiPixelWorker::put(edm::Event &e, edm::EventSetup const& iSetup, std::vector<PileupSummaryInfo> const& ps, int bs) {
213 
214  // collection of Digis to put in the event
215 
216  std::vector< edm::DetSet<PixelDigi> > vPixelDigi;
217 
218  // loop through our collection of detectors, merging hits and putting new ones in the output
219  signalMaps signal;
220 
221  // big loop over Detector IDs:
222 
223  for(SiGlobalIndex::const_iterator IDet = SiHitStorage_.begin();
224  IDet != SiHitStorage_.end(); IDet++) {
225 
226  uint32_t detID = IDet->first;
227 
228  OneDetectorMap LocalMap = IDet->second;
229 
230  signal_map_type Signals;
231  Signals.clear();
232 
233  //counter variables
234  int formerPixel = -1;
235  int currentPixel;
236  int ADCSum = 0;
237 
238 
239  OneDetectorMap::const_iterator iLocalchk;
240 
241  for(OneDetectorMap::const_iterator iLocal = LocalMap.begin();
242  iLocal != LocalMap.end(); ++iLocal) {
243 
244  currentPixel = iLocal->first;
245 
246  if (currentPixel == formerPixel) { // we have to add these digis together
247  ADCSum+=(iLocal->second).adc();
248  }
249  else{
250  if(formerPixel!=-1){ // ADC info stolen from SiStrips...
251  if (ADCSum > 511) ADCSum = 255;
252  else if (ADCSum > 253 && ADCSum < 512) ADCSum = 254;
253 
254  Signals.insert( std::make_pair(formerPixel, ADCSum));
255  }
256  // save pointers for next iteration
257  formerPixel = currentPixel;
258  ADCSum = (iLocal->second).adc();
259  }
260 
261  iLocalchk = iLocal;
262  if((++iLocalchk) == LocalMap.end()) { //make sure not to lose the last one
263  if (ADCSum > 511) ADCSum = 255;
264  else if (ADCSum > 253 && ADCSum < 512) ADCSum = 254;
265  Signals.insert( std::make_pair(formerPixel, ADCSum));
266  }
267 
268  }// end of loop over one detector
269 
270  // stick this into the global vector of detector info
271  signal.insert( std::make_pair( detID, Signals));
272 
273  } // end of big loop over all detector IDs
274 
275  // put the collection of digis in the event
276  edm::LogInfo("PreMixingSiPixelWorker") << "total # Merged Pixels: " << signal.size() ;
277 
278 
279  std::vector<edm::DetSet<PixelDigi> > theDigiVector;
280 
281  // Load inefficiency constants (1st pass), set pileup information.
282  if(firstFinalizeEvent_) {
283  digitizer_.init_DynIneffDB(iSetup, bs);
284  firstFinalizeEvent_ = false;
285  }
286 
289 
291  CLHEP::HepRandomEngine* engine = &rng->getEngine(e.streamID());
292 
294  iSetup.get<TrackerTopologyRcd>().get(tTopoHand);
295 
296  const TrackerTopology* tTopo = tTopoHand.product();
297 
299  std::unique_ptr<PixelFEDChannelCollection> PixelFEDChannelCollection_ = digitizer_.chooseScenario(ps, engine);
300  if (PixelFEDChannelCollection_ == nullptr) {
301  throw cms::Exception("NullPointerError") << "PixelFEDChannelCollection not set in chooseScenario function.\n";
302  }
303  e.put(std::move(PixelFEDChannelCollection_), PixelDigiCollectionDM_);
304  }
305 
306  for (const auto& iu : pDD->detUnits()) {
307  if (iu->type().isTrackerPixel()) {
308  edm::DetSet<PixelDigi> collector(iu->geographicalId().rawId());
309  edm::DetSet<PixelDigiSimLink> linkcollector(iu->geographicalId().rawId()); // ignored as DigiSimLinks are combined separately
310 
311  digitizer_.digitize(dynamic_cast<const PixelGeomDetUnit *>(iu),
312  collector.data,
313  linkcollector.data,
314  tTopo,
315  engine);
316  if(!collector.data.empty()) {
317  theDigiVector.push_back(std::move(collector));
318  }
319  }
320  }
321 
322  e.put(std::make_unique<edm::DetSetVector<PixelDigi> >(theDigiVector), PixelDigiCollectionDM_);
323 
324  // clear local storage for this event
325  SiHitStorage_.clear();
326 }
327 
#define LogDebug(id)
BranchAliasSetterT< ProductType > produces()
declare what type of product will make and with which optional label
void init(const edm::EventSetup &es)
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
void init_DynIneffDB(const edm::EventSetup &, const unsigned int &)
void put(edm::Event &e, edm::EventSetup const &iSetup, std::vector< PileupSummaryInfo > const &ps, int bs) override
std::multimap< int, PixelDigi > OneDetectorMap
edm::EDGetTokenT< edm::DetSetVector< PixelDigi > > PixelDigiPToken_
void addPileups(PileUpEventPrincipal const &, edm::EventSetup const &es) override
const DetContainer & detUnits() const override
Returm a vector of all GeomDet.
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
EventID const & id() const
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &)=0
Use this engine in event methods.
static std::string const input
Definition: EdmProvDump.cc:48
edm::EventPrincipal const & principal()
~PreMixingSiPixelWorker() override=default
edm::EDGetTokenT< edm::DetSetVector< PixelDigi > > PixelDigiToken_
SiPixelDigitizerAlgorithm digitizer_
void digitize(const PixelGeomDetUnit *pixdet, std::vector< PixelDigi > &digis, std::vector< PixelDigiSimLink > &simlinks, const TrackerTopology *tTopo, CLHEP::HepRandomEngine *)
#define end
Definition: vmac.h:39
constexpr int adc(sample_type sample)
get the ADC sample (12 bits)
bool isValid() const
Definition: HandleBase.h:74
std::unique_ptr< PixelFEDChannelCollection > chooseScenario(PileupMixingContent *puInfo, CLHEP::HepRandomEngine *)
void addSignals(edm::Event const &e, edm::EventSetup const &es) override
void setSimAccumulator(const std::map< uint32_t, std::map< int, int > > &signalMap)
void initializeEvent(edm::Event const &e, edm::EventSetup const &c) override
void calculateInstlumiFactor(PileupMixingContent *puInfo)
std::map< uint32_t, signal_map_type > signalMaps
edm::EventID id() const
Definition: EventBase.h:59
#define begin
Definition: vmac.h:32
bool getByLabel(edm::InputTag const &tag, edm::Handle< T > &result) const
T get() const
Definition: EventSetup.h:71
edm::ESHandle< TrackerGeometry > pDD
StreamID streamID() const
Definition: Event.h:95
std::map< int, Amplitude, std::less< int > > signal_map_type
collection_type::const_iterator const_iterator
Definition: DetSet.h:33
collection_type::const_iterator const_iterator
Definition: DetSetVector.h:104
#define DEFINE_PREMIXING_WORKER(TYPE)
T const * product() const
Definition: ESHandle.h:86
PreMixingSiPixelWorker(const edm::ParameterSet &ps, edm::ProducerBase &producer, edm::ConsumesCollector &&iC)
def move(src, dest)
Definition: eostools.py:511
std::map< uint32_t, OneDetectorMap > SiGlobalIndex