LVL1ConfigSvc.cxx 10.2 KB
Newer Older
1
/*
2
  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3
4
*/

Joerg Stelzer's avatar
Joerg Stelzer committed
5
6
#include "./LVL1ConfigSvc.h"
#include "./Verifyer.h"
7
8
9
10
11
12
13
14
15
16
17
18
19


// Athena/Gaudi includes:
#include "PathResolver/PathResolver.h"

#include "GaudiKernel/ServiceHandle.h"
#include "GaudiKernel/IIncidentSvc.h"
#include "GaudiKernel/Incident.h"

#include "StoreGate/StoreGateSvc.h"


// Trigger database interface includes:
20
#include "TrigConfIO/JsonFileLoader.h"
Joerg Stelzer's avatar
Joerg Stelzer committed
21
#include "TrigConfIO/TrigDBMenuLoader.h"
22
#include "TrigConfData/L1Menu.h"
23

24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "TrigConfL1Data/DeadTime.h"
#include "TrigConfL1Data/CTPConfig.h"
#include "TrigConfL1Data/CTPConfigOnline.h"
#include "TrigConfL1Data/Menu.h"
#include "TrigConfL1Data/ThresholdConfig.h"
#include "TrigConfL1Data/Muctpi.h"
#include "TrigConfL1Data/PrescaledClock.h"
#include "TrigConfL1Data/Random.h"
#include "TrigConfStorage/StorageMgr.h"
#include "TrigConfStorage/XMLStorageMgr.h"
#include "TrigConfL1Data/TriggerThreshold.h"
#include "TrigConfL1Data/TriggerThresholdValue.h"
#include "TrigConfL1Data/ClusterThresholdValue.h"
#include "TrigConfL1Data/EtThresholdValue.h"
#include "TrigConfL1Data/JetThresholdValue.h"
#include "TrigConfL1Data/L1DataDef.h"

41
42
#include "TrigConfBase/TrigDBConnectionConfig.h"

Joerg Stelzer's avatar
Joerg Stelzer committed
43
#include "TrigConfInterfaces/IJobOptionsSvc.h"
44

45
#include "boost/algorithm/string.hpp"
Joerg Stelzer's avatar
Joerg Stelzer committed
46
47
48
49

// STL includes:
#include <exception>
#include <vector>
50
51
52

using namespace std;

53
54
55
TrigConf::LVL1ConfigSvc::LVL1ConfigSvc( const std::string& name, ISvcLocator* pSvcLocator ) :
   base_class( name, pSvcLocator )
{
56
57
58
   base_class::declareCommonProperties();
}

59
TrigConf::LVL1ConfigSvc::~LVL1ConfigSvc()
60
61
{}

62
63
64
const TrigConf::ThresholdConfig*
TrigConf::LVL1ConfigSvc::thresholdConfig() const { return m_ctpConfig ? &m_ctpConfig->menu().thresholdConfig() : nullptr; }

65

66
StatusCode
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
TrigConf::LVL1ConfigSvc::initializeRun3StyleMenu() {

   m_inputType = boost::to_lower_copy(m_inputType.value()); // lower case

   if( auto joSvc = serviceLocator()->service<TrigConf::IJobOptionsSvc>( "JobOptionsSvc" ) ) {
      if( joSvc->superMasterKey()>0 ) {
         ATH_MSG_INFO("Taking new style menu from db with setting from TrigConf::JobOptionsSvc");
         m_inputType = "db";
         m_smk = joSvc->superMasterKey();
         m_dbConnection = joSvc->server();
      }
   }

   ATH_MSG_INFO("Run 3 style menu configuration");
   ATH_MSG_INFO("    Run 3 input type  = " << m_inputType.value());
   if( m_inputType == "file" ) {
      ATH_MSG_INFO("    Run 3 input file  = " << m_l1FileName.value());
   } else if ( m_inputType == "db" ) {
      ATH_MSG_INFO("    Run 3 DB connection  = " << m_dbConnection);
      ATH_MSG_INFO("    Run 3 SMK            = " << m_smk);
   }
   if( ! loadRun3StyleMenu().isSuccess() ) {
      ATH_MSG_INFO( "The previous WARNING message is being ignored in the current transition phase. Once we rely entirely on the new menu providing mechanism, this will become a reason to abort.");
   }
   return StatusCode::SUCCESS;
}
93

94
95
96

