CMS 3D CMS Logo

TrackingRecHitProducer.cc
Go to the documentation of this file.
12 
16 
19 
21 
24 
27 
30 
32 
35 
36 #include <map>
37 #include <memory>
38 #include <vector>
39 
42 {
43  private:
45  std::vector<TrackingRecHitAlgorithm*> _recHitAlgorithms;
47  std::map<unsigned int, TrackingRecHitPipe> _detIdPipes;
48  void setupDetIdPipes(const edm::EventSetup& eventSetup);
49 
50  public:
52 
53  virtual void beginRun(edm::Run const&, const edm::EventSetup& eventSetup);
54 
55  virtual void beginStream(edm::StreamID id);
56 
57  virtual void produce(edm::Event& event, const edm::EventSetup& eventSetup);
58 
59  virtual void endStream();
60 
61  virtual ~TrackingRecHitProducer();
62 };
63 
64 
66 {
67  edm::ConsumesCollector consumeCollector = consumesCollector();
68  const std::vector<edm::ParameterSet>& pluginConfigs = config.getParameter<std::vector<edm::ParameterSet>>("plugins");
69 
70  for (unsigned int iplugin = 0; iplugin<pluginConfigs.size(); ++iplugin)
71  {
72  const edm::ParameterSet& pluginConfig = pluginConfigs[iplugin];
73  const std::string pluginType = pluginConfig.getParameter<std::string>("type");
74  const std::string pluginName = pluginConfig.getParameter<std::string>("name");
75 
76  TrackingRecHitAlgorithm* recHitAlgorithm = TrackingRecHitAlgorithmFactory::get()->tryToCreate(pluginType,pluginName,pluginConfig,consumeCollector);
77  if (recHitAlgorithm)
78  {
79  edm::LogInfo("TrackingRecHitProducer: ")<< "adding plugin type '"<<pluginType<<"' as '"<<pluginName<<"'"<<std::endl;
80  _recHitAlgorithms.push_back(recHitAlgorithm);
81  }
82  else
83  {
84  throw cms::Exception("TrackingRecHitAlgorithm plugin not found: ") << "plugin type = "<<pluginType<<"\nconfiguration=\n"<<pluginConfig.dump();
85  }
86  }
87 
89  _simHitToken = consumes<std::vector<PSimHit>>(simHitTag);
90 
91  produces<FastTrackerRecHitCollection>();
92  produces<FastTrackerRecHitRefCollection>("simHit2RecHitMap");
93 }
94 
96 {
98  {
99  delete algo;
100  }
101  _recHitAlgorithms.clear();
102 }
103 
105 {
107  {
108  algo->beginStream(id);
109  }
110 }
111 
113 {
114 
115 }
116 
118 {
119  if (_iovSyncValue!=eventSetup.iovSyncValue())
120  {
121  _iovSyncValue=eventSetup.iovSyncValue();
122  edm::ESHandle<TrackerGeometry> trackerGeometryHandle;
123  edm::ESHandle<TrackerTopology> trackerTopologyHandle;
124  eventSetup.get<TrackerDigiGeometryRecord>().get(trackerGeometryHandle);
125  eventSetup.get<TrackerTopologyRcd>().get(trackerTopologyHandle);
126  const TrackerGeometry* trackerGeometry = trackerGeometryHandle.product();
127  const TrackerTopology* trackerTopology = trackerTopologyHandle.product();
128 
129  _detIdPipes.clear();
130 
131  //build pipes for all detIds
132  const std::vector<DetId>& detIds = trackerGeometry->detIds();
133  std::vector<unsigned int> numberOfDetIdsPerAlgorithm(_recHitAlgorithms.size(),0);
134 
135  for (const DetId& detId: detIds)
136  {
137  TrackerDetIdSelector selector(detId,*trackerTopology);
138 
140  for (unsigned int ialgo = 0; ialgo < _recHitAlgorithms.size(); ++ialgo)
141  {
143  if (selector.passSelection(algo->getSelectionString()))
144  {
145  numberOfDetIdsPerAlgorithm[ialgo]+=1;
146  pipe.addAlgorithm(algo);
147  }
148  }
149  if (pipe.size()==0)
150  {
151  throw cms::Exception("FastSimulation/TrackingRecHitProducer: DetId not configured! ("+trackerTopology->print(detId)+")");
152  }
153  _detIdPipes[detId.rawId()]=pipe;
154  }
155  }
156 }
157 
159 {
160 
161 
162  //resetup pipes if new iov
163  setupDetIdPipes(eventSetup);
164  //begin event
166  {
167  algo->beginEvent(event,eventSetup);
168  }
169 
170  //build DetId -> PSimHit map
172  event.getByToken(_simHitToken,simHits);
173 
174  std::unique_ptr<FastTrackerRecHitCollection> output_recHits(new FastTrackerRecHitCollection);
175  output_recHits->reserve(simHits->size());
176 
177  edm::RefProd<FastTrackerRecHitCollection> output_recHits_refProd = event.getRefBeforePut<FastTrackerRecHitCollection>();
178  std::unique_ptr<FastTrackerRecHitRefCollection> output_recHitRefs(new FastTrackerRecHitRefCollection(simHits->size(),FastTrackerRecHitRef()));
179 
180  std::map<unsigned int,std::vector<std::pair<unsigned int,const PSimHit*>>> simHitsIdPairPerDetId;
181  for (unsigned int ihit = 0; ihit < simHits->size(); ++ihit)
182  {
183  const PSimHit* simHit = &(*simHits)[ihit];
184  simHitsIdPairPerDetId[simHit->detUnitId()].push_back(std::make_pair(ihit,simHit));
185  }
186 
187  for (auto simHitsIdPairIt = simHitsIdPairPerDetId.begin(); simHitsIdPairIt != simHitsIdPairPerDetId.end(); ++simHitsIdPairIt)
188  {
189  const DetId& detId = simHitsIdPairIt->first;
190  std::map<unsigned int, TrackingRecHitPipe>::const_iterator pipeIt = _detIdPipes.find(detId);
191  if (pipeIt!=_detIdPipes.cend())
192  {
193  auto& simHitIdPairList = simHitsIdPairIt->second;
194  const TrackingRecHitPipe& pipe = pipeIt->second;
195 
196  TrackingRecHitProductPtr product = std::make_shared<TrackingRecHitProduct>(detId,simHitIdPairList);
197 
198  product = pipe.produce(product);
199 
200  const std::vector<TrackingRecHitProduct::RecHitToSimHitIdPairs>& recHitToSimHitIdPairsList = product->getRecHitToSimHitIdPairs();
201 
202 
203  for (unsigned int irecHit = 0; irecHit < recHitToSimHitIdPairsList.size(); ++irecHit)
204  {
205  output_recHits->push_back(recHitToSimHitIdPairsList[irecHit].first);
206  const std::vector<TrackingRecHitProduct::SimHitIdPair>& simHitIdPairList = recHitToSimHitIdPairsList[irecHit].second;
207  for (unsigned int isimHit = 0; isimHit < simHitIdPairList.size(); ++isimHit)
208  {
209  unsigned int simHitId = simHitIdPairList[isimHit].first;
210  if (not (*output_recHitRefs)[simHitId].isNull())
211  {
212  throw cms::Exception("FastSimulation/TrackingRecHitProducer","A PSimHit cannot lead to multiple FastTrackerRecHits");
213  }
214  (*output_recHitRefs)[simHitId] = FastTrackerRecHitRef(output_recHits_refProd,output_recHits->size()-1);
215  }
216  }
217  }
218  else
219  {
220  //there should be at least an empty pipe for each DetId
221  throw cms::Exception("FastSimulation/TrackingRecHitProducer","A PSimHit carries a DetId which does not belong to the TrackerGeometry: "+std::to_string(detId));
222  }
223  }
224 
225  //end event
226  for (TrackingRecHitAlgorithm* algo: _recHitAlgorithms)
227  {
228  algo->endEvent(event,eventSetup);
229  }
230 
231  // note from lukas:
232  // all rechits need a unique id numbers
233  for(unsigned recHitIndex = 0; recHitIndex < output_recHits->size(); ++recHitIndex)
234  {
235  ((FastSingleTrackerRecHit*)&(*output_recHits)[recHitIndex])->setId(recHitIndex);
236  }
237 
238  event.put(std::move(output_recHits));
239  event.put(std::move(output_recHitRefs),"simHit2RecHitMap");
240 
241 }
242 
244 {
246  {
247  algo->endStream();
248  }
249 }
250 
T getParameter(std::string const &) const
virtual void beginRun(edm::Run const &, const edm::EventSetup &eventSetup)
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
virtual void beginStream(edm::StreamID id)
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
def pipe(cmdline, input=None)
Definition: pipe.py:5
std::shared_ptr< TrackingRecHitProduct > TrackingRecHitProductPtr
virtual void produce(edm::Event &event, const edm::EventSetup &eventSetup)
Definition: DetId.h:18
const T & get() const
Definition: EventSetup.h:56
const IOVSyncValue & iovSyncValue() const
Definition: EventSetup.h:101
std::vector< FastTrackerRecHitRef > FastTrackerRecHitRefCollection
TrackingRecHitProducer(const edm::ParameterSet &config)
const DetIdContainer & detIds() const
Returm a vector of all GeomDet DetIds (including those of GeomDetUnits)
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:42