CMS 3D CMS Logo

TrackingRecHitProducer.cc
Go to the documentation of this file.
10 
15 
18 
20 
23 
26 
29 // Pixel-related stuff:
33 
35 
38 
39 #include <map>
40 #include <memory>
41 #include <vector>
42 
44 private:
46  std::vector<std::unique_ptr<TrackingRecHitAlgorithm>> _recHitAlgorithms;
47  unsigned long long _trackerGeometryCacheID = 0;
48  unsigned long long _trackerTopologyCacheID = 0;
49  std::map<unsigned int, TrackingRecHitPipe> _detIdPipes;
51  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 
71  : siPixelTemplateDBObjectESToken_(esConsumes<edm::Transition::BeginRun>()),
72  trackerGeometryESToken_(esConsumes()),
73  trackerTopologyESToken_(esConsumes()) {
74  edm::ConsumesCollector consumeCollector = consumesCollector();
75  const std::vector<edm::ParameterSet>& pluginConfigs = config.getParameter<std::vector<edm::ParameterSet>>("plugins");
76 
77  for (unsigned int iplugin = 0; iplugin < pluginConfigs.size(); ++iplugin) {
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{
83  TrackingRecHitAlgorithmFactory::get()->tryToCreate(pluginType, pluginName, pluginConfig, consumeCollector)};
84  if (recHitAlgorithm) {
85  edm::LogInfo("TrackingRecHitProducer: ")
86  << "adding plugin type '" << pluginType << "' as '" << pluginName << "'" << std::endl;
87  _recHitAlgorithms.push_back(std::move(recHitAlgorithm));
88  } else {
89  throw cms::Exception("TrackingRecHitAlgorithm plugin not found: ")
90  << "plugin type = " << pluginType << "\nconfiguration=\n"
91  << pluginConfig.dump();
92  }
93  }
94 
95  edm::InputTag simHitTag = config.getParameter<edm::InputTag>("simHits");
96  _simHitToken = consumes<std::vector<PSimHit>>(simHitTag);
97 
98  produces<FastTrackerRecHitCollection>();
99  produces<FastTrackerRecHitRefCollection>("simHit2RecHitMap");
100 }
101 
103 
105  for (auto& algo : _recHitAlgorithms) {
106  algo->beginStream(id);
107  }
108 }
109 
111  //--- Since all pixel algorithms (of which there could be several) use the same
112  // templateStore, filled out from the same DB Object, we need to it centrally
113  // (namely here), and then distribute it to the algorithms. Note that only
114  // the pixel algorithms implement beginRun(), for the strip tracker this defaults
115  // to a no-op.
116 
117  const SiPixelTemplateDBObject& pixelTemplateDBObject = eventSetup.getData(siPixelTemplateDBObjectESToken_);
118 
119  //--- Now that we have the DB object, load the correct templates from the DB.
120  // (They are needed for data and full sim MC, so in a production FastSim
121  // run, everything should already be in the DB.)
122  if (!SiPixelTemplate::pushfile(pixelTemplateDBObject, _pixelTempStore)) {
123  throw cms::Exception("TrackingRecHitProducer:")
124  << "SiPixel Templates not loaded correctly from the DB object!" << std::endl;
125  }
126 
127  for (auto& algo : _recHitAlgorithms) {
128  algo->beginRun(run, eventSetup, &pixelTemplateDBObject, _pixelTempStore);
129  }
130 }
131 
133  auto const& trackerGeomRec = eventSetup.get<TrackerDigiGeometryRecord>();
134  auto const& trackerTopoRec = eventSetup.get<TrackerTopologyRcd>();
135  if (trackerGeomRec.cacheIdentifier() != _trackerGeometryCacheID or
136  trackerTopoRec.cacheIdentifier() != _trackerTopologyCacheID) {
137  _trackerGeometryCacheID = trackerGeomRec.cacheIdentifier();
138  _trackerTopologyCacheID = trackerTopoRec.cacheIdentifier();
139 
140  const TrackerGeometry& trackerGeometry = trackerGeomRec.get(trackerGeometryESToken_);
141  const TrackerTopology& trackerTopology = trackerTopoRec.get(trackerTopologyESToken_);
142 
143  _detIdPipes.clear();
144 
145  //build pipes for all detIds
146  const std::vector<DetId>& detIds = trackerGeometry.detIds();
147  std::vector<unsigned int> numberOfDetIdsPerAlgorithm(_recHitAlgorithms.size(), 0);
148 
149  for (const DetId& detId : detIds) {
150  TrackerDetIdSelector selector(detId, trackerTopology);
151 
153  for (unsigned int ialgo = 0; ialgo < _recHitAlgorithms.size(); ++ialgo) {
154  auto& algo = _recHitAlgorithms[ialgo];
155  if (selector.passSelection(algo->getSelectionString())) {
156  numberOfDetIdsPerAlgorithm[ialgo] += 1;
157  pipe.addAlgorithm(algo.get());
158  }
159  }
160  if (pipe.size() == 0) {
161  throw cms::Exception("FastSimulation/TrackingRecHitProducer: DetId not configured! (" +
162  trackerTopology.print(detId) + ")");
163  }
164  _detIdPipes[detId.rawId()] = pipe;
165  }
166  }
167 }
168 
170  //resetup pipes if new iov
172  //begin event
173  for (auto& algo : _recHitAlgorithms) {
174  algo->beginEvent(event, eventSetup);
175  }
176 
177  //build DetId -> PSimHit map
179  event.getByToken(_simHitToken, simHits);
180 
181  auto output_recHits = std::make_unique<FastTrackerRecHitCollection>();
182  output_recHits->reserve(simHits->size());
183 
184  edm::RefProd<FastTrackerRecHitCollection> output_recHits_refProd =
185  event.getRefBeforePut<FastTrackerRecHitCollection>();
186  auto output_recHitRefs = std::make_unique<FastTrackerRecHitRefCollection>(simHits->size(), FastTrackerRecHitRef());
187 
188  std::map<unsigned int, std::vector<std::pair<unsigned int, const PSimHit*>>> simHitsIdPairPerDetId;
189  for (unsigned int ihit = 0; ihit < simHits->size(); ++ihit) {
190  const PSimHit* simHit = &(*simHits)[ihit];
191  simHitsIdPairPerDetId[simHit->detUnitId()].push_back(std::make_pair(ihit, simHit));
192  }
193 
194  for (auto simHitsIdPairIt = simHitsIdPairPerDetId.begin(); simHitsIdPairIt != simHitsIdPairPerDetId.end();
195  ++simHitsIdPairIt) {
196  const DetId& detId = simHitsIdPairIt->first;
197  std::map<unsigned int, TrackingRecHitPipe>::const_iterator pipeIt = _detIdPipes.find(detId);
198  if (pipeIt != _detIdPipes.cend()) {
199  auto& simHitIdPairList = simHitsIdPairIt->second;
200  const TrackingRecHitPipe& pipe = pipeIt->second;
201 
202  TrackingRecHitProductPtr product = std::make_shared<TrackingRecHitProduct>(detId, simHitIdPairList);
203 
204  product = pipe.produce(product);
205 
206  const std::vector<TrackingRecHitProduct::RecHitToSimHitIdPairs>& recHitToSimHitIdPairsList =
207  product->getRecHitToSimHitIdPairs();
208 
209  for (unsigned int irecHit = 0; irecHit < recHitToSimHitIdPairsList.size(); ++irecHit) {
210  output_recHits->push_back(recHitToSimHitIdPairsList[irecHit].first);
211  const std::vector<TrackingRecHitProduct::SimHitIdPair>& simHitIdPairList =
212  recHitToSimHitIdPairsList[irecHit].second;
213  double energyLoss_tot = 0; //
214  for (unsigned int isimHit = 0; isimHit < simHitIdPairList.size(); ++isimHit) {
215  unsigned int simHitId = simHitIdPairList[isimHit].first;
216  const PSimHit* simHit = simHitIdPairList[isimHit].second;
217  energyLoss_tot +=
218  simHit
219  ->energyLoss(); //energy loss of sim hit in GeV std::cout << "energyLoss_tot" << energyLoss_tot << std::endl;
220  if (not(*output_recHitRefs)[simHitId].isNull()) {
221  throw cms::Exception("FastSimulation/TrackingRecHitProducer",
222  "A PSimHit cannot lead to multiple FastTrackerRecHits");
223  }
224  (*output_recHitRefs)[simHitId] = FastTrackerRecHitRef(output_recHits_refProd, output_recHits->size() - 1);
225  }
226  static_cast<FastSingleTrackerRecHit&>(output_recHits->back()).setEnergyLoss(energyLoss_tot);
227  }
228  } else {
229  //there should be at least an empty pipe for each DetId
230  throw cms::Exception(
231  "FastSimulation/TrackingRecHitProducer",
232  "A PSimHit carries a DetId which does not belong to the TrackerGeometry: " + std::to_string(detId));
233  }
234  }
235 
236  //end event
237  for (auto& algo : _recHitAlgorithms) {
238  algo->endEvent(event, eventSetup);
239  }
240 
241  // note from lukas:
242  // all rechits need a unique id numbers
243  for (unsigned recHitIndex = 0; recHitIndex < output_recHits->size(); ++recHitIndex) {
244  ((FastSingleTrackerRecHit*)&(*output_recHits)[recHitIndex])->setId(recHitIndex);
245  }
246 
247  event.put(std::move(output_recHits));
248  event.put(std::move(output_recHitRefs), "simHit2RecHitMap");
249 }
250 
252  for (auto& algo : _recHitAlgorithms) {
253  algo->endStream();
254  }
255 }
256 
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
bool passSelection(const std::string &selectionStr) const
const edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > trackerTopologyESToken_
const DetIdContainer & detIds() const override
Returm a vector of all GeomDet DetIds (including those of GeomDetUnits)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::string print(DetId detid) const
void setupDetIdPipes(const edm::EventSetup &eventSetup)
std::map< unsigned int, TrackingRecHitPipe > _detIdPipes
void beginStream(edm::StreamID id) override
std::string to_string(const V &value)
Definition: OMSAccess.h:71
edm::EDGetTokenT< std::vector< PSimHit > > _simHitToken
Definition: config.py:1
unsigned long long _trackerGeometryCacheID
edm::Ref< FastTrackerRecHitCollection > FastTrackerRecHitRef
unsigned long long cacheIdentifier() const
std::vector< SiPixelTemplateStore > _pixelTempStore
const edm::ESGetToken< TrackerGeometry, TrackerDigiGeometryRecord > trackerGeometryESToken_
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
size_type size() const
Definition: OwnVector.h:300
void produce(edm::Event &event, const edm::EventSetup &eventSetup) override
Transition
Definition: Transition.h:12
def pipe(cmdline, input=None)
Definition: pipe.py:5
std::shared_ptr< TrackingRecHitProduct > TrackingRecHitProductPtr
Log< level::Info, false > LogInfo
const edm::ESGetToken< SiPixelTemplateDBObject, SiPixelTemplateDBObjectESProducerRcd > siPixelTemplateDBObjectESToken_
Definition: DetId.h:17
static bool pushfile(int filenum, std::vector< SiPixelTemplateStore > &pixelTemp, std::string dir="CalibTracker/SiPixelESProducers/data/")
TrackingRecHitProducer(const edm::ParameterSet &config)
HLT enums.
void beginRun(edm::Run const &, const edm::EventSetup &eventSetup) override
std::vector< std::unique_ptr< TrackingRecHitAlgorithm > > _recHitAlgorithms
unsigned long long _trackerTopologyCacheID
#define get
ProductT const & get(ESGetToken< ProductT, DepRecordT > const &iToken) const
Definition: pipe.py:1
def move(src, dest)
Definition: eostools.py:511
Definition: event.py:1
Definition: Run.h:45