ATLAS Offline Software
CaloInfoLoader.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 //
7 //NAME: CaloInfoLoader.cpp
8 //PACKAGE: TrigConfStorage
9 //
11 
12 #include "./CaloInfoLoader.h"
13 
14 #include <CoralBase/Attribute.h>
15 #include <CoralBase/AttributeList.h>
16 
17 #include "RelationalAccess/SchemaException.h"
18 #include "RelationalAccess/ITransaction.h"
19 #include "RelationalAccess/ITable.h"
20 #include "RelationalAccess/ISchema.h"
21 #include "RelationalAccess/ICursor.h"
22 #include "RelationalAccess/IQuery.h"
23 
25 
26 #include "./DBHelper.h"
27 
28 #include <iostream>
29 #include <stdexcept>
30 #include <sstream>
31 #include <typeinfo>
32 #include <iterator>
33 #include <map>
34 
35 #include "boost/lexical_cast.hpp"
36 
37 using namespace std;
38 
39 namespace {
40 
41  string join(const vector<int> & v) {
42  string joined("");
43  bool first=true;
44  for(int id : v) {
45  if(first) { first = false; } else { joined += ","; }
46  joined += boost::lexical_cast<string,int>(id);
47  }
48  return joined;
49  }
50 
51 }
52 
53 
54 bool
56 
57  if(data.smk()!=0) {
58  TRG_MSG_INFO("Loading CaloInfo for SMK " << data.smk());
59  } else if( data.id()!=0 ) {
60  TRG_MSG_INFO("Loading CaloInfo with ID = " << data.id());
61  } else {
62  TRG_MSG_ERROR("Can not load CaloInfo which has no id or SMK specified");
63  throw runtime_error("Can not load CaloInfo, no SMK or Id specified");
64  }
65 
66 
67 
68  try {
69  unsigned int schema = triggerDBSchemaVersion();
70 
71  startSession();
72 
73  unique_ptr<coral::IQuery> q(m_session.nominalSchema().newQuery());
74  q->addToTableList( "L1_CALO_INFO", "L1CI" );
75  q->setRowCacheSize( 5 );
76 
77  //Bind list
78  coral::AttributeList bindList;
79  bindList.extend<int>("Id");
80  std::string cond("");
81  if(data.smk()!=0) {
82  q->addToTableList( "SUPER_MASTER_TABLE", "SMT" );
83  q->addToTableList( "L1_MASTER_TABLE", "L1M" );
84  cond = "SMT.SMT_ID = :Id";
85  cond += " AND SMT.SMT_L1_MASTER_TABLE_ID = L1M.L1MT_ID";
86  cond += " AND L1CI.L1CI_ID = L1M.L1MT_CALO_INFO_ID";
87  bindList[0].data<int>() = data.smk();
88  } else {
89  cond = "L1CI.L1CI_ID = :Id";
90  bindList[0].data<int>() = data.id();
91  }
92  q->setCondition( cond, bindList );
93 
94  //Output data and types
95  coral::AttributeList attList;
96  attList.extend<std::string>( "L1CI.L1CI_NAME" );
97  attList.extend<int>( "L1CI.L1CI_VERSION" );
98  if(isRun1()) {
99  attList.extend<std::string>( "L1CI.L1CI_GLOBAL_SCALE" );
100  } else {
101  attList.extend<float>( "L1CI.L1CI_GLOBAL_EM_SCALE" );
102  attList.extend<float>( "L1CI.L1CI_GLOBAL_JET_SCALE" );
103  }
104  if(isRun2() || (isRun1() && schema>=10)) {
105  attList.extend<int>( "L1CI.L1CI_XS_SIGMA_SCALE" );
106  attList.extend<int>( "L1CI.L1CI_XS_SIGMA_OFFSET" );
107  attList.extend<int>( "L1CI.L1CI_XS_XE_MIN" );
108  attList.extend<int>( "L1CI.L1CI_XS_XE_MAX" );
109  attList.extend<int>( "L1CI.L1CI_XS_TESQRT_MIN" );
110  attList.extend<int>( "L1CI.L1CI_XS_TESQRT_MAX" );
111  }
112  if(isRun1()) {
113  for(unsigned int index = 1; index <= 12 ;index++) {
114  std::stringstream helpstring;
115  helpstring << "L1CI.L1CI_JET_WEIGHT" << index;
116  attList.extend<int>( helpstring.str() );
117  }
118  }
119  if(isRun2()) {
120  attList.extend<int>( "L1CI.L1CI_MIN_TOB_EM" );
121  attList.extend<int>( "L1CI.L1CI_MIN_TOB_TAU" );
122  attList.extend<int>( "L1CI.L1CI_MIN_TOB_JETS" );
123  attList.extend<int>( "L1CI.L1CI_MIN_TOB_JETL" );
124  attList.extend<int>( "L1CI.L1CI_ISO_HA_EM" );
125  attList.extend<int>( "L1CI.L1CI_ISO_EM_EM" );
126  attList.extend<int>( "L1CI.L1CI_ISO_EM_TAU" );
127  }
128 
129 
130  fillQuery(q.get(),attList);
131 
132  coral::ICursor& cursor = q->execute();
133 
134  if(!cursor.next()) {
135  if(data.smk()!=0) {
136  TRG_MSG_ERROR("No CaloInfo exists for SMK " << data.smk());
137  } else {
138  TRG_MSG_ERROR("No CaloInfo exists with ID " << data.id());
139  }
140  commitSession();
141  throw std::runtime_error( "CaloInfoLoader >> CaloInfo not available" );
142  }
143 
144  // fill the object with data
145  const coral::AttributeList& row = cursor.currentRow();
146  data.setName ( row["L1CI.L1CI_NAME"].data<std::string>() );
147  data.setVersion ( row["L1CI.L1CI_VERSION"].data<int>() );
148  if(isRun1()) {
149  if(schema <= 6)
150  data.setGlobalScale( row["L1CI.L1CI_GLOBAL_SCALE"].data<float>());
151  else
152  data.setGlobalScale( boost::lexical_cast<float,std::string>(row["L1CI.L1CI_GLOBAL_SCALE"].data<std::string>()));
153  } else {
154  data.setGlobalEmScale( row["L1CI.L1CI_GLOBAL_EM_SCALE"].data<float>() );
155  data.setGlobalJetScale( row["L1CI.L1CI_GLOBAL_JET_SCALE"].data<float>() );
156  }
157  if(isRun1()) {
158  for(unsigned int index = 1; index <= 12 ;index++) {
159  std::stringstream helpstring;
160  helpstring << "L1CI.L1CI_JET_WEIGHT" << index;
161  data.addJetWeight( static_cast<int>(row[helpstring.str()].data<int>()) );
162  }
163  }
164  if(isRun2() || (isRun1() && schema>=10)) {
165  int XSSigmaScale = row["L1CI.L1CI_XS_SIGMA_SCALE"].data<int>();
166  int XSSigmaOffset = row["L1CI.L1CI_XS_SIGMA_OFFSET"].data<int>();
167  int XEmin = row["L1CI.L1CI_XS_XE_MIN"].data<int>();
168  int XEmax = row["L1CI.L1CI_XS_XE_MAX"].data<int>();
169  int TESqrtMin = row["L1CI.L1CI_XS_TESQRT_MIN"].data<int>();
170  int TESqrtMax = row["L1CI.L1CI_XS_TESQRT_MAX"].data<int>();
171  data.metSigParam().setValues( XSSigmaScale, XSSigmaOffset,
172  XEmin, XEmax, TESqrtMin, TESqrtMax);
173  }
174 
175  vector<int> mintobIDs;
176  vector<int> isoparIDs;
177  if(isRun2()) {
178  mintobIDs.push_back(row["L1CI.L1CI_MIN_TOB_EM"].data<int>());
179  mintobIDs.push_back(row["L1CI.L1CI_MIN_TOB_TAU"].data<int>());
180  mintobIDs.push_back(row["L1CI.L1CI_MIN_TOB_JETS"].data<int>());
181  mintobIDs.push_back(row["L1CI.L1CI_MIN_TOB_JETL"].data<int>());
182  isoparIDs.push_back(row["L1CI.L1CI_ISO_HA_EM"].data<int>());
183  isoparIDs.push_back(row["L1CI.L1CI_ISO_EM_EM"].data<int>());
184  isoparIDs.push_back(row["L1CI.L1CI_ISO_EM_TAU"].data<int>());
185  }
186 
187  // check for uniqness of CaloInfo object
188  if ( cursor.next() ) {
189  TRG_MSG_ERROR("More than one CaloInfo exists " );
190  throw std::runtime_error( "Too many CaloInfo objects" );
191  }
192 
193  if(isRun2()) {
194  loadMinTobInfo(data, mintobIDs);
195  loadIsolationInfo(data, isoparIDs);
196  }
197 
198  commitSession();
199  }
200  catch( const coral::Exception& e ) {
201  TRG_MSG_ERROR("Coral::Exception: " << e.what());
202  m_session.transaction().rollback();
203  throw;
204  }
205  return true;
206 }
207 
208 
209 void
210 TrigConf::CaloInfoLoader::loadMinTobInfo( CaloInfo& data, const vector<int> & mintobIDs ) {
211 
212  unique_ptr<coral::IQuery> q(m_session.nominalSchema().tableHandle( "L1_CALO_MIN_TOB" ).newQuery());
213  q->setRowCacheSize( 4 );
214 
215  string cond("L1CMT_ID IN (");
216  bool first=true;
217  for(int id : mintobIDs) {
218  if(first) { first = false; } else { cond += ","; }
219  cond += boost::lexical_cast<string,int>(id);
220  }
221  cond += ")";
222  q->setCondition( cond, coral::AttributeList() );
223 
224  coral::AttributeList attList;
225  attList.extend<int>( "L1CMT_ID" );
226  attList.extend<string>( "L1CMT_THR_TYPE" );
227  attList.extend<int>( "L1CMT_WINDOW" );
228  attList.extend<int>( "L1CMT_PT_MIN" );
229  attList.extend<int>( "L1CMT_ETA_MIN" );
230  attList.extend<int>( "L1CMT_ETA_MAX" );
231  attList.extend<int>( "L1CMT_PRIORITY" );
232  fillQuery(q.get(),attList);
233 
234  coral::ICursor& cursor = q->execute();
235 
236  while ( cursor.next() ) {
237 
238  const coral::AttributeList& row = cursor.currentRow();
239 
240  string thrtype = row["L1CMT_THR_TYPE"].data<string>();
241  int window = row["L1CMT_WINDOW"].data<int>();
242  int ptmin = row["L1CMT_PT_MIN"].data<int>();
243  int etamin = row["L1CMT_ETA_MIN"].data<int>();
244  int etamax = row["L1CMT_ETA_MAX"].data<int>();
245  int priority = row["L1CMT_PRIORITY"].data<int>();
246 
247  if(ptmin<0) {
248  TRG_MSG_ERROR("MinTOBPt " << thrtype << " with pt " << ptmin << " which is less than 0");
249  throw runtime_error("PTMin of MinTOBPt found to be less than 0");
250  }
251  if(priority<0) {
252  TRG_MSG_ERROR("MinTOBPt " << thrtype << " with priority " << priority << " which is less than 0");
253  throw runtime_error("Priority of MinTOBPt found to be less than 0");
254  }
255 
256  MinTOBPt mintob((unsigned int)ptmin, etamin, etamax, (unsigned int) priority);
257 
258  if(thrtype=="EM") {
259  data.setMinTobEM(mintob);
260  } else if(thrtype=="TAU") {
261  data.setMinTobTau(mintob);
262  } else if(thrtype=="JETS") {
263  data.setMinTobJetSmall(mintob);
264  data.setJetWindowSizeSmall(window);
265  } else if(thrtype=="JETL") {
266  data.setMinTobJetLarge(mintob);
267  data.setJetWindowSizeLarge(window);
268  } else {
269  TRG_MSG_ERROR("Unknown threshold type " << thrtype);
270  throw runtime_error("MinTOBPt with unknown threshold type");
271  }
272 
273  }
274 
275 }
276 
277 
278 
279 void
280 TrigConf::CaloInfoLoader::loadIsolationInfo( CaloInfo& data, const vector<int> & isoparIDs ) {
281 
282  // first get a map of threshold type to isolation parameterizations
283 
284  map<string,vector<int>> m;
285 
286  {
287  unique_ptr<coral::IQuery> q(m_session.nominalSchema().tableHandle( "L1_CALO_ISOLATION" ).newQuery());
288  q->setRowCacheSize( 3 );
289 
290  string cond( "L1CIS_ID IN (" + join(isoparIDs) + ")" );
291  q->setCondition( cond, coral::AttributeList() );
292 
293  coral::AttributeList attList;
294  attList.extend<string>( "L1CIS_THR_TYPE" );
295  attList.extend<int>( "L1CIS_PAR1_ID" );
296  attList.extend<int>( "L1CIS_PAR2_ID" );
297  attList.extend<int>( "L1CIS_PAR3_ID" );
298  attList.extend<int>( "L1CIS_PAR4_ID" );
299  attList.extend<int>( "L1CIS_PAR5_ID" );
300  fillQuery(q.get(),attList);
301 
302  coral::ICursor& cursor = q->execute();
303 
304  while ( cursor.next() ) {
305 
306  const coral::AttributeList& row = cursor.currentRow();
307 
308  string thrtype = row["L1CIS_THR_TYPE"].data<string>();
309 
310  vector<int> & idbytype = m[thrtype];
311 
312  idbytype.push_back( row["L1CIS_PAR1_ID"].data<int>() );
313  idbytype.push_back( row["L1CIS_PAR2_ID"].data<int>() );
314  idbytype.push_back( row["L1CIS_PAR3_ID"].data<int>() );
315  idbytype.push_back( row["L1CIS_PAR4_ID"].data<int>() );
316  idbytype.push_back( row["L1CIS_PAR5_ID"].data<int>() );
317 
318  }
319 
320  }
321 
322  // second get the individual parameterizations
323 
324  {
325  for( const auto & isolation : m ) {
326 
327  unique_ptr<coral::IQuery> q(m_session.nominalSchema().tableHandle( "L1_CALO_ISOPARAM" ).newQuery());
328  q->setRowCacheSize( 5 );
329 
330  const string & thrtype = isolation.first;
331  const vector<int> & isoparIds = isolation.second;
332 
333  string cond( "L1CIP_ID IN (" + join(isoparIds) + ")" );
334  q->setCondition( cond, coral::AttributeList() );
335 
336  coral::AttributeList attList;
337  attList.extend<int>( "L1CIP_ID" );
338  attList.extend<int>( "L1CIP_ISO_BIT" );
339  attList.extend<int>( "L1CIP_OFFSET" );
340  attList.extend<int>( "L1CIP_SLOPE" );
341  attList.extend<int>( "L1CIP_MIN_CUT" );
342  attList.extend<int>( "L1CIP_UPPER_LIMIT" );
343  attList.extend<int>( "L1CIP_ETA_MIN" );
344  attList.extend<int>( "L1CIP_ETA_MAX" );
345  attList.extend<int>( "L1CIP_PRIORITY" );
346  fillQuery(q.get(),attList);
347 
348  coral::ICursor& cursor = q->execute();
349 
350  while ( cursor.next() ) {
351 
352  const coral::AttributeList& row = cursor.currentRow();
353 
354  unsigned int id = (unsigned int)row["L1CIP_ID"].data<int>();
355  unsigned int pos=1; // the position of this paramerization
356  for(unsigned int parId : isoparIds) {
357  if(id==parId) break;
358  ++pos;
359  }
360 
361  int isobit = row["L1CIP_ISO_BIT"].data<int>();
362  int offset = row["L1CIP_OFFSET"].data<int>();
363  int slope = row["L1CIP_SLOPE"].data<int>();
364  int mincut = row["L1CIP_MIN_CUT"].data<int>();
365  int upperlimit = row["L1CIP_UPPER_LIMIT"].data<int>();
366  int etamin = row["L1CIP_ETA_MIN"].data<int>();
367  int etamax = row["L1CIP_ETA_MAX"].data<int>();
368  int priority = row["L1CIP_PRIORITY"].data<int>();
369 
370 
371  if(isobit>0) {
372  if(pos!=(unsigned int)isobit) {
373  TRG_MSG_ERROR(thrtype << " isolation bit " << isobit << " does not match the position it should have " << pos);
374  throw runtime_error("Isolation bit not matching");
375  }
376  data.setIsolation(thrtype, pos, IsolationParam( thrtype, isobit, offset, slope, mincut, upperlimit, etamin, etamax, priority ));
377  } else {
378  data.setIsolation(thrtype, pos, IsolationParam());
379  }
380 
381 
382  }
383 
384  cursor.close();
385  }
386  }
387 }
TRG_MSG_ERROR
#define TRG_MSG_ERROR(x)
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStreamMacros.h:29
query_example.row
row
Definition: query_example.py:24
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
CaloInfoLoader.h
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
index
Definition: index.py:1
TrigJetMonitorAlgorithm.ptmin
ptmin
Definition: TrigJetMonitorAlgorithm.py:1081
DBHelper.h
TrigConf::fillQuery
void fillQuery(coral::IQuery *q, coral::AttributeList &attList)
Definition: DBHelper.cxx:13
python.selector.AtlRunQuerySelectorLhcOlc.priority
priority
Definition: AtlRunQuerySelectorLhcOlc.py:611
python.PyKernel.AttributeList
AttributeList
Definition: PyKernel.py:36
CheckTagAssociation.schema
schema
Definition: CheckTagAssociation.py:22
TrigConf::CaloInfo
Definition: CaloInfo.h:35
CaloInfo.h
TrigConf::CaloInfoLoader::loadIsolationInfo
void loadIsolationInfo(CaloInfo &data, const std::vector< int > &isoparIDs)
Definition: CaloInfoLoader.cxx:280
TrigConf::IsolationParam
Definition: IsolationParam.h:16
TRG_MSG_INFO
#define TRG_MSG_INFO(x)
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStreamMacros.h:27
TrigConf::CaloInfoLoader::load
virtual bool load(CaloInfo &data) override
Definition: CaloInfoLoader.cxx:55
python.TrigConfigSvcUtils.isRun2
def isRun2(cursor, schemaname)
Definition: TrigConfigSvcUtils.py:290
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
python.PyAthena.v
v
Definition: PyAthena.py:157
DeMoScan.index
string index
Definition: DeMoScan.py:362
DiTauMassTools::MaxHistStrategyV2::e
e
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:26
query_example.cursor
cursor
Definition: query_example.py:21
TrigConf::CaloInfoLoader::loadMinTobInfo
void loadMinTobInfo(CaloInfo &data, const std::vector< int > &mintobIDs)
Definition: CaloInfoLoader.cxx:210
DeMoScan.first
bool first
Definition: DeMoScan.py:534
convertTimingResiduals.offset
offset
Definition: convertTimingResiduals.py:71
extractSporadic.q
list q
Definition: extractSporadic.py:98
LArCellBinning.etamin
etamin
Definition: LArCellBinning.py:137
TrigConf::MinTOBPt
Definition: CaloInfo.h:19