CMS 3D CMS Logo

L1TCaloParamsOnlineProd.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <stdexcept>
4 #include <sstream>
5 
13 //#include "L1Trigger/L1TCalorimeter/interface/CaloParamsHelper.h"
14 #include "CaloParamsHelperO2O.h"
15 #include "OnlineDBqueryHelper.h"
16 
17 #include "xercesc/util/PlatformUtils.hpp"
18 using namespace XERCES_CPP_NAMESPACE;
19 
20 class L1TCaloParamsOnlineProd : public L1ConfigOnlineProdBaseExt<L1TCaloParamsO2ORcd,l1t::CaloParams> {
21 private:
22  unsigned int exclusiveLayer; // 0 - process calol1 and calol2, 1 - only calol1, 2 - only calol2
24 
25  bool readCaloLayer1OnlineSettings(l1t::CaloParamsHelperO2O& paramsHelper, std::map<std::string, l1t::Parameter>& conf, std::map<std::string,
26 l1t::Mask>& );
27  bool readCaloLayer2OnlineSettings(l1t::CaloParamsHelperO2O& paramsHelper, std::map<std::string, l1t::Parameter>& conf, std::map<std::string,
28 l1t::Mask>& );
29 public:
30  std::shared_ptr<l1t::CaloParams> newObject(const std::string& objectKey, const L1TCaloParamsO2ORcd& record) override ;
31 
33  ~L1TCaloParamsOnlineProd(void) override{}
34 };
35 
36 bool
37 L1TCaloParamsOnlineProd::readCaloLayer1OnlineSettings(l1t::CaloParamsHelperO2O& paramsHelper, std::map<std::string, l1t::Parameter>& conf,
38 std::map<std::string, l1t::Mask>& ) {
39  const char * expectedParams[] = {
40  "layer1ECalScaleFactors",
41  "layer1HCalScaleFactors",
42  "layer1HFScaleFactors",
43  "layer1ECalScaleETBins",
44  "layer1HCalScaleETBins",
45  "layer1HFScaleETBins"
46  // Optional params
47  //"layer1ECalScalePhiBins",
48  //"layer1HCalScalePhiBins",
49  //"layer1HFScalePhiBins",
50  //"layer1SecondStageLUT"
51  };
52  for (const auto param : expectedParams) {
53  if ( conf.find(param) == conf.end() ) {
54  edm::LogError("L1-O2O: L1TCaloParamsOnlineProd") << "Unable to locate expected CaloLayer1 parameter: " << param << " in L1 settings payload!";
55  return false;
56  }
57  }
58  // Layer 1 LUT specification
59  paramsHelper.setLayer1ECalScaleFactors((conf["layer1ECalScaleFactors"].getVector<double>()));
60  paramsHelper.setLayer1HCalScaleFactors((conf["layer1HCalScaleFactors"].getVector<double>()));
61  paramsHelper.setLayer1HFScaleFactors ((conf["layer1HFScaleFactors"] .getVector<double>()));
62  paramsHelper.setLayer1ECalScaleETBins(conf["layer1ECalScaleETBins"].getVector<int>());
63  paramsHelper.setLayer1HCalScaleETBins(conf["layer1HCalScaleETBins"].getVector<int>());
64  paramsHelper.setLayer1HFScaleETBins (conf["layer1HFScaleETBins"] .getVector<int>());
65 
66  if( conf.find("layer1ECalScalePhiBins") != conf.end() )
67  paramsHelper.setLayer1ECalScalePhiBins(conf["layer1ECalScalePhiBins"].getVector<unsigned int>()); // std::vector<unsigned>(36,0)
68  if( conf.find("layer1HCalScalePhiBins") != conf.end() )
69  paramsHelper.setLayer1HCalScalePhiBins(conf["layer1HCalScalePhiBins"].getVector<unsigned int>());
70  if( conf.find("layer1HFScalePhiBins") != conf.end() )
71  paramsHelper.setLayer1HFScalePhiBins (conf["layer1HFScalePhiBins"] .getVector<unsigned int>());
72  if( conf.find("layer1SecondStageLUT") != conf.end() )
73  paramsHelper.setLayer1SecondStageLUT(conf["layer1SecondStageLUT"].getVector<unsigned int>() );
74 
75  return true;
76 }
77 
78 bool
79 L1TCaloParamsOnlineProd::readCaloLayer2OnlineSettings(l1t::CaloParamsHelperO2O& paramsHelper, std::map<std::string, l1t::Parameter>& conf,
80 std::map<std::string, l1t::Mask>& ) {
81  const char * expectedParams[] = {
82  "leptonSeedThreshold",
83  "leptonTowerThreshold",
84  "pileUpTowerThreshold",
85  "jetSeedThreshold",
86  "jetMaxEta",
87  "HTMHT_maxJetEta",
88  "HT_jetThreshold",
89  "MHT_jetThreshold",
90  "jetEnergyCalibLUT",
91  "ETMET_maxTowerEta",
92  "ET_energyCalibLUT",
93  "ecalET_energyCalibLUT",
94  "METX_energyCalibLUT",
95  "METY_energyCalibLUT",
96  "egammaRelaxationThreshold",
97  "egammaMaxEta",
98  "egammaEnergyCalibLUT",
99  "egammaIsoLUT1",
100  "egammaIsoLUT2",
101  "tauMaxEta",
102  "tauEnergyCalibLUT",
103  "tauIsoLUT1",
105  "towerCountThreshold",
106  "towerCountMaxEta",
107  "ET_towerThresholdLUT",
108  "MET_towerThresholdLUT",
109  "ecalET_towerThresholdLUT",
110  "jetBypassPileUpSub",
111  "egammaBypassCuts",
112  "egammaHOverECut_iEtaLT15",
113  "egammaHOverECut_iEtaGTEq15",
114  "egammaBypassExtendedHOverE"
115  };
116  for (const auto param : expectedParams) {
117  if ( conf.find(param) == conf.end() ) {
118  edm::LogError("L1-O2O: L1TCaloParamsOnlineProd") << "Unable to locate expected CaloLayer2 parameter: " << param << " in L1 settings payload!";
119  return false;
120  }
121  }
122  // Layer 2 params specification
123  paramsHelper.setEgSeedThreshold((conf["leptonSeedThreshold"].getValue<int>())/2);
124  paramsHelper.setTauSeedThreshold((conf["leptonSeedThreshold"].getValue<int>())/2);
125  paramsHelper.setEgNeighbourThreshold((conf["leptonTowerThreshold"].getValue<int>())/2);
126  paramsHelper.setTauNeighbourThreshold((conf["leptonTowerThreshold"].getValue<int>())/2);
127  paramsHelper.setJetSeedThreshold((conf["jetSeedThreshold"].getValue<int>())/2);
128  paramsHelper.setJetBypassPUS(conf["jetBypassPileUpSub"].getValue<bool>());
129  paramsHelper.setEgBypassEGVetos(conf["egammaBypassCuts"].getValue<bool>());
130  paramsHelper.setEgHOverEcutBarrel(conf["egammaHOverECut_iEtaLT15"].getValue<int>());
131  paramsHelper.setEgHOverEcutEndcap(conf["egammaHOverECut_iEtaGTEq15"].getValue<int>());
132 
133 
134  // Currently not used // paramsHelper.setEgPileupTowerThresh((conf["pileUpTowerThreshold"].getValue<int>()));
135  // Currently not used // paramsHelper.setTauPileupTowerThresh((conf["pileUpTowerThreshold"].getValue<int>()));
136  // Currently not used // paramsHelper.setJetMaxEta((conf["jetMaxEta"].getValue<int>()));
137 
138  std::vector<int> etSumEtaMax;
139  std::vector<int> etSumEtThresh;
140 
141  etSumEtaMax.push_back(conf["ETMET_maxTowerEta"].getValue<int>());
142  etSumEtaMax.push_back(conf["HTMHT_maxJetEta"].getValue<int>());
143  etSumEtaMax.push_back(conf["ETMET_maxTowerEta"].getValue<int>());
144  etSumEtaMax.push_back(conf["HTMHT_maxJetEta"].getValue<int>());
145  etSumEtaMax.push_back(conf["towerCountMaxEta"].getValue<int>());
146 
147  etSumEtThresh.push_back(0); //conf["ET_towerThreshold"].getValue<int>()/2); // ETT tower threshold
148  etSumEtThresh.push_back(conf["HT_jetThreshold"].getValue<int>()/2);
149  etSumEtThresh.push_back(0); //conf["MET_towerThreshold"].getValue<int>()/2); // ETM tower threshold
150  etSumEtThresh.push_back(conf["MHT_jetThreshold"].getValue<int>()/2);
151  etSumEtThresh.push_back(conf["ET_towerThreshold"].getValue<int>()/2);
152 
153  for (uint i=0; i<5; ++i) {
154  paramsHelper.setEtSumEtaMax(i, etSumEtaMax.at(i));
155  paramsHelper.setEtSumEtThreshold(i, etSumEtThresh.at(i));
156  }
157 
158  paramsHelper.setJetCalibrationLUT ( l1t::convertToLUT( conf["jetEnergyCalibLUT"].getVector<uint32_t>() ) );
159 
160  paramsHelper.setEtSumMetPUSLUT ( l1t::convertToLUT( conf["MET_towerThresholdLUT"].getVector<int>() ) );
161  paramsHelper.setEtSumEttPUSLUT ( l1t::convertToLUT( conf["ET_towerThresholdLUT"].getVector<int>() ) );
162  paramsHelper.setEtSumEcalSumPUSLUT( l1t::convertToLUT( conf["ecalET_towerThresholdLUT"].getVector<int>() ) );
163 
164  paramsHelper.setEtSumEttCalibrationLUT ( l1t::convertToLUT( conf["ET_energyCalibLUT"].getVector<int>() ) );
165  paramsHelper.setEtSumEcalSumCalibrationLUT( l1t::convertToLUT( conf["ecalET_energyCalibLUT"].getVector<int>() ) );
166  paramsHelper.setEtSumXCalibrationLUT ( l1t::convertToLUT( conf["METX_energyCalibLUT"].getVector<int>() ) );
167 
168  paramsHelper.setEgMaxPtHOverE((conf["egammaRelaxationThreshold"].getValue<int>())/2.);
169  paramsHelper.setEgEtaCut((conf["egammaMaxEta"].getValue<int>()));
170  paramsHelper.setEgCalibrationLUT ( l1t::convertToLUT( conf["egammaEnergyCalibLUT"].getVector<int>() ) );
171  paramsHelper.setEgIsolationLUT ( l1t::convertToLUT( conf["egammaIsoLUT1"].getVector<int>() ) );
172  paramsHelper.setEgIsolationLUT2 ( l1t::convertToLUT( conf["egammaIsoLUT2"].getVector<int>() ) );
173 
174  paramsHelper.setIsoTauEtaMax((conf["tauMaxEta"].getValue<int>()));
175 
176  paramsHelper.setTauCalibrationLUT( l1t::convertToLUT( conf["tauEnergyCalibLUT"].getVector<int>() ) );
177  paramsHelper.setTauIsolationLUT ( l1t::convertToLUT( conf["tauIsoLUT1"].getVector<int>() ) );
178  if( conf.find("tauIsoLUT2") != conf.end() )
179  paramsHelper.setTauIsolationLUT2 ( l1t::convertToLUT( conf["tauIsoLUT2"].getVector<int>() ) );
180 
181  paramsHelper.setEgBypassExtHOverE( conf["egammaBypassExtendedHOverE"].getValue<bool>() );
182 
183  if( conf.find("P_TauTrimming_13to8.mif") != conf.end() )
184  paramsHelper.setTauTrimmingShapeVetoLUT( l1t::convertToLUT( conf["P_TauTrimming_13to8.mif"].getVector<int>() ) );
185 
186  return true;
187 }
188 
191 {
192  exclusiveLayer = iConfig.getParameter<uint32_t>("exclusiveLayer");
193  transactionSafe = iConfig.getParameter<bool>("transactionSafe");
194 }
195 
196 std::shared_ptr<l1t::CaloParams> L1TCaloParamsOnlineProd::newObject(const std::string& objectKey, const L1TCaloParamsO2ORcd& record) {
197  using namespace edm::es;
198 
199  const L1TCaloParamsRcd& baseRcd = record.template getRecord< L1TCaloParamsRcd >() ;
200  edm::ESHandle< l1t::CaloParams > baseSettings ;
201  baseRcd.get( baseSettings ) ;
202 
203 
204  if( objectKey.empty() ){
205  edm::LogError( "L1-O2O: L1TCaloParamsOnlineProd" ) << "Key is empty";
206  if( transactionSafe )
207  throw std::runtime_error("SummaryForFunctionManager: Calo | Faulty | Empty objectKey");
208  else {
209  edm::LogError( "L1-O2O: L1TCaloParamsOnlineProd" ) << "returning unmodified prototype of l1t::CaloParams";
210  return std::make_shared< l1t::CaloParams >( *(baseSettings.product()) ) ;
211  }
212  }
213 
214  std::string tscKey = objectKey.substr(0, objectKey.find(":") );
215  std::string rsKey = objectKey.substr( objectKey.find(":")+1, std::string::npos );
216 
217  edm::LogInfo( "L1-O2O: L1TCaloParamsOnlineProd" ) << "Producing L1TCaloParamsOnlineProd with TSC key = " << tscKey << " and RS key = " << rsKey;
218 
219  std::string calol1_top_key, calol1_algo_key;
220  std::string calol1_algo_payload;
221  std::string calol2_top_key, calol2_algo_key, calol2_hw_key;
222  std::string calol2_hw_payload;
223  std::map<std::string,std::string> calol2_algo_payloads; // key -> XML payload
224  try {
225 
226  std::map<std::string,std::string> topKeys =
227  l1t::OnlineDBqueryHelper::fetch( {"CALOL1_KEY","CALOL2_KEY"},
228  "L1_TRG_CONF_KEYS",
229  tscKey,
231  );
232 
233  if( exclusiveLayer == 0 || exclusiveLayer == 1 ){
234 
235  calol1_top_key = topKeys["CALOL1_KEY"];
236 
237  calol1_algo_key = l1t::OnlineDBqueryHelper::fetch( {"ALGO"},
238  "CALOL1_KEYS",
239  calol1_top_key,
241  ) ["ALGO"];
242 
243  calol1_algo_payload = l1t::OnlineDBqueryHelper::fetch( {"CONF"},
244  "CALOL1_CLOBS",
245  calol1_algo_key,
247  ) ["CONF"];
248  }
249 
250  if( exclusiveLayer == 0 || exclusiveLayer == 2 ){
251 
252  calol2_top_key = topKeys["CALOL2_KEY"];
253 
254  std::map<std::string,std::string> calol2_keys =
255  l1t::OnlineDBqueryHelper::fetch( {"ALGO","HW"},
256  "CALOL2_KEYS",
257  calol2_top_key,
259  );
260 
261  calol2_hw_key = calol2_keys["HW"];
262  calol2_hw_payload = l1t::OnlineDBqueryHelper::fetch( {"CONF"},
263  "CALOL2_CLOBS",
264  calol2_hw_key,
266  ) ["CONF"];
267 
268  calol2_algo_key = calol2_keys["ALGO"];
269 
270  std::map<std::string,std::string> calol2_algo_keys =
271  l1t::OnlineDBqueryHelper::fetch( {"DEMUX","MPS_COMMON","MPS_JET","MP_EGAMMA","MP_SUM","MP_TAU"},
272  "CALOL2_ALGO_KEYS",
273  calol2_algo_key,
275  );
276 
277  for(auto &key : calol2_algo_keys)
278  calol2_algo_payloads[ key.second ] =
280  "CALOL2_CLOBS",
281  key.second,
283  ) ["CONF"];
284  }
285 
286  } catch ( std::runtime_error &e ) {
287  edm::LogError( "L1-O2O: L1TCaloParamsOnlineProd" ) << e.what();
288  if( transactionSafe )
289  throw std::runtime_error(std::string("SummaryForFunctionManager: Calo | Faulty | ") + e.what());
290  else {
291  edm::LogError( "L1-O2O: L1TCaloParamsOnlineProd" ) << "returning unmodified prototype of l1t::CaloParams";
292  return std::make_shared< l1t::CaloParams >( *(baseSettings.product()) ) ;
293  }
294  }
295 
296  if( exclusiveLayer == 0 || exclusiveLayer == 2 ){
297  // for debugging purposes dump the configs to local files
298  for(auto &conf : calol2_algo_payloads){
299  std::ofstream output(std::string("/tmp/").append(conf.first.substr(0,conf.first.find("/"))).append(".xml"));
300  output<<conf.second;
301  output.close();
302  }
303  std::ofstream output(std::string("/tmp/").append(calol2_hw_key.substr(0,calol2_hw_key.find("/"))).append(".xml"));
304  output << calol2_hw_payload;
305  output.close();
306  }
307  if( exclusiveLayer == 0 || exclusiveLayer == 1 )
308  {
309  std::ofstream output(std::string("/tmp/").append(calol1_algo_key.substr(0,calol1_algo_key.find("/"))).append(".xml"));
310  output << calol1_algo_payload;
311  output.close();
312  }
313 
314  l1t::CaloParamsHelperO2O m_params_helper( *(baseSettings.product()) );
315 
316 
317  if( exclusiveLayer == 0 || exclusiveLayer == 1 ){
318  try {
319  l1t::XmlConfigParser xmlReader1;
320  xmlReader1.readDOMFromString( calol1_algo_payload );
321 
322  l1t::TriggerSystem calol1;
323  calol1.addProcessor("processors", "processors","-1","-1");
324  xmlReader1.readRootElement( calol1, "calol1" );
325  calol1.setConfigured();
326 
327  std::map<std::string, l1t::Parameter> calol1_conf = calol1.getParameters("processors");
328  std::map<std::string, l1t::Mask> calol1_rs ;//= calol1.getMasks ("processors");
329 
330  if( !readCaloLayer1OnlineSettings(m_params_helper, calol1_conf, calol1_rs) )
331  throw std::runtime_error("Parsing error for CaloLayer1");
332 
333  } catch ( std::runtime_error &e ){
334  edm::LogError( "L1-O2O: L1TCaloParamsOnlineProd" ) << e.what();
335  if( transactionSafe )
336  throw std::runtime_error(std::string("SummaryForFunctionManager: Calo | Faulty | ") + e.what());
337  else {
338  edm::LogError( "L1-O2O: L1TCaloParamsOnlineProd" ) << "returning unmodified prototype of l1t::CaloParams";
339  return std::make_shared< l1t::CaloParams >( *(baseSettings.product()) ) ;
340  }
341  }
342  }
343 
344 
345  if( exclusiveLayer == 0 || exclusiveLayer == 2 ){
346  try {
347  l1t::TriggerSystem calol2;
348  l1t::XmlConfigParser xmlReader2;
349  xmlReader2.readDOMFromString( calol2_hw_payload );
350  xmlReader2.readRootElement( calol2, "calol2" );
351 
352  for(auto &conf : calol2_algo_payloads){
353  xmlReader2.readDOMFromString( conf.second );
354  xmlReader2.readRootElement( calol2, "calol2" );
355  }
356 
357 // calol2.setSystemId("calol2");
358  calol2.setConfigured();
359 
360  std::map<std::string, l1t::Parameter> calol2_conf = calol2.getParameters("MP1");
361  std::map<std::string, l1t::Mask> calol2_rs ;//= calol2.getMasks ("processors");
362 
363  if( !readCaloLayer2OnlineSettings(m_params_helper, calol2_conf, calol2_rs) )
364  throw std::runtime_error("Parsing error for CaloLayer2");
365 
366  } catch ( std::runtime_error &e ){
367  edm::LogError( "L1-O2O: L1TCaloParamsOnlineProd" ) << e.what();
368  if( transactionSafe )
369  throw std::runtime_error(std::string("SummaryForFunctionManager: Calo | Faulty | ") + e.what());
370  else {
371  edm::LogError( "L1-O2O: L1TCaloParamsOnlineProd" ) << "returning unmodified prototype of l1t::CaloParams";
372  return std::make_shared< l1t::CaloParams >( *(baseSettings.product()) ) ;
373  }
374  }
375  }
376 
377  std::shared_ptr< l1t::CaloParams > retval = std::make_shared< l1t::CaloParams >( m_params_helper ) ;
378 
379  edm::LogInfo( "L1-O2O: L1TCaloParamsOnlineProd" ) << "SummaryForFunctionManager: Calo | OK | All looks good";
380  return retval;
381 }
382 
383 //define this as a plug-in
385 
386 
T getParameter(std::string const &) const
void setConfigured(bool state=true) noexcept
Definition: TriggerSystem.h:73
void setEtSumXCalibrationLUT(const l1t::LUT &lut)
void setTauIsolationLUT(const l1t::LUT &lut)
void setLayer1HFScalePhiBins(const std::vector< unsigned > params)
JetCorrectorParameters::Record record
Definition: classes.h:7
void setEgCalibrationLUT(const l1t::LUT &lut)
void setEtSumEttCalibrationLUT(const l1t::LUT &lut)
void setJetCalibrationLUT(const l1t::LUT &lut)
std::shared_ptr< l1t::CaloParams > newObject(const std::string &objectKey, const L1TCaloParamsO2ORcd &record) override
void setEgNeighbourThreshold(double thresh)
void setLayer1HCalScaleFactors(const std::vector< double > params)
void setLayer1ECalScaleFactors(const std::vector< double > params)
void setLayer1SecondStageLUT(const std::vector< unsigned > &lut)
void setEtSumEcalSumCalibrationLUT(const l1t::LUT &lut)
delete x;
Definition: CaloConfig.h:22
L1TCaloParamsOnlineProd(const edm::ParameterSet &)
void setEtSumEttPUSLUT(const l1t::LUT &lut)
void setTauCalibrationLUT(const l1t::LUT &lut)
void setLayer1HCalScalePhiBins(const std::vector< unsigned > params)
void setJetSeedThreshold(double thresh)
void setLayer1HCalScaleETBins(const std::vector< int > params)
void setEtSumEtaMax(unsigned isum, int eta)
void readDOMFromString(const std::string &str, xercesc::DOMDocument *&doc)
void setEgIsolationLUT(const l1t::LUT &lut)
void setLayer1HFScaleETBins(const std::vector< int > params)
void setTauSeedThreshold(double thresh)
static std::map< std::string, std::string > fetch(const std::vector< std::string > &queryColumns, const std::string &table, const std::string &key, l1t::OMDSReader &m_omdsReader)
void setTauNeighbourThreshold(double thresh)
void setLayer1ECalScaleETBins(const std::vector< int > params)
void get(HolderT &iHolder) const
bool readCaloLayer2OnlineSettings(l1t::CaloParamsHelperO2O &paramsHelper, std::map< std::string, l1t::Parameter > &conf, std::map< std::string, l1t::Mask > &)
void setEgIsolationLUT2(const l1t::LUT &lut)
l1t::LUT convertToLUT(const std::vector< uint64_t > &v, int padding=-1) noexcept
Definition: ConvertToLUT.h:10
void setLayer1ECalScalePhiBins(const std::vector< unsigned > params)
void setTauTrimmingShapeVetoLUT(const l1t::LUT &lut)
def uint(string)
#define DEFINE_FWK_EVENTSETUP_MODULE(type)
Definition: ModuleFactory.h:60
const std::map< std::string, Parameter > & getParameters(const char *processor) const
void setEgBypassEGVetos(unsigned flag)
void setEtSumEcalSumPUSLUT(const l1t::LUT &lut)
void setEgSeedThreshold(double thresh)
void setEtSumEtThreshold(unsigned isum, double thresh)
void readRootElement(TriggerSystem &aTriggerSystem, const std::string &sysId="")
bool readCaloLayer1OnlineSettings(l1t::CaloParamsHelperO2O &paramsHelper, std::map< std::string, l1t::Parameter > &conf, std::map< std::string, l1t::Mask > &)
void setEgBypassExtHOverE(unsigned flag)
void addProcessor(const char *processor, const char *role, const char *crate, const char *slot)
void setEgMaxPtHOverE(double thresh)
void setEtSumMetPUSLUT(const l1t::LUT &lut)
void setTauIsolationLUT2(const l1t::LUT &lut)
void setJetBypassPUS(unsigned flag)
void setLayer1HFScaleFactors(const std::vector< double > params)
Definition: Mask.h:10