CMS 3D CMS Logo

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TrackingRecHitProducer.cc
Go to the documentation of this file.
12 
17 
20 
22 
25 
28 
31 // Pixel-related stuff:
35 
37 
40 
41 #include <map>
42 #include <memory>
43 #include <vector>
44 
47 {
48  private:
50  std::vector<TrackingRecHitAlgorithm*> _recHitAlgorithms;
52  std::map<unsigned int, TrackingRecHitPipe> _detIdPipes;
53  void setupDetIdPipes(const edm::EventSetup& eventSetup);
54  std::vector< SiPixelTemplateStore > _pixelTempStore ; // pixel template storage
55 
56  public:
58 
59  void beginRun(edm::Run const&, const edm::EventSetup& eventSetup) override;
60 
61  void beginStream(edm::StreamID id) override;
62 
63  void produce(edm::Event& event, const edm::EventSetup& eventSetup) override;
64 
65  void endStream() override;
66 
67  ~TrackingRecHitProducer() override;
68 };
69 
70 
72 {
73  edm::ConsumesCollector consumeCollector = consumesCollector();
74  const std::vector<edm::ParameterSet>& pluginConfigs = config.getParameter<std::vector<edm::ParameterSet>>("plugins");
75 
76  for (unsigned int iplugin = 0; iplugin<pluginConfigs.size(); ++iplugin)
77  {
78  const edm::ParameterSet& pluginConfig = pluginConfigs[iplugin];
79  const std::string pluginType = pluginConfig.getParameter<std::string>("type");
80  const std::string pluginName = pluginConfig.getParameter<std::string>("name");
81 
82  TrackingRecHitAlgorithm* recHitAlgorithm = TrackingRecHitAlgorithmFactory::get()->tryToCreate(pluginType,pluginName,pluginConfig,consumeCollector);
83  if (recHitAlgorithm)
84  {
85  edm::LogInfo("TrackingRecHitProducer: ")<< "adding plugin type '"<<pluginType<<"' as '"<<pluginName<<"'"<<std::endl;
86  _recHitAlgorithms.push_back(recHitAlgorithm);
87  }
88  else
89  {
90  throw cms::Exception("TrackingRecHitAlgorithm plugin not found: ") << "plugin type = "<<pluginType<<"\nconfiguration=\n"<<pluginConfig.dump();
91  }
92  }
93 
95  _simHitToken = consumes<std::vector<PSimHit>>(simHitTag);
96 
97  produces<FastTrackerRecHitCollection>();
98  produces<FastTrackerRecHitRefCollection>("simHit2RecHitMap");
99 }
100 
102 {
104  {
105  delete algo;
106  }
107  _recHitAlgorithms.clear();
108 
109  //--- Delete the templates. This is safe even if thePixelTemp_ vector is empty.
110  for (auto x : _pixelTempStore) x.destroy();
111 }
112 
113 
115 {
117  {
118  algo->beginStream(id);
119  }
120 }
121 
123 {
124  //--- Since all pixel algorithms (of which there could be several) use the same
125  // templateStore, filled out from the same DB Object, we need to it centrally
126  // (namely here), and then distribute it to the algorithms. Note that only
127  // the pixel algorithms implement beginRun(), for the strip tracker this defaults
128  // to a no-op.
129 
131  eventSetup.get<SiPixelTemplateDBObjectESProducerRcd>().get(templateDBobject);
132  const SiPixelTemplateDBObject * pixelTemplateDBObject = templateDBobject.product();
133 
134  //--- Now that we have the DB object, load the correct templates from the DB.
135  // (They are needed for data and full sim MC, so in a production FastSim
136  // run, everything should already be in the DB.)
137  if ( !SiPixelTemplate::pushfile( *pixelTemplateDBObject, _pixelTempStore ) ) {
138  throw cms::Exception("TrackingRecHitProducer:")
139  << "SiPixel Templates not loaded correctly from the DB object!" << std::endl;
140  }
141 
143  {
144  algo->beginRun(run, eventSetup, pixelTemplateDBObject, _pixelTempStore );
145  }
146 }
147 
149 {
150  if (_iovSyncValue!=eventSetup.iovSyncValue())
151  {
152  _iovSyncValue=eventSetup.iovSyncValue();
153  edm::ESHandle<TrackerGeometry> trackerGeometryHandle;
154  edm::ESHandle<TrackerTopology> trackerTopologyHandle;
155  eventSetup.get<TrackerDigiGeometryRecord>().get(trackerGeometryHandle);
156  eventSetup.get<TrackerTopologyRcd>().get(trackerTopologyHandle);
157  const TrackerGeometry* trackerGeometry = trackerGeometryHandle.product();
158  const TrackerTopology* trackerTopology = trackerTopologyHandle.product();
159 
160  _detIdPipes.clear();
161 
162  //build pipes for all detIds
163  const std::vector<DetId>& detIds = trackerGeometry->detIds();
164  std::vector<unsigned int> numberOfDetIdsPerAlgorithm(_recHitAlgorithms.size(),0);
165 
166  for (const DetId& detId: detIds)
167  {
168  TrackerDetIdSelector selector(detId,*trackerTopology);
169 
171  for (unsigned int ialgo = 0; ialgo < _recHitAlgorithms.size(); ++ialgo)
172  {
174  if (selector.passSelection(algo->getSelectionString()))
175  {
176  numberOfDetIdsPerAlgorithm[ialgo]+=1;
177  pipe.addAlgorithm(algo);
178  }
179  }
180  if (pipe.size()==0)
181  {
182  throw cms::Exception("FastSimulation/TrackingRecHitProducer: DetId not configured! ("+trackerTopology->print(detId)+")");
183  }
184  _detIdPipes[detId.rawId()]=pipe;
185  }
186  }
187 }
188 
190 {
191 
192 
193  //resetup pipes if new iov
194  setupDetIdPipes(eventSetup);
195  //begin event
197  {
198  algo->beginEvent(event,eventSetup);
199  }
200 
201  //build DetId -> PSimHit map
203  event.getByToken(_simHitToken,simHits);
204 
205  std::unique_ptr<FastTrackerRecHitCollection> output_recHits(new FastTrackerRecHitCollection);
206  output_recHits->reserve(simHits->size());
207 
208  edm::RefProd<FastTrackerRecHitCollection> output_recHits_refProd = event.getRefBeforePut<FastTrackerRecHitCollection>();
209  std::unique_ptr<FastTrackerRecHitRefCollection> output_recHitRefs(new FastTrackerRecHitRefCollection(simHits->size(),FastTrackerRecHitRef()));
210 
211  std::map<unsigned int,std::vector<std::pair<unsigned int,const PSimHit*>>> simHitsIdPairPerDetId;
212  for (unsigned int ihit = 0; ihit < simHits->size(); ++ihit)
213  {
214  const PSimHit* simHit = &(*simHits)[ihit];
215  simHitsIdPairPerDetId[simHit->detUnitId()].push_back(std::make_pair(ihit,simHit));
216  }
217 
218  for (auto simHitsIdPairIt = simHitsIdPairPerDetId.begin(); simHitsIdPairIt != simHitsIdPairPerDetId.end(); ++simHitsIdPairIt)
219  {
220  const DetId& detId = simHitsIdPairIt->first;
221  std::map<unsigned int, TrackingRecHitPipe>::const_iterator pipeIt = _detIdPipes.find(detId);
222  if (pipeIt!=_detIdPipes.cend())
223  {
224  auto& simHitIdPairList = simHitsIdPairIt->second;
225  const TrackingRecHitPipe& pipe = pipeIt->second;
226 
227  TrackingRecHitProductPtr product = std::make_shared<TrackingRecHitProduct>(detId,simHitIdPairList);
228 
229  product = pipe.produce(product);
230 
231  const std::vector<TrackingRecHitProduct::RecHitToSimHitIdPairs>& recHitToSimHitIdPairsList = product->getRecHitToSimHitIdPairs();
232 
233 
234  for (unsigned int irecHit = 0; irecHit < recHitToSimHitIdPairsList.size(); ++irecHit)
235  {
236  output_recHits->push_back(recHitToSimHitIdPairsList[irecHit].first);
237  const std::vector<TrackingRecHitProduct::SimHitIdPair>& simHitIdPairList = recHitToSimHitIdPairsList[irecHit].second;
238  for (unsigned int isimHit = 0; isimHit < simHitIdPairList.size(); ++isimHit)
239  {
240  unsigned int simHitId = simHitIdPairList[isimHit].first;
241  if (not (*output_recHitRefs)[simHitId].isNull())
242  {
243  throw cms::Exception("FastSimulation/TrackingRecHitProducer","A PSimHit cannot lead to multiple FastTrackerRecHits");
244  }
245  (*output_recHitRefs)[simHitId] = FastTrackerRecHitRef(output_recHits_refProd,output_recHits->size()-1);
246  }
247  }
248  }
249  else
250  {
251  //there should be at least an empty pipe for each DetId
252  throw cms::Exception("FastSimulation/TrackingRecHitProducer","A PSimHit carries a DetId which does not belong to the TrackerGeometry: "+std::to_string(detId));
253  }
254  }
255 
256  //end event
257  for (TrackingRecHitAlgorithm* algo: _recHitAlgorithms)
258  {
259  algo->endEvent(event,eventSetup);
260  }
261 
262  // note from lukas:
263  // all rechits need a unique id numbers
264  for(unsigned recHitIndex = 0; recHitIndex < output_recHits->size(); ++recHitIndex)
265  {
266  ((FastSingleTrackerRecHit*)&(*output_recHits)[recHitIndex])->setId(recHitIndex);
267  }
268 
269  event.put(std::move(output_recHits));
270  event.put(std::move(output_recHitRefs),"simHit2RecHitMap");
271 
272 }
273 
275 {
277  {
278  algo->endStream();
279  }
280 }
281 
T getParameter(std::string const &) const
std::string dump(unsigned int indent=0) const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
void setupDetIdPipes(const edm::EventSetup &eventSetup)
std::map< unsigned int, TrackingRecHitPipe > _detIdPipes
size_type size() const
Definition: OwnVector.h:264
static bool pushfile(int filenum, std::vector< SiPixelTemplateStore > &thePixelTemp_)
void beginStream(edm::StreamID id) override
void addAlgorithm(TrackingRecHitAlgorithm *algorithm)
edm::EDGetTokenT< std::vector< PSimHit > > _simHitToken
Definition: config.py:1
std::string print(DetId detid) const
edm::Ref< FastTrackerRecHitCollection > FastTrackerRecHitRef
bool passSelection(std::string selectionStr) const
const std::string & getSelectionString() const
std::vector< TrackingRecHitAlgorithm * > _recHitAlgorithms
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
unsigned int size() const
const DetIdContainer & detIds() const override
Returm a vector of all GeomDet DetIds (including those of GeomDetUnits)
void produce(edm::Event &event, const edm::EventSetup &eventSetup) override
def pipe(cmdline, input=None)
Definition: pipe.py:5
std::shared_ptr< TrackingRecHitProduct > TrackingRecHitProductPtr
Definition: DetId.h:18
const T & get() const
Definition: EventSetup.h:55
const IOVSyncValue & iovSyncValue() const
Definition: EventSetup.h:100
std::vector< FastTrackerRecHitRef > FastTrackerRecHitRefCollection
TrackingRecHitProducer(const edm::ParameterSet &config)
void beginRun(edm::Run const &, const edm::EventSetup &eventSetup) override
std::vector< SiPixelTemplateStore > _pixelTempStore
T const * product() const
Definition: ESHandle.h:86
Definition: pipe.py:1
TrackingRecHitProductPtr produce(TrackingRecHitProductPtr product) const
def move(src, dest)
Definition: eostools.py:510
T get(const Candidate &c)
Definition: component.h:55
unsigned int detUnitId() const
Definition: PSimHit.h:93
Definition: event.py:1
Definition: Run.h:43