CMS 3D CMS Logo

TrackingRecHitProducer.cc
Go to the documentation of this file.
11 
16 
19 
21 
24 
27 
30 // Pixel-related stuff:
34 
36 
39 
40 #include <map>
41 #include <memory>
42 #include <vector>
43 
46 {
47  private:
49  std::vector<std::unique_ptr<TrackingRecHitAlgorithm>> _recHitAlgorithms;
50  unsigned long long _trackerGeometryCacheID = 0;
51  unsigned long long _trackerTopologyCacheID = 0;
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  std::unique_ptr<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(std::move(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 {
103  //--- Delete the templates. This is safe even if thePixelTemp_ vector is empty.
104  for (auto x : _pixelTempStore) x.destroy();
105 }
106 
107 
109 {
110  for (auto& algo: _recHitAlgorithms)
111  {
112  algo->beginStream(id);
113  }
114 }
115 
117 {
118  //--- Since all pixel algorithms (of which there could be several) use the same
119  // templateStore, filled out from the same DB Object, we need to it centrally
120  // (namely here), and then distribute it to the algorithms. Note that only
121  // the pixel algorithms implement beginRun(), for the strip tracker this defaults
122  // to a no-op.
123 
125  eventSetup.get<SiPixelTemplateDBObjectESProducerRcd>().get(templateDBobject);
126  const SiPixelTemplateDBObject * pixelTemplateDBObject = templateDBobject.product();
127 
128  //--- Now that we have the DB object, load the correct templates from the DB.
129  // (They are needed for data and full sim MC, so in a production FastSim
130  // run, everything should already be in the DB.)
131  if ( !SiPixelTemplate::pushfile( *pixelTemplateDBObject, _pixelTempStore ) ) {
132  throw cms::Exception("TrackingRecHitProducer:")
133  << "SiPixel Templates not loaded correctly from the DB object!" << std::endl;
134  }
135 
136  for (auto& algo: _recHitAlgorithms)
137  {
138  algo->beginRun(run, eventSetup, pixelTemplateDBObject, _pixelTempStore );
139  }
140 }
141 
143 {
144  auto const& trackerGeomRec = eventSetup.get<TrackerDigiGeometryRecord>();
145  auto const& trackerTopoRec = eventSetup.get<TrackerTopologyRcd>();
146  if (trackerGeomRec.cacheIdentifier() != _trackerGeometryCacheID or
147  trackerTopoRec.cacheIdentifier() != _trackerTopologyCacheID )
148  {
149  _trackerGeometryCacheID = trackerGeomRec.cacheIdentifier();
150  _trackerTopologyCacheID = trackerTopoRec.cacheIdentifier();
151  edm::ESHandle<TrackerGeometry> trackerGeometryHandle;
152  edm::ESHandle<TrackerTopology> trackerTopologyHandle;
153  trackerGeomRec.get(trackerGeometryHandle);
154  trackerTopoRec.get(trackerTopologyHandle);
155  const TrackerGeometry* trackerGeometry = trackerGeometryHandle.product();
156  const TrackerTopology* trackerTopology = trackerTopologyHandle.product();
157 
158  _detIdPipes.clear();
159 
160  //build pipes for all detIds
161  const std::vector<DetId>& detIds = trackerGeometry->detIds();
162  std::vector<unsigned int> numberOfDetIdsPerAlgorithm(_recHitAlgorithms.size(),0);
163 
164  for (const DetId& detId: detIds)
165  {
166  TrackerDetIdSelector selector(detId,*trackerTopology);
167 
169  for (unsigned int ialgo = 0; ialgo < _recHitAlgorithms.size(); ++ialgo)
170  {
171  auto& algo = _recHitAlgorithms[ialgo];
172  if (selector.passSelection(algo->getSelectionString()))
173  {
174  numberOfDetIdsPerAlgorithm[ialgo]+=1;
175  pipe.addAlgorithm(algo.get());
176  }
177  }
178  if (pipe.size()==0)
179  {
180  throw cms::Exception("FastSimulation/TrackingRecHitProducer: DetId not configured! ("+trackerTopology->print(detId)+")");
181  }
182  _detIdPipes[detId.rawId()]=pipe;
183  }
184  }
185 }
186 
188 {
189 
190 
191  //resetup pipes if new iov
192  setupDetIdPipes(eventSetup);
193  //begin event
194  for (auto& algo: _recHitAlgorithms)
195  {
196  algo->beginEvent(event,eventSetup);
197  }
198 
199  //build DetId -> PSimHit map
201  event.getByToken(_simHitToken,simHits);
202 
203  auto output_recHits = std::make_unique<FastTrackerRecHitCollection>();
204  output_recHits->reserve(simHits->size());
205 
206  edm::RefProd<FastTrackerRecHitCollection> output_recHits_refProd = event.getRefBeforePut<FastTrackerRecHitCollection>();
207  auto output_recHitRefs = std::make_unique<FastTrackerRecHitRefCollection>(simHits->size(),FastTrackerRecHitRef());
208 
209  std::map<unsigned int,std::vector<std::pair<unsigned int,const PSimHit*>>> simHitsIdPairPerDetId;
210  for (unsigned int ihit = 0; ihit < simHits->size(); ++ihit)
211  {
212  const PSimHit* simHit = &(*simHits)[ihit];
213  simHitsIdPairPerDetId[simHit->detUnitId()].push_back(std::make_pair(ihit,simHit));
214  }
215 
216  for (auto simHitsIdPairIt = simHitsIdPairPerDetId.begin(); simHitsIdPairIt != simHitsIdPairPerDetId.end(); ++simHitsIdPairIt)
217  {
218  const DetId& detId = simHitsIdPairIt->first;
219  std::map<unsigned int, TrackingRecHitPipe>::const_iterator pipeIt = _detIdPipes.find(detId);
220  if (pipeIt!=_detIdPipes.cend())
221  {
222  auto& simHitIdPairList = simHitsIdPairIt->second;
223  const TrackingRecHitPipe& pipe = pipeIt->second;
224 
225  TrackingRecHitProductPtr product = std::make_shared<TrackingRecHitProduct>(detId,simHitIdPairList);
226 
227  product = pipe.produce(product);
228 
229  const std::vector<TrackingRecHitProduct::RecHitToSimHitIdPairs>& recHitToSimHitIdPairsList = product->getRecHitToSimHitIdPairs();
230 
231 
232  for (unsigned int irecHit = 0; irecHit < recHitToSimHitIdPairsList.size(); ++irecHit)
233  {
234  output_recHits->push_back(recHitToSimHitIdPairsList[irecHit].first);
235  const std::vector<TrackingRecHitProduct::SimHitIdPair>& simHitIdPairList = recHitToSimHitIdPairsList[irecHit].second;
236  double energyLoss_tot = 0;//
237  for (unsigned int isimHit = 0; isimHit < simHitIdPairList.size(); ++isimHit){
238  unsigned int simHitId = simHitIdPairList[isimHit].first;
239  const PSimHit * simHit = simHitIdPairList[isimHit].second;
240  energyLoss_tot+=simHit->energyLoss();//energy loss of sim hit in GeV std::cout << "energyLoss_tot" << energyLoss_tot << std::endl;
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  static_cast<FastSingleTrackerRecHit&>(output_recHits->back()).setEnergyLoss(energyLoss_tot);
248  }
249  }
250  else
251  {
252  //there should be at least an empty pipe for each DetId
253  throw cms::Exception("FastSimulation/TrackingRecHitProducer","A PSimHit carries a DetId which does not belong to the TrackerGeometry: "+std::to_string(detId));
254  }
255  }
256 
257  //end event
258  for (auto& algo: _recHitAlgorithms)
259  {
260  algo->endEvent(event,eventSetup);
261  }
262 
263  // note from lukas:
264  // all rechits need a unique id numbers
265  for(unsigned recHitIndex = 0; recHitIndex < output_recHits->size(); ++recHitIndex)
266  {
267  ((FastSingleTrackerRecHit*)&(*output_recHits)[recHitIndex])->setId(recHitIndex);
268  }
269 
270  event.put(std::move(output_recHits));
271  event.put(std::move(output_recHitRefs),"simHit2RecHitMap");
272 
273 }
274 
276 {
277  for (auto& algo: _recHitAlgorithms)
278  {
279  algo->endStream();
280  }
281 }
282 
T getParameter(std::string const &) const
unsigned long long cacheIdentifier() const
std::string dump(unsigned int indent=0) const
void setupDetIdPipes(const edm::EventSetup &eventSetup)
std::map< unsigned int, TrackingRecHitPipe > _detIdPipes
size_type size() const
Definition: OwnVector.h:264
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
unsigned long long _trackerGeometryCacheID
edm::Ref< FastTrackerRecHitCollection > FastTrackerRecHitRef
bool passSelection(std::string selectionStr) const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
ProductT const & get(ESGetToken< ProductT, DepRecordT > const &iToken) const
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
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
static bool pushfile(int filenum, std::vector< SiPixelTemplateStore > &pixelTemp, std::string dir="CalibTracker/SiPixelESProducers/data/")
TrackingRecHitProducer(const edm::ParameterSet &config)
float energyLoss() const
The energy deposit in the PSimHit, in ???.
Definition: PSimHit.h:79
void beginRun(edm::Run const &, const edm::EventSetup &eventSetup) override
std::vector< std::unique_ptr< TrackingRecHitAlgorithm > > _recHitAlgorithms
T get() const
Definition: EventSetup.h:71
std::vector< SiPixelTemplateStore > _pixelTempStore
unsigned long long _trackerTopologyCacheID
T const * product() const
Definition: ESHandle.h:86
Definition: pipe.py:1
TrackingRecHitProductPtr produce(TrackingRecHitProductPtr product) const
def move(src, dest)
Definition: eostools.py:511
T get(const Candidate &c)
Definition: component.h:55
unsigned int detUnitId() const
Definition: PSimHit.h:97
Definition: event.py:1
Definition: Run.h:45