CMS 3D CMS Logo

L1TCaloSummary.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: L1Trigger/L1TCaloSummary
4 // Class: L1TCaloSummary
5 //
13 //
14 // Original Author: Sridhara Dasu
15 // Created: Sat, 14 Nov 2015 14:18:27 GMT
16 //
17 //
18 
19 // system include files
20 #include <memory>
21 
22 // user include files
25 
28 
30 
33 
34 #include "L1Trigger/L1TCaloLayer1/src/UCTLayer1.hh"
35 #include "L1Trigger/L1TCaloLayer1/src/UCTCrate.hh"
36 #include "L1Trigger/L1TCaloLayer1/src/UCTCard.hh"
37 #include "L1Trigger/L1TCaloLayer1/src/UCTRegion.hh"
38 #include "L1Trigger/L1TCaloLayer1/src/UCTTower.hh"
39 #include "L1Trigger/L1TCaloLayer1/src/UCTGeometry.hh"
40 
41 #include "L1Trigger/L1TCaloLayer1/src/UCTObject.hh"
42 #include "L1Trigger/L1TCaloLayer1/src/UCTSummaryCard.hh"
43 #include "L1Trigger/L1TCaloLayer1/src/UCTGeometryExtended.hh"
44 
51 
54 
56 
57 #include "L1Trigger/L1TCaloLayer1/src/UCTLogging.hh"
58 #include <bitset>
59 
60 //Anomaly detection includes
61 #include "ap_fixed.h"
62 #include "hls4ml/emulator.h"
63 
64 using namespace l1tcalo;
65 using namespace l1extra;
66 using namespace std;
67 
68 //
69 // class declaration
70 //
71 
72 template <class INPUT, class OUTPUT>
74 public:
75  explicit L1TCaloSummary(const edm::ParameterSet&);
76  ~L1TCaloSummary() override = default;
77 
78  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
79 
80 private:
81  //void beginJob() override;
82  void produce(edm::Event&, const edm::EventSetup&) override;
83  //void endJob() override;
84 
85  void beginRun(edm::Run const&, edm::EventSetup const&) override{};
86 
87  void print();
88 
89  // ----------member data ---------------------------
90 
91  uint32_t nPumBins;
92 
93  std::vector<std::vector<std::vector<uint32_t>>> pumLUT;
94 
96 
97  uint32_t jetSeed;
98  uint32_t tauSeed;
100  uint32_t eGammaSeed;
103 
104  bool verbose;
106 
108 
109  UCTLayer1* layer1;
110 
111  hls4mlEmulator::ModelLoader loader;
112  std::shared_ptr<hls4mlEmulator::Model> model;
113 };
114 
115 //
116 // constants, enums and typedefs
117 //
118 
119 //
120 // static data member definitions
121 //
122 
123 //
124 // constructors and destructor
125 //
126 template <class INPUT, class OUTPUT>
128  : nPumBins(iConfig.getParameter<unsigned int>("nPumBins")),
129  pumLUT(nPumBins, std::vector<std::vector<uint32_t>>(2, std::vector<uint32_t>(13))),
130  caloScaleFactor(iConfig.getParameter<double>("caloScaleFactor")),
131  jetSeed(iConfig.getParameter<unsigned int>("jetSeed")),
132  tauSeed(iConfig.getParameter<unsigned int>("tauSeed")),
133  tauIsolationFactor(iConfig.getParameter<double>("tauIsolationFactor")),
134  eGammaSeed(iConfig.getParameter<unsigned int>("eGammaSeed")),
135  eGammaIsolationFactor(iConfig.getParameter<double>("eGammaIsolationFactor")),
136  boostedJetPtFactor(iConfig.getParameter<double>("boostedJetPtFactor")),
137  verbose(iConfig.getParameter<bool>("verbose")),
138  fwVersion(iConfig.getParameter<int>("firmwareVersion")),
139  regionToken(consumes<L1CaloRegionCollection>(edm::InputTag("simCaloStage2Layer1Digis"))),
140  loader(hls4mlEmulator::ModelLoader(iConfig.getParameter<string>("CICADAModelVersion"))) {
141  std::vector<double> pumLUTData;
142  char pumLUTString[10];
143  for (uint32_t pumBin = 0; pumBin < nPumBins; pumBin++) {
144  for (uint32_t side = 0; side < 2; side++) {
145  if (side == 0)
146  sprintf(pumLUTString, "pumLUT%2.2dp", pumBin);
147  else
148  sprintf(pumLUTString, "pumLUT%2.2dn", pumBin);
149  pumLUTData = iConfig.getParameter<std::vector<double>>(pumLUTString);
150  for (uint32_t iEta = 0; iEta < std::max((uint32_t)pumLUTData.size(), MaxUCTRegionsEta); iEta++) {
151  pumLUT[pumBin][side][iEta] = (uint32_t)round(pumLUTData[iEta] / caloScaleFactor);
152  }
153  if (pumLUTData.size() != (MaxUCTRegionsEta))
154  edm::LogError("L1TCaloSummary") << "PUM LUT Data size integrity check failed; Expected size = "
155  << MaxUCTRegionsEta << "; Provided size = " << pumLUTData.size()
156  << "; Will use what is provided :(" << std::endl;
157  }
158  }
159  produces<L1JetParticleCollection>("Boosted");
160 
161  //anomaly trigger loading
162  model = loader.load_model();
163  produces<float>("CICADAScore");
164 }
165 
166 //
167 // member functions
168 //
169 
170 // ------------ method called to produce the data ------------
171 template <class INPUT, class OUTPUT>
173  using namespace edm;
174 
175  std::unique_ptr<L1JetParticleCollection> bJetCands(new L1JetParticleCollection);
176 
177  std::unique_ptr<float> CICADAScore = std::make_unique<float>();
178 
179  UCTGeometry g;
180 
181  // Here we read region data from the region collection created by L1TCaloLayer1 instead of
182  // independently creating regions from TPGs for processing by the summary card. This results
183  // in a single region vector of size 252 whereas from independent creation we had 3*6 vectors
184  // of size 7*2. Indices are mapped in UCTSummaryCard accordingly.
185  UCTSummaryCard summaryCard =
187  std::vector<UCTRegion*> inputRegions;
188  inputRegions.clear();
189  edm::Handle<std::vector<L1CaloRegion>> regionCollection;
190  if (!iEvent.getByToken(regionToken, regionCollection))
191  edm::LogError("L1TCaloSummary") << "UCT: Failed to get regions from region collection!";
192  iEvent.getByToken(regionToken, regionCollection);
193  //Model input
194  //This is done as a flat vector input, but future versions may involve 2D input
195  //This will have to be handled later
196  INPUT modelInput[252];
197  for (const L1CaloRegion& i : *regionCollection) {
198  UCTRegionIndex r = g.getUCTRegionIndexFromL1CaloRegion(i.gctEta(), i.gctPhi());
199  UCTTowerIndex t = g.getUCTTowerIndexFromL1CaloRegion(r, i.raw());
200  uint32_t absCaloEta = std::abs(t.first);
201  uint32_t absCaloPhi = std::abs(t.second);
202  bool negativeEta = false;
203  if (t.first < 0)
204  negativeEta = true;
205  uint32_t crate = g.getCrate(t.first, t.second);
206  uint32_t card = g.getCard(t.first, t.second);
207  uint32_t region = g.getRegion(absCaloEta, absCaloPhi);
208  UCTRegion* test = new UCTRegion(crate, card, negativeEta, region, fwVersion);
209  test->setRegionSummary(i.raw());
210  inputRegions.push_back(test);
211  //This *should* fill the tensor in the proper order to be fed to the anomaly model
212  //We take 4 off of the GCT eta/iEta.
213  //iEta taken from this ranges from 4-17, (I assume reserving lower and higher for forward regions)
214  //So our first index, index 0, is technically iEta=4, and so-on.
215  //CICADA reads this as a flat vector
216  modelInput[14 * i.gctPhi() + (i.gctEta() - 4)] = i.et();
217  }
218  //Extract model output
219  OUTPUT modelResult[1] = {
220  OUTPUT("0.0", 10)}; //the 10 here refers to the fact that we read in "0.0" as a decimal number
221  model->prepare_input(modelInput);
222  model->predict();
223  model->read_result(modelResult);
224 
225  *CICADAScore = modelResult[0].to_float();
226 
227  summaryCard.setRegionData(inputRegions);
228 
229  if (!summaryCard.process()) {
230  edm::LogError("L1TCaloSummary") << "UCT: Failed to process summary card" << std::endl;
231  exit(1);
232  }
233 
234  double pt = 0;
235  double eta = -999.;
236  double phi = -999.;
237  double mass = 0;
238 
239  std::list<UCTObject*> boostedJetObjs = summaryCard.getBoostedJetObjs();
240  for (std::list<UCTObject*>::const_iterator i = boostedJetObjs.begin(); i != boostedJetObjs.end(); i++) {
241  const UCTObject* object = *i;
242  pt = ((double)object->et()) * caloScaleFactor * boostedJetPtFactor;
243  eta = g.getUCTTowerEta(object->iEta());
244  phi = g.getUCTTowerPhi(object->iPhi());
245  bitset<3> activeRegionEtaPattern = 0;
246  for (uint32_t iEta = 0; iEta < 3; iEta++) {
247  bool activeStrip = false;
248  for (uint32_t iPhi = 0; iPhi < 3; iPhi++) {
249  if (object->boostedJetRegionET()[3 * iEta + iPhi] > 30 &&
250  object->boostedJetRegionET()[3 * iEta + iPhi] > object->et() * 0.0625)
251  activeStrip = true;
252  }
253  if (activeStrip)
254  activeRegionEtaPattern |= (0x1 << iEta);
255  }
256  bitset<3> activeRegionPhiPattern = 0;
257  for (uint32_t iPhi = 0; iPhi < 3; iPhi++) {
258  bool activeStrip = false;
259  for (uint32_t iEta = 0; iEta < 3; iEta++) {
260  if (object->boostedJetRegionET()[3 * iEta + iPhi] > 30 &&
261  object->boostedJetRegionET()[3 * iEta + iPhi] > object->et() * 0.0625)
262  activeStrip = true;
263  }
264  if (activeStrip)
265  activeRegionPhiPattern |= (0x1 << iPhi);
266  }
267  string regionEta = activeRegionEtaPattern.to_string<char, std::string::traits_type, std::string::allocator_type>();
268  string regionPhi = activeRegionPhiPattern.to_string<char, std::string::traits_type, std::string::allocator_type>();
269 
270  bool centralHighest = object->boostedJetRegionET()[4] >= object->boostedJetRegionET()[0] &&
271  object->boostedJetRegionET()[4] >= object->boostedJetRegionET()[1] &&
272  object->boostedJetRegionET()[4] >= object->boostedJetRegionET()[2] &&
273  object->boostedJetRegionET()[4] >= object->boostedJetRegionET()[3] &&
274  object->boostedJetRegionET()[4] >= object->boostedJetRegionET()[5] &&
275  object->boostedJetRegionET()[4] >= object->boostedJetRegionET()[6] &&
276  object->boostedJetRegionET()[4] >= object->boostedJetRegionET()[7] &&
277  object->boostedJetRegionET()[4] >= object->boostedJetRegionET()[8];
278 
279  if (abs(eta) < 2.5 && ((regionEta == "101" && (regionPhi == "110" || regionPhi == "101" || regionPhi == "010")) ||
280  ((regionEta == "110" || regionEta == "101" || regionEta == "010") && regionPhi == "101") ||
281  (regionEta == "111" && (regionPhi == "110" || regionPhi == "010")) ||
282  ((regionEta == "110" || regionEta == "010") && regionPhi == "111") ||
283  ((regionEta == "010" || regionPhi == "010" || regionEta == "110" || regionPhi == "110" ||
284  regionEta == "011" || regionPhi == "011") &&
285  centralHighest)))
286  bJetCands->push_back(L1JetParticle(math::PtEtaPhiMLorentzVector(pt, eta, phi, mass), L1JetParticle::kCentral));
287  }
288 
289  iEvent.put(std::move(bJetCands), "Boosted");
290  //Write out anomaly score
291  iEvent.put(std::move(CICADAScore), "CICADAScore");
292 }
293 
294 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
295 template <class INPUT, class OUTPUT>
297  //The following says we do not know what parameters are allowed so do no validation
298  // Please change this to state exactly what you do use, even if it is no parameters
300  desc.setUnknown();
301  descriptions.addDefault(desc);
302 }
303 
306 //define type version plugins
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
L1TCaloSummary< ap_ufixed< 10, 10 >, ap_fixed< 11, 5 > > L1TCaloSummaryCICADAv1
float tauIsolationFactor
bool verbose
L1TCaloSummary< ap_uint< 10 >, ap_ufixed< 16, 8 > > L1TCaloSummaryCICADAv2
Log< level::Error, false > LogError
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 g
Definition: Activities.doc:4
PtEtaPhiMLorentzVectorD PtEtaPhiMLorentzVector
Lorentz vector with cartesian internal representation.
Definition: LorentzVector.h:25
L1TCaloSummary(const edm::ParameterSet &)
int iEvent
Definition: GenABIO.cc:224
void addDefault(ParameterSetDescription const &psetDescription)
UCTLayer1 * layer1
void produce(edm::Event &, const edm::EventSetup &) override
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::EDGetTokenT< L1CaloRegionCollection > regionToken
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::shared_ptr< hls4mlEmulator::Model > model
double boostedJetPtFactor
double eGammaIsolationFactor
hls4mlEmulator::ModelLoader loader
HLT enums.
std::vector< std::vector< std::vector< uint32_t > > > pumLUT
A calorimeter trigger region (sum of 4x4 trigger towers)
Definition: L1CaloRegion.h:21
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::vector< L1CaloRegion > L1CaloRegionCollection
double caloScaleFactor
void beginRun(edm::Run const &, edm::EventSetup const &) override
def move(src, dest)
Definition: eostools.py:511
Definition: Run.h:45
def exit(msg="")