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 #include <string>
61 #include <sstream>
62 
63 //Anomaly detection includes
64 #include "ap_fixed.h"
65 #include "hls4ml/emulator.h"
66 
67 using namespace l1tcalo;
68 using namespace l1extra;
69 using namespace std;
70 
71 //
72 // class declaration
73 //
74 
75 template <class INPUT, class OUTPUT>
77 public:
78  explicit L1TCaloSummary(const edm::ParameterSet&);
79  ~L1TCaloSummary() override = default;
80 
81  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
82 
83 private:
84  //void beginJob() override;
85  void produce(edm::Event&, const edm::EventSetup&) override;
86  //void endJob() override;
87 
88  void print();
89 
90  // ----------member data ---------------------------
91 
92  uint32_t nPumBins;
93 
94  std::vector<std::vector<std::vector<uint32_t>>> pumLUT;
95 
97 
98  uint32_t jetSeed;
99  uint32_t tauSeed;
101  uint32_t eGammaSeed;
104 
105  bool verbose;
107 
109 
110  UCTLayer1* layer1;
111 
112  hls4mlEmulator::ModelLoader loader;
113  std::shared_ptr<hls4mlEmulator::Model> model;
114 
116  std::vector<edm::ParameterSet> testPatterns;
117 };
118 
119 //
120 // constants, enums and typedefs
121 //
122 
123 //
124 // static data member definitions
125 //
126 
127 //
128 // constructors and destructor
129 //
130 template <class INPUT, class OUTPUT>
132  : nPumBins(iConfig.getParameter<unsigned int>("nPumBins")),
133  pumLUT(nPumBins, std::vector<std::vector<uint32_t>>(2, std::vector<uint32_t>(13))),
134  caloScaleFactor(iConfig.getParameter<double>("caloScaleFactor")),
135  jetSeed(iConfig.getParameter<unsigned int>("jetSeed")),
136  tauSeed(iConfig.getParameter<unsigned int>("tauSeed")),
137  tauIsolationFactor(iConfig.getParameter<double>("tauIsolationFactor")),
138  eGammaSeed(iConfig.getParameter<unsigned int>("eGammaSeed")),
139  eGammaIsolationFactor(iConfig.getParameter<double>("eGammaIsolationFactor")),
140  boostedJetPtFactor(iConfig.getParameter<double>("boostedJetPtFactor")),
141  verbose(iConfig.getParameter<bool>("verbose")),
142  fwVersion(iConfig.getParameter<int>("firmwareVersion")),
143  regionToken(consumes<L1CaloRegionCollection>(edm::InputTag("simCaloStage2Layer1Digis"))),
144  loader(hls4mlEmulator::ModelLoader(iConfig.getParameter<string>("CICADAModelVersion"))),
145  overwriteWithTestPatterns(iConfig.getParameter<bool>("useTestPatterns")),
146  testPatterns(iConfig.getParameter<std::vector<edm::ParameterSet>>("testPatterns")) {
147  std::vector<double> pumLUTData;
148  char pumLUTString[10];
149  for (uint32_t pumBin = 0; pumBin < nPumBins; pumBin++) {
150  for (uint32_t side = 0; side < 2; side++) {
151  if (side == 0)
152  sprintf(pumLUTString, "pumLUT%2.2dp", pumBin);
153  else
154  sprintf(pumLUTString, "pumLUT%2.2dn", pumBin);
155  pumLUTData = iConfig.getParameter<std::vector<double>>(pumLUTString);
156  for (uint32_t iEta = 0; iEta < std::max((uint32_t)pumLUTData.size(), MaxUCTRegionsEta); iEta++) {
157  pumLUT[pumBin][side][iEta] = (uint32_t)round(pumLUTData[iEta] / caloScaleFactor);
158  }
159  if (pumLUTData.size() != (MaxUCTRegionsEta))
160  edm::LogError("L1TCaloSummary") << "PUM LUT Data size integrity check failed; Expected size = "
161  << MaxUCTRegionsEta << "; Provided size = " << pumLUTData.size()
162  << "; Will use what is provided :(" << std::endl;
163  }
164  }
165  produces<L1JetParticleCollection>("Boosted");
166 
167  //anomaly trigger loading
168  model = loader.load_model();
169  produces<float>("CICADAScore");
170 }
171 
172 //
173 // member functions
174 //
175 
176 // ------------ method called to produce the data ------------
177 template <class INPUT, class OUTPUT>
179  using namespace edm;
180 
181  std::unique_ptr<L1JetParticleCollection> bJetCands(new L1JetParticleCollection);
182 
183  std::unique_ptr<float> CICADAScore = std::make_unique<float>();
184 
185  UCTGeometry g;
186 
187  // Here we read region data from the region collection created by L1TCaloLayer1 instead of
188  // independently creating regions from TPGs for processing by the summary card. This results
189  // in a single region vector of size 252 whereas from independent creation we had 3*6 vectors
190  // of size 7*2. Indices are mapped in UCTSummaryCard accordingly.
191  UCTSummaryCard summaryCard =
193  std::vector<UCTRegion*> inputRegions;
194  inputRegions.clear();
195  edm::Handle<std::vector<L1CaloRegion>> regionCollection;
196  if (!iEvent.getByToken(regionToken, regionCollection))
197  edm::LogError("L1TCaloSummary") << "UCT: Failed to get regions from region collection!";
198  iEvent.getByToken(regionToken, regionCollection);
199  //Model input
200  //This is done as a flat vector input, but future versions may involve 2D input
201  //This will have to be handled later
202  INPUT modelInput[252];
203  for (const L1CaloRegion& i : *regionCollection) {
204  UCTRegionIndex r = g.getUCTRegionIndexFromL1CaloRegion(i.gctEta(), i.gctPhi());
205  UCTTowerIndex t = g.getUCTTowerIndexFromL1CaloRegion(r, i.raw());
206  uint32_t absCaloEta = std::abs(t.first);
207  uint32_t absCaloPhi = std::abs(t.second);
208  bool negativeEta = false;
209  if (t.first < 0)
210  negativeEta = true;
211  uint32_t crate = g.getCrate(t.first, t.second);
212  uint32_t card = g.getCard(t.first, t.second);
213  uint32_t region = g.getRegion(absCaloEta, absCaloPhi);
214  UCTRegion* test = new UCTRegion(crate, card, negativeEta, region, fwVersion);
215  test->setRegionSummary(i.raw());
216  inputRegions.push_back(test);
217  //This *should* fill the tensor in the proper order to be fed to the anomaly model
218  //We take 4 off of the GCT eta/iEta.
219  //iEta taken from this ranges from 4-17, (I assume reserving lower and higher for forward regions)
220  //So our first index, index 0, is technically iEta=4, and so-on.
221  //CICADA reads this as a flat vector
222  modelInput[14 * i.gctPhi() + (i.gctEta() - 4)] = i.et();
223  }
224  // Check if we're using test patterns. If so, we overwrite the inputs with a test pattern
225  if (overwriteWithTestPatterns) {
226  unsigned int evt = iEvent.id().event();
227  unsigned int totalTestPatterns = testPatterns.size();
228  unsigned int patternElement = evt % totalTestPatterns;
229  const edm::ParameterSet& element = testPatterns.at(patternElement);
230  std::stringstream inputStream;
231  std::string PhiRowString;
232 
233  edm::LogWarning("L1TCaloSummary") << "Overwriting existing CICADA input with test pattern!\n";
234 
235  for (unsigned short int iPhi = 1; iPhi <= 18; ++iPhi) {
236  PhiRowString = "";
237  std::stringstream PhiRowStringStream;
238  PhiRowStringStream << "iPhi_" << iPhi;
239  PhiRowString = PhiRowStringStream.str();
240  std::vector<unsigned int> phiRow = element.getParameter<std::vector<unsigned int>>(PhiRowString);
241  for (unsigned short int iEta = 1; iEta <= 14; ++iEta) {
242  modelInput[14 * (iPhi - 1) + (iEta - 1)] = phiRow.at(iEta - 1);
243  inputStream << phiRow.at(iEta - 1) << " ";
244  }
245  inputStream << "\n";
246  }
247  edm::LogInfo("L1TCaloSummary") << "Input Stream:\n" << inputStream.str();
248  }
249 
250  //Extract model output
251  OUTPUT modelResult[1] = {
252  OUTPUT("0.0", 10)}; //the 10 here refers to the fact that we read in "0.0" as a decimal number
253  model->prepare_input(modelInput);
254  model->predict();
255  model->read_result(modelResult);
256 
257  *CICADAScore = modelResult[0].to_float();
258 
259  if (overwriteWithTestPatterns)
260  edm::LogInfo("L1TCaloSummary") << "Test Pattern Output: " << *CICADAScore;
261 
262  summaryCard.setRegionData(inputRegions);
263 
264  if (!summaryCard.process()) {
265  edm::LogError("L1TCaloSummary") << "UCT: Failed to process summary card" << std::endl;
266  exit(1);
267  }
268 
269  double pt = 0;
270  double eta = -999.;
271  double phi = -999.;
272  double mass = 0;
273 
274  std::list<UCTObject*> boostedJetObjs = summaryCard.getBoostedJetObjs();
275  for (std::list<UCTObject*>::const_iterator i = boostedJetObjs.begin(); i != boostedJetObjs.end(); i++) {
276  const UCTObject* object = *i;
277  pt = ((double)object->et()) * caloScaleFactor * boostedJetPtFactor;
278  eta = g.getUCTTowerEta(object->iEta());
279  phi = g.getUCTTowerPhi(object->iPhi());
280  bitset<3> activeRegionEtaPattern = 0;
281  for (uint32_t iEta = 0; iEta < 3; iEta++) {
282  bool activeStrip = false;
283  for (uint32_t iPhi = 0; iPhi < 3; iPhi++) {
284  if (object->boostedJetRegionET()[3 * iEta + iPhi] > 30 &&
285  object->boostedJetRegionET()[3 * iEta + iPhi] > object->et() * 0.0625)
286  activeStrip = true;
287  }
288  if (activeStrip)
289  activeRegionEtaPattern |= (0x1 << iEta);
290  }
291  bitset<3> activeRegionPhiPattern = 0;
292  for (uint32_t iPhi = 0; iPhi < 3; iPhi++) {
293  bool activeStrip = false;
294  for (uint32_t iEta = 0; iEta < 3; iEta++) {
295  if (object->boostedJetRegionET()[3 * iEta + iPhi] > 30 &&
296  object->boostedJetRegionET()[3 * iEta + iPhi] > object->et() * 0.0625)
297  activeStrip = true;
298  }
299  if (activeStrip)
300  activeRegionPhiPattern |= (0x1 << iPhi);
301  }
302  string regionEta = activeRegionEtaPattern.to_string<char, std::string::traits_type, std::string::allocator_type>();
303  string regionPhi = activeRegionPhiPattern.to_string<char, std::string::traits_type, std::string::allocator_type>();
304 
305  bool centralHighest = object->boostedJetRegionET()[4] >= object->boostedJetRegionET()[0] &&
306  object->boostedJetRegionET()[4] >= object->boostedJetRegionET()[1] &&
307  object->boostedJetRegionET()[4] >= object->boostedJetRegionET()[2] &&
308  object->boostedJetRegionET()[4] >= object->boostedJetRegionET()[3] &&
309  object->boostedJetRegionET()[4] >= object->boostedJetRegionET()[5] &&
310  object->boostedJetRegionET()[4] >= object->boostedJetRegionET()[6] &&
311  object->boostedJetRegionET()[4] >= object->boostedJetRegionET()[7] &&
312  object->boostedJetRegionET()[4] >= object->boostedJetRegionET()[8];
313 
314  if (abs(eta) < 2.5 && ((regionEta == "101" && (regionPhi == "110" || regionPhi == "101" || regionPhi == "010")) ||
315  ((regionEta == "110" || regionEta == "101" || regionEta == "010") && regionPhi == "101") ||
316  (regionEta == "111" && (regionPhi == "110" || regionPhi == "010")) ||
317  ((regionEta == "110" || regionEta == "010") && regionPhi == "111") ||
318  ((regionEta == "010" || regionPhi == "010" || regionEta == "110" || regionPhi == "110" ||
319  regionEta == "011" || regionPhi == "011") &&
320  centralHighest)))
321  bJetCands->push_back(L1JetParticle(math::PtEtaPhiMLorentzVector(pt, eta, phi, mass), L1JetParticle::kCentral));
322  }
323 
324  iEvent.put(std::move(bJetCands), "Boosted");
325  //Write out anomaly score
326  iEvent.put(std::move(CICADAScore), "CICADAScore");
327 }
328 
329 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
330 template <class INPUT, class OUTPUT>
332  //The following says we do not know what parameters are allowed so do no validation
333  // Please change this to state exactly what you do use, even if it is no parameters
335  desc.setUnknown();
336  descriptions.addDefault(desc);
337 }
338 
341 //define type version plugins
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
L1TCaloSummary< ap_ufixed< 10, 10 >, ap_fixed< 11, 5 > > L1TCaloSummaryCICADAv1
bool verbose
bool overwriteWithTestPatterns
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
std::vector< edm::ParameterSet > testPatterns
edm::EDGetTokenT< L1CaloRegionCollection > regionToken
Log< level::Info, false > LogInfo
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
Log< level::Warning, false > LogWarning
def move(src, dest)
Definition: eostools.py:511
def exit(msg="")