StatusCode
TrigConf::LVL1ConfigSvc::loadRun3StyleMenu() {
97
98
99
100
101
102
   // do not write empty menu to storegate, so people can check on handlekey being valid
   if( m_inputType == "none" ) {
      ATH_MSG_INFO( "No L1 menu recorded in the detector store" );
      return StatusCode::SUCCESS;
   }
   TrigConf::L1Menu * l1menu = new TrigConf::L1Menu();
Joerg Stelzer's avatar
Joerg Stelzer committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
   if( m_inputType == "db" ) {
      // db menu loader
      TrigConf::TrigDBMenuLoader dbloader(m_dbConnection);
      dbloader.setLevel(TrigConf::MSGTC::WARNING);
      if( dbloader.loadL1Menu( m_smk, *l1menu ) ) {
         ATH_MSG_INFO( "Loaded L1 menu from DB " << m_dbConnection << " for SMK " << m_smk.value() );
      } else {
         ATH_MSG_WARNING( "Failed loading L1 menu from DB for SMK " << m_smk.value());
         return StatusCode::RECOVERABLE;
      }
   } else if ( m_inputType == "file" ) {
      // json file menu loader
      TrigConf::JsonFileLoader fileLoader;
      fileLoader.setLevel(TrigConf::MSGTC::WARNING);
117
      if( fileLoader.loadFile( m_l1FileName, *l1menu ) ) {
Joerg Stelzer's avatar
Joerg Stelzer committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
         ATH_MSG_INFO( "Loaded L1 menu file " << m_l1FileName.value() );
      } else {
         ATH_MSG_WARNING( "Failed loading L1 menu file " << m_l1FileName.value());
         return StatusCode::RECOVERABLE;
      }
   } else if( m_inputType == "cool" ) {
      ATH_MSG_FATAL( "Loading of L1 menu from COOL + DB not implemented");
      return StatusCode::FAILURE;
   }
   // auto writeHandle = SG::makeHandle(m_l1MenuKey);
   // ATH_MSG_INFO("Recording L1 menu with " << m_l1MenuKey);
   // ATH_CHECK( writeHandle.record( std::unique_ptr<TrigConf::L1Menu>(l1menu) ));
   ServiceHandle<StoreGateSvc> detStore( "StoreGateSvc/DetectorStore", name() );   
   ATH_CHECK( detStore.retrieve() );
   if( detStore->record(l1menu,"L1TriggerMenu").isSuccess() ) {
      ATH_MSG_INFO( "Recorded L1 menu with key 'L1TriggerMenu' in the detector store" );
   }
   return StatusCode::SUCCESS;
136
137
138
139
} 



140
StatusCode
141
TrigConf::LVL1ConfigSvc::initializeRun2StyleMenu() {
142

143
   m_configSourceString = boost::to_lower_copy(m_configSourceString); // lower case
Joerg Stelzer's avatar
Joerg Stelzer committed
144

145
146
147
148
149
150
   if ( m_configSourceString == "none" ) {
      ATH_MSG_INFO("Run 2 style menu has been disabled");
      m_xmlFile = "";
   } else if( m_configSourceString == "xml") {
      if( boost::algorithm::ends_with(m_xmlFile, ".xml") && ! boost::algorithm::starts_with(m_xmlFile, "./")  ) {
         m_xmlFile = PathResolver::find_file( m_xmlFile, "XMLPATH" );
Joerg Stelzer's avatar
Joerg Stelzer committed
151
152
      }
   } else {
153
154
155
156
157
158
159
160
161
162
      TrigDBConnectionConfig::DBType dbtype(TrigDBConnectionConfig::DBLookup);
      if (m_configSourceString == "oracle") { dbtype = TrigDBConnectionConfig::Oracle; }
      else if (m_configSourceString == "mysql")  { dbtype = TrigDBConnectionConfig::MySQL; }
      else if (m_configSourceString == "sqlite") { dbtype = TrigDBConnectionConfig::SQLite; }
      if(m_dbHLTPSKey) {
         m_dbconfig = std::make_unique<TrigDBConnectionConfig>( dbtype, m_dbServer, m_dbSMKey, m_dbHLTPSKey );
      } else {
         m_dbconfig = std::make_unique<TrigDBConnectionConfig>( dbtype, m_dbServer, m_dbSMKey, m_dbHLTPSKeySet );
      }
      m_dbconfig->m_useFrontier = m_useFrontier;
Joerg Stelzer's avatar
Joerg Stelzer committed
163
   }
164

165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
   ATH_MSG_INFO("Run 2 style menu");
   ATH_MSG_INFO("    Run 2 config source  = " << m_configSourceString);
   if( m_configSourceString != "none" ) {
      if( m_configSourceString == "xml" ) {
         ATH_MSG_INFO("    Run 2 Input file     = " << m_xmlFile);
      } else {
         ATH_MSG_INFO("    Run 2 DB Server      = " << m_dbServer);
         ATH_MSG_INFO("    Run 2 DB User        = " << m_dbUser);
         ATH_MSG_INFO("    Run 2 DB Table       = " << m_dbTable);
         ATH_MSG_INFO("    Run 2 DB connection  = " << m_dbconfig->toString());
         ATH_MSG_INFO("    Run 2 SMK            = " << m_dbSMKey);
         ATH_MSG_INFO("    Run 2 L1 PSK         = " << m_prescaleSetID);
         ATH_MSG_INFO("    Run 2 BGSK           = " << m_bunchgroupSetID);
         ATH_MSG_INFO("    UseFrontier          = " << m_useFrontier.value());
      }
180
   }
Joerg Stelzer's avatar
Joerg Stelzer committed
181

182
   if( m_dbconfig==nullptr && m_xmlFile=="NONE" ) {
183
      ATH_MSG_INFO("xml file set to NONE, will not load LVL1 Menu");
184
185
   } else {
      CHECK( loadRun2StyleMenu() );
186
   }
187
188
   return StatusCode::SUCCESS;
}
189
190


191
192
193
194
195
/**
 * Loading Run-2-style menu
 */
StatusCode
TrigConf::LVL1ConfigSvc::loadRun2StyleMenu() {
Joerg Stelzer's avatar
Joerg Stelzer committed
196
   CHECK( initStorageMgr() );
197
198
199
200
201
   try {
      m_ctpConfig = new CTPConfig();
      m_ctpConfig->setSMK( m_dbSMKey );
      m_ctpConfig->setPrescaleSetId( m_prescaleSetID );
      m_ctpConfig->setBunchGroupSetId( m_bunchgroupSetID );
202
203
204
205
206
      if(!m_storageMgr->masterTableLoader().load( *m_ctpConfig ))
      {
         ATH_MSG_ERROR("Could not load CTPConfig object");
         return StatusCode::FAILURE;
      }
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
      ATH_MSG_VERBOSE("CTPConfig object loaded");
      m_muctpi = new Muctpi();
      m_muctpi->setSMK( m_dbSMKey );
      m_storageMgr->masterTableLoader().load( *m_muctpi );
      ATH_MSG_VERBOSE("MuCTPI configuration object loaded");
   } catch ( std::exception & e ) {
      CHECK(freeStorageMgr());
      ATH_MSG_ERROR("Standard C++ exception caught: " << e.what());
      return StatusCode::FAILURE;
   } catch ( ... ) {
      CHECK(freeStorageMgr());
      ATH_MSG_ERROR("Unknown exception caught.");
      return StatusCode::FAILURE;
   }

   CHECK(freeStorageMgr());

224
225
226
   /**
    * Check if we have a valid trigger menu:
    */
227
228
229
230
231
232
233
   if( Verifyer::verify( m_ctpConfig->menu(), msg() ) ) {
      ATH_MSG_ERROR("Loaded trigger menu fails verification");
      return StatusCode::FAILURE;
   } else {
      ATH_MSG_DEBUG("Loaded trigger menu passed verification");
   }

234
235
   return StatusCode::SUCCESS;
}
236
237


238
239
240
241
242
243
244
245
246
247
248
249
250
251
StatusCode
TrigConf::LVL1ConfigSvc::initialize() {

   CHECK(AthService::initialize());

   /// Handle to JobOptionsSvc used to retrieve the DataFlowConfig property
   ATH_MSG_INFO("=================================");
   ATH_MSG_INFO("Initializing " << name() << " service");
   ATH_MSG_INFO("=================================");

   /*
     initializing and load new and old menu
   */
   CHECK( initializeRun3StyleMenu() );
252

253
   CHECK( initializeRun2StyleMenu() );
254
255

   //
256
   // fire incident (this has to be revisited)
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
   // 
   ServiceHandle<IIncidentSvc> incSvc("IncidentSvc",name());
   if (incSvc.retrieve().isFailure()) {
      ATH_MSG_WARNING("Cannot retrieve IncidentSvc");
   } else {
      // prefered
      std::string incname = "";
      incname += "L1Menu";
      incname += "L1PS";
      incSvc->fireIncident(Incident(incname,"TrigConf"));
      ATH_MSG_INFO("Fired Incident 'TrigConf' - " << incname);    
   }

   return StatusCode::SUCCESS;
}

StatusCode
274
TrigConf::LVL1ConfigSvc::finalize() {
275
276
277
278
279
280
281
282
283
284
285
286
287

   ATH_MSG_DEBUG("Finalizing");

   delete m_ctpConfig;
   delete m_muctpi;

   CHECK(AthService::finalize());

   return StatusCode::SUCCESS;

}

StatusCode
288
TrigConf::LVL1ConfigSvc::queryInterface( const InterfaceID& riid, void** ppvIF ) {
289
290
291
292
293
294
295
296
297

   StatusCode sc = StatusCode::FAILURE;

   if( ppvIF ) {
      *ppvIF = 0;

      if( riid == ILVL1ConfigSvc::interfaceID() ) {
         try {
            *ppvIF = dynamic_cast<ILVL1ConfigSvc*>( this );
298
         } catch( const bad_cast& ) {
299
300
301
302
303
304
305
306
307
308
309
310
311
312
            return StatusCode::FAILURE;
         }
         sc = StatusCode::SUCCESS;
      } else {
         sc = Service::queryInterface( riid, ppvIF );
      }

   }

   return sc;

}


313
314
const TrigConf::BunchGroupSet*
TrigConf::LVL1ConfigSvc::bunchGroupSet() const { 
315
316
317
   if (ctpConfig()==0) return 0;
   return &ctpConfig()->bunchGroupSet();
}