CMS 3D CMS Logo

CaloTruthCellsProducer.cc
Go to the documentation of this file.
1 #include <memory>
2 
25 
27 public:
29  ~CaloTruthCellsProducer() override;
30 
31  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
32 
33 private:
34  void produce(edm::Event&, edm::EventSetup const&) override;
35 
36  std::unordered_map<uint32_t, double> makeHitMap(edm::Event const&,
37  edm::EventSetup const&,
38  HGCalTriggerGeometryBase const&) const;
39 
41 
48 
51 
53 };
54 
56  : makeCellsCollection_(config.getParameter<bool>("makeCellsCollection")),
57  caloParticlesToken_(consumes<CaloParticleCollection>(config.getParameter<edm::InputTag>("caloParticles"))),
58  triggerCellsToken_(
59  consumes<l1t::HGCalTriggerCellBxCollection>(config.getParameter<edm::InputTag>("triggerCells"))),
60  simHitsTokenEE_(consumes<std::vector<PCaloHit>>(config.getParameter<edm::InputTag>("simHitsEE"))),
61  simHitsTokenHEfront_(consumes<std::vector<PCaloHit>>(config.getParameter<edm::InputTag>("simHitsHEfront"))),
62  simHitsTokenHEback_(consumes<std::vector<PCaloHit>>(config.getParameter<edm::InputTag>("simHitsHEback"))),
63  dummyClustering_(config.getParameterSet("dummyClustering")) {
64  produces<CaloToCellsMap>();
65  produces<l1t::HGCalClusterBxCollection>();
66  produces<l1t::HGCalMulticlusterBxCollection>();
68  produces<l1t::HGCalTriggerCellBxCollection>();
69 }
70 
72 
74  edm::Handle<CaloParticleCollection> caloParticlesHandle;
75  event.getByToken(caloParticlesToken_, caloParticlesHandle);
76  auto const& caloParticles(*caloParticlesHandle);
77 
79  event.getByToken(triggerCellsToken_, triggerCellsHandle);
80  auto const& triggerCells(*triggerCellsHandle);
81 
85 
87  setup.get<CaloGeometryRecord>().get(geometryHandle);
88  auto const& geometry(*geometryHandle);
89 
90  std::unordered_map<uint32_t, CaloParticleRef> tcToCalo;
91 
92  std::unordered_map<uint32_t, double> hitToEnergy(makeHitMap(event, setup, geometry)); // cellId -> sim energy
93  std::unordered_map<uint32_t, std::pair<double, double>>
94  tcToEnergies; // tcId -> {total sim energy, fractioned sim energy}
95 
96  for (auto const& he : hitToEnergy) {
97  DetId hitId(he.first);
98  // this line will throw if (for whatever reason) hitId is not mapped to a trigger cell id
99  uint32_t tcId(geometry.getTriggerCellFromCell(hitId));
100  tcToEnergies[tcId].first += he.second;
101  }
102 
103  // used later to order multiclusters
104  std::map<int, CaloParticleRef> orderedCaloRefs;
105 
106  for (unsigned iP(0); iP != caloParticles.size(); ++iP) {
107  auto const& caloParticle(caloParticles.at(iP));
108  if (caloParticle.g4Tracks().at(0).eventId().event() != 0) // pileup
109  continue;
110 
111  CaloParticleRef ref(caloParticlesHandle, iP);
112 
113  SimClusterRefVector const& simClusters(caloParticle.simClusters());
114  for (auto const& simCluster : simClusters) {
115  for (auto const& hAndF : simCluster->hits_and_fractions()) {
116  DetId hitId(hAndF.first);
117  uint32_t tcId;
118  try {
119  tcId = geometry.getTriggerCellFromCell(hitId);
120  } catch (cms::Exception const& ex) {
121  edm::LogError("CaloTruthCellsProducer") << ex.what();
122  continue;
123  }
124 
125  tcToCalo.emplace(tcId, ref);
126  tcToEnergies[tcId].second += hitToEnergy[hAndF.first] * hAndF.second;
127  }
128  }
129 
130  // ordered by the gen particle index
131  int genIndex(caloParticle.g4Tracks().at(0).genpartIndex() - 1);
132  orderedCaloRefs[genIndex] = ref;
133  }
134 
135  auto outMap(std::make_unique<CaloToCellsMap>(caloParticlesHandle, triggerCellsHandle));
136  std::unique_ptr<l1t::HGCalTriggerCellBxCollection> outCollection;
138  outCollection = std::make_unique<l1t::HGCalTriggerCellBxCollection>();
139 
140  typedef edm::Ptr<l1t::HGCalTriggerCell> TriggerCellPtr;
141  typedef edm::Ptr<l1t::HGCalCluster> ClusterPtr;
142 
143  // ClusteringDummyImpl only considers BX 0, so we dump all cells to one vector
144  std::vector<TriggerCellPtr> triggerCellPtrs;
145 
146  // loop through all bunch crossings
147  for (int bx(triggerCells.getFirstBX()); bx <= triggerCells.getLastBX(); ++bx) {
148  for (auto cItr(triggerCells.begin(bx)); cItr != triggerCells.end(bx); ++cItr) {
149  auto const& cell(*cItr);
150 
151  auto mapElem(tcToCalo.find(cell.detId()));
152  if (mapElem == tcToCalo.end())
153  continue;
154 
155  outMap->insert(mapElem->second,
156  edm::Ref<l1t::HGCalTriggerCellBxCollection>(triggerCellsHandle, triggerCells.key(cItr)));
157 
158  if (makeCellsCollection_) {
159  auto const& simEnergies(tcToEnergies.at(cell.detId()));
160  if (simEnergies.first > 0.) {
161  double eFraction(simEnergies.second / simEnergies.first);
162 
163  outCollection->push_back(bx, cell);
164  auto& newCell((*outCollection)[outCollection->size() - 1]);
165 
166  newCell.setMipPt(cell.mipPt() * eFraction);
167  newCell.setP4(cell.p4() * eFraction);
168  }
169  }
170 
171  triggerCellPtrs.emplace_back(triggerCellsHandle, triggerCells.key(cItr));
172  }
173  }
174 
175  event.put(std::move(outMap));
177  event.put(std::move(outCollection));
178 
179  auto outClusters(std::make_unique<l1t::HGCalClusterBxCollection>());
180 
181  auto sortCellPtrs(
182  [](TriggerCellPtr const& lhs, TriggerCellPtr const& rhs) -> bool { return lhs->mipPt() > rhs->mipPt(); });
183 
184  std::sort(triggerCellPtrs.begin(), triggerCellPtrs.end(), sortCellPtrs);
185  dummyClustering_.clusterizeDummy(triggerCellPtrs, *outClusters);
186 
187  std::unordered_map<unsigned, std::vector<unsigned>> caloToClusterIndices;
188  for (unsigned iC(0); iC != outClusters->size(); ++iC) {
189  auto const& cluster((*outClusters)[iC]);
190  // cluster detId and cell detId are identical
191  auto caloRef(tcToCalo.at(cluster.detId()));
192  caloToClusterIndices[caloRef.key()].push_back(iC);
193  }
194 
195  auto clustersHandle(event.put(std::move(outClusters)));
196 
197  auto outMulticlusters(std::make_unique<l1t::HGCalMulticlusterBxCollection>());
198 
199  for (auto const& ocr : orderedCaloRefs) {
200  auto const& ref(ocr.second);
201 
202  if (ref.isNull()) // shouldn't happen
203  continue;
204 
205  auto const& caloParticle(*ref);
206 
207  l1t::HGCalMulticluster multicluster;
208 
209  for (unsigned iC : caloToClusterIndices[ref.key()]) {
210  ClusterPtr clPtr(clustersHandle, iC);
211  multicluster.addConstituent(clPtr, true, 1.);
212  }
213 
214  // Set the gen particle index as the DetId
215  multicluster.setDetId(caloParticle.g4Tracks().at(0).genpartIndex() - 1);
216 
217  auto const& centre(multicluster.centre());
218  math::PtEtaPhiMLorentzVector multiclusterP4(multicluster.sumPt(), centre.eta(), centre.phi(), 0.);
219  multicluster.setP4(multiclusterP4);
220 
221  showerShape_.fillShapes(multicluster, geometry);
222 
223  // not setting the quality flag
224  // multicluster.setHwQual(id_->decision(multicluster));
225  // fill H/E
226  multicluster.saveHOverE();
227 
228  outMulticlusters->push_back(0, multicluster);
229  }
230 
231  event.put(std::move(outMulticlusters));
232 }
233 
234 std::unordered_map<uint32_t, double> CaloTruthCellsProducer::makeHitMap(
236  std::unordered_map<uint32_t, double> hitMap; // cellId -> sim energy
237 
238  typedef std::function<DetId(DetId const&)> DetIdMapper;
239  typedef std::pair<edm::EDGetTokenT<std::vector<PCaloHit>> const*, DetIdMapper> SimHitSpec;
240 
241  SimHitSpec specs[3] = {{&simHitsTokenEE_,
242  [this, &geometry](DetId const& simId) -> DetId {
243  return this->triggerTools_.simToReco(simId, geometry.eeTopology());
244  }},
246  [this, &geometry](DetId const& simId) -> DetId {
247  return this->triggerTools_.simToReco(simId, geometry.fhTopology());
248  }},
249  {&simHitsTokenHEback_, nullptr}};
250  if (geometry.isV9Geometry())
251  specs[2].second = [this, &geometry](DetId const& simId) -> DetId {
252  return this->triggerTools_.simToReco(simId, geometry.hscTopology());
253  };
254  else
255  specs[2].second = [this, &geometry](DetId const& simId) -> DetId {
256  return this->triggerTools_.simToReco(simId, geometry.bhTopology());
257  };
258 
259  for (auto const& tt : specs) {
261  event.getByToken(*tt.first, handle);
262  auto const& simhits(*handle);
263 
264  for (auto const& simhit : simhits)
265  hitMap.emplace(tt.second(simhit.id()), simhit.energy());
266  }
267 
268  return hitMap;
269 }
270 
272  //The following says we do not know what parameters are allowed so do no validation
273  // Please change this to state exactly what you do use, even if it is no parameters
275  desc.setUnknown();
276  descriptions.addDefault(desc);
277 }
278 
CaloTruthCellsProducer::makeCellsCollection_
bool makeCellsCollection_
Definition: CaloTruthCellsProducer.cc:42
l1t::HGCalClusterT::setDetId
void setDetId(uint32_t id)
Definition: HGCalClusterT.h:94
HGCalTriggerTools.h
HGCalTriggerGeometryBase
Definition: HGCalTriggerGeometryBase.h:19
HGCalShowerShape::fillShapes
void fillShapes(l1t::HGCalMulticluster &, const HGCalTriggerGeometryBase &) const
Definition: HGCalShowerShape.cc:515
HGCalTriggerTools::eventSetup
void eventSetup(const edm::EventSetup &)
Definition: HGCalTriggerTools.cc:35
CaloTruthCellsProducer::simHitsTokenHEfront_
edm::EDGetTokenT< std::vector< PCaloHit > > simHitsTokenHEfront_
Definition: CaloTruthCellsProducer.cc:46
electrons_cff.bool
bool
Definition: electrons_cff.py:366
CaloTruthCellsProducer::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: CaloTruthCellsProducer.cc:271
l1t::HGCalMulticluster::saveHOverE
void saveHOverE()
Definition: HGCalMulticluster.h:31
HGCalShowerShape
Definition: HGCalShowerShape.h:12
ESHandle.h
groupFilesInBlocks.tt
int tt
Definition: groupFilesInBlocks.py:144
l1t::HGCalClusterT::sumPt
double sumPt() const
Definition: HGCalClusterT.h:96
patZpeak.handle
handle
Definition: patZpeak.py:23
CaloTruthCellsProducer::caloParticlesToken_
edm::EDGetTokenT< CaloParticleCollection > caloParticlesToken_
Definition: CaloTruthCellsProducer.cc:43
edm::EDGetTokenT< CaloParticleCollection >
CaloGeometryRecord
Definition: CaloGeometryRecord.h:30
edm
HLT enums.
Definition: AlignableModifier.h:19
AssociationMap.h
geometry
Definition: geometry.py:1
HistogramManager_cfi.specs
specs
Definition: HistogramManager_cfi.py:83
CaloParticleFwd.h
HLT_FULL_cff.InputTag
InputTag
Definition: HLT_FULL_cff.py:89285
l1GtPatternGenerator_cfi.bx
bx
Definition: l1GtPatternGenerator_cfi.py:18
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
EDProducer.h
edm::second
U second(std::pair< T, U > const &p)
Definition: ParameterSet.cc:222
relativeConstraints.geometry
geometry
Definition: relativeConstraints.py:39
CaloTruthCellsProducer::showerShape_
HGCalShowerShape showerShape_
Definition: CaloTruthCellsProducer.cc:50
edm::RefVector< SimClusterCollection >
cms::Exception::what
char const * what() const noexcept override
Definition: Exception.cc:103
edm::Handle
Definition: AssociativeIterator.h:50
singleTopDQM_cfi.setup
setup
Definition: singleTopDQM_cfi.py:37
HGCalShowerShape.h
edm::Ref
Definition: AssociativeIterator.h:58
CaloTruthCellsProducer::triggerTools_
HGCalTriggerTools triggerTools_
Definition: CaloTruthCellsProducer.cc:52
config
Definition: config.py:1
DetId
Definition: DetId.h:17
l1t::HGCalMulticluster
Definition: HGCalMulticluster.h:13
MakerMacros.h
CaloTruthCellsProducer
Definition: CaloTruthCellsProducer.cc:26
HGCalClusteringDummyImpl::eventSetup
void eventSetup(const edm::EventSetup &es)
Definition: HGCalClusteringDummyImpl.h:17
SimCluster.h
HGCalMulticluster.h
CaloTruthCellsProducer::CaloToCellsMap
edm::AssociationMap< edm::OneToMany< CaloParticleCollection, l1t::HGCalTriggerCellBxCollection > > CaloToCellsMap
Definition: CaloTruthCellsProducer.cc:40
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
HGCalTriggerGeometryBase.h
l1t::HGCalTriggerCellBxCollection
BXVector< HGCalTriggerCell > HGCalTriggerCellBxCollection
Definition: HGCalTriggerCell.h:11
caloTruthCellsProducer_cfi.caloParticles
caloParticles
Definition: caloTruthCellsProducer_cfi.py:6
edm::ESHandle< HGCalTriggerGeometryBase >
OneToMany.h
CaloParticleCollection
std::vector< CaloParticle > CaloParticleCollection
Definition: CaloParticleFwd.h:8
CaloGeometryRecord.h
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
HGCalClusteringDummyImpl::clusterizeDummy
void clusterizeDummy(const std::vector< edm::Ptr< l1t::HGCalTriggerCell >> &triggerCellsPtrs, l1t::HGCalClusterBxCollection &clusters)
Definition: HGCalClusteringDummyImpl.cc:16
CaloTruthCellsProducer::dummyClustering_
HGCalClusteringDummyImpl dummyClustering_
Definition: CaloTruthCellsProducer.cc:49
HLTBitAnalyser_cfi.simhits
simhits
SIM objects.
Definition: HLTBitAnalyser_cfi.py:21
edm::ParameterSet
Definition: ParameterSet.h:47
Event.h
l1t::HGCalClusterT::centre
const GlobalPoint & centre() const
Definition: HGCalClusterT.h:101
HGCalClusteringDummyImpl.h
l1t
delete x;
Definition: CaloConfig.h:22
edm::AssociationMap
Definition: AssociationMap.h:48
jetUpdater_cfi.sort
sort
Definition: jetUpdater_cfi.py:29
CaloParticle.h
trackerHitRTTI::vector
Definition: trackerHitRTTI.h:21
edm::stream::EDProducer
Definition: EDProducer.h:38
HGCalShowerShape::eventSetup
void eventSetup(const edm::EventSetup &es)
Definition: HGCalShowerShape.h:21
CaloTruthCellsProducer::produce
void produce(edm::Event &, edm::EventSetup const &) override
Definition: CaloTruthCellsProducer.cc:73
edm::EventSetup
Definition: EventSetup.h:58
CaloTruthCellsProducer::simHitsTokenEE_
edm::EDGetTokenT< std::vector< PCaloHit > > simHitsTokenEE_
Definition: CaloTruthCellsProducer.cc:45
PCaloHit
Definition: PCaloHit.h:8
l1t::HGCalClusterT::addConstituent
void addConstituent(const edm::Ptr< C > &c, bool updateCentre=true, float fraction=1.)
Definition: HGCalClusterT.h:55
edm::LogError
Log< level::Error, false > LogError
Definition: MessageLogger.h:123
HGCalTriggerTools::simToReco
DetId simToReco(const DetId &, const HGCalTopology &) const
Definition: HGCalTriggerTools.cc:346
get
#define get
edm::Ptr
Definition: AssociationVector.h:31
CaloTruthCellsProducer::CaloTruthCellsProducer
CaloTruthCellsProducer(edm::ParameterSet const &)
Definition: CaloTruthCellsProducer.cc:55
edm::getParameterSet
ParameterSet const & getParameterSet(ParameterSetID const &id)
Definition: ParameterSet.cc:862
reco::LeafCandidate::setP4
void setP4(const LorentzVector &p4) final
set 4-momentum
Definition: LeafCandidate.h:158
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
HGCalDetId.h
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
Ref.h
hcalSimParameters_cfi.he
he
Definition: hcalSimParameters_cfi.py:79
Frameworkfwd.h
CaloTruthCellsProducer::makeHitMap
std::unordered_map< uint32_t, double > makeHitMap(edm::Event const &, edm::EventSetup const &, HGCalTriggerGeometryBase const &) const
Definition: CaloTruthCellsProducer.cc:234
HiBiasedCentrality_cfi.function
function
Definition: HiBiasedCentrality_cfi.py:4
EventSetup.h
HGCalTriggerTools
Definition: HGCalTriggerTools.h:32
HGCalTriggerCell.h
math::PtEtaPhiMLorentzVector
PtEtaPhiMLorentzVectorD PtEtaPhiMLorentzVector
Lorentz vector with cartesian internal representation.
Definition: LorentzVector.h:25
cms::Exception
Definition: Exception.h:70
ParameterSet.h
event
Definition: event.py:1
edm::Event
Definition: Event.h:73
edm::ConfigurationDescriptions::addDefault
void addDefault(ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:99
StreamID.h
CaloTruthCellsProducer::triggerCellsToken_
edm::EDGetTokenT< l1t::HGCalTriggerCellBxCollection > triggerCellsToken_
Definition: CaloTruthCellsProducer.cc:44
caloTruthCellsProducer_cfi.triggerCells
triggerCells
Definition: caloTruthCellsProducer_cfi.py:7
CaloTruthCellsProducer::simHitsTokenHEback_
edm::EDGetTokenT< std::vector< PCaloHit > > simHitsTokenHEback_
Definition: CaloTruthCellsProducer.cc:47
CaloTruthCellsProducer::~CaloTruthCellsProducer
~CaloTruthCellsProducer() override
Definition: CaloTruthCellsProducer.cc:71
HGCalClusteringDummyImpl
Definition: HGCalClusteringDummyImpl.h:13