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

// class declaration
Joerg Stelzer's avatar
Joerg Stelzer committed
6
#include "./HLTConfigSvc.h"
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

#include <exception>
#include <vector>

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

#include "GaudiKernel/ServiceHandle.h"
#include "GaudiKernel/IIncidentSvc.h"
#include "GaudiKernel/Incident.h"
#include "GaudiKernel/IJobOptionsSvc.h"
#include "GaudiKernel/ITHistSvc.h"
#include "GaudiKernel/Timing.h"

// Root incluces:
#include "TH1F.h"
#include "TH2I.h"

// Local includes:
28
#include "TrigConfIO/JsonFileLoader.h"
Joerg Stelzer's avatar
Joerg Stelzer committed
29
#include "TrigConfIO/TrigDBMenuLoader.h"
30
#include "TrigConfData/HLTMenu.h"
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "TrigConfBase/TrigDBConnectionConfig.h"
#include "TrigConfStorage/StorageMgr.h"
#include "TrigConfStorage/IStorageMgr.h"
#include "TrigConfStorage/XMLStorageMgr.h"
#include "TrigConfStorage/IHLTPrescaleSetLoader.h"
#include "TrigConfStorage/IHLTPrescaleSetCollectionLoader.h"
#include "TrigConfL1Data/HelperFunctions.h"
#include "TrigConfL1Data/CTPConfig.h" 
#include "TrigConfHLTData/HLTFrame.h"
#include "TrigConfHLTData/HLTChainList.h"
#include "TrigConfHLTData/HLTSequenceList.h"
#include "TrigConfHLTData/HLTPrescaleSet.h"
#include "TrigConfHLTData/HLTPrescaleSetCollection.h"
44
#include "AthenaMonitoringKernel/OHLockedHist.h"
45

46
47
#include "TrigConfData/HLTMenu.h"

Joerg Stelzer's avatar
Joerg Stelzer committed
48
49
50
#include "TrigConfInterfaces/IJobOptionsSvc.h"

#include "TrigConfInterfaces/IJobOptionsSvc.h"
51
52
53
54
55
56
57
58
59

#include "boost/algorithm/string/case_conv.hpp"
#include "boost/lexical_cast.hpp"

using namespace std;
using namespace TrigConf;

HLTConfigSvc::HLTConfigSvc( const string& name, ISvcLocator* pSvcLocator ) :
   base_class(name, pSvcLocator),
60
61
   m_eventStore( "StoreGateSvc/StoreGateSvc",  name ),
   m_histProp_timePrescaleUpdate(Gaudi::Histo1DDef("Time for prescale update",0,200,100))
62
63
64
65
66
67
68
69
70
71
72
73
{
   base_class::declareCommonProperties();

   declareProperty( "DBHLTPSKey",       m_dbHLTPSKey );
   declareProperty( "DBHLTPSKeySet",    m_dbHLTPSKeySet,
                    "List of HLT Prescale keys associated with start Lumiblocks LB1uPSK1cLB2uPSK2" );
   declareProperty( "HistTimePrescaleUpdate", m_histProp_timePrescaleUpdate,
                    "Histogram of time for prescale update");
   declareProperty( "PartitionName",    m_PartitionName,
                    "Name of the partition for the trigger prescale update mechanism");
   declareProperty( "doMergedHLT",      m_setMergedHLT,
                    "Set true to run the merged HLT processing");
74
75
   declareProperty( "doMonitoring",     m_doMon,
                    "Enable monitoring (mostly for online)");
76
77
78
79
80
81
}

HLTConfigSvc::~HLTConfigSvc()
{}


82
83
84
85
86
StatusCode
HLTConfigSvc::writeConfigToDetectorStore() {

    TrigConf::HLTMenu * hltmenu = new TrigConf::HLTMenu;

Joerg Stelzer's avatar
Joerg Stelzer committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
   if( m_inputType == "db" ) {

      // db menu loader
      TrigConf::TrigDBMenuLoader dbloader(m_dbConnection);
      dbloader.setLevel(TrigConf::MSGTC::WARNING);

      if( dbloader.loadHLTMenu( m_smk, *hltmenu ) ) {
         ATH_MSG_INFO( "Loaded HLT menu from DB " << m_dbConnection << " for SMK " << m_smk.value() );
      } else {
         ATH_MSG_WARNING( "Failed loading HLT menu from DB for SMK " << m_smk.value());
         return StatusCode::RECOVERABLE;
      }

   } else if ( m_inputType == "file" ) {

      // load the json file into TrigConf::HLTMenu
      TrigConf::JsonFileLoader fileLoader;
      fileLoader.setLevel(TrigConf::MSGTC::WARNING);

106
      if( fileLoader.loadFile( m_hltFileName, *hltmenu ) ) {
Joerg Stelzer's avatar
Joerg Stelzer committed
107
108
109
110
111
112
113
114
115
116
         ATH_MSG_INFO( "Loaded HLT menu file " << m_hltFileName.value() );
      } else {
         ATH_MSG_WARNING( "Failed loading HLT menu file " << m_hltFileName.value());
         return StatusCode::RECOVERABLE;
      }

   } else if( m_inputType == "cool" ) {
      ATH_MSG_FATAL( "Loading of HLT menu from COOL + DB not implemented");
      return StatusCode::FAILURE;
   }
117

Joerg Stelzer's avatar
Joerg Stelzer committed
118
119
120
121
   // To the reviewers: I will need to leave this commented, as one has to understand why this solution does not work
   // auto writeHandle = SG::makeHandle(m_hltMenuKey);
   // ATH_MSG_INFO("Recording HLT menu with " << m_hltMenuKey);
   // ATH_CHECK( writeHandle.record( std::unique_ptr<TrigConf::HLTMenu>(hltmenu) ));
122

Joerg Stelzer's avatar
Joerg Stelzer committed
123
124
125
126
127
   ServiceHandle<StoreGateSvc> detStore( "StoreGateSvc/DetectorStore", name() );
   ATH_CHECK( detStore.retrieve() );
   if( detStore->record(hltmenu,"HLTTriggerMenu").isSuccess() ) {
      ATH_MSG_INFO( "Recorded HLT menu with key 'HLTTriggerMenu' in the detector store" );
   }
128

Joerg Stelzer's avatar
Joerg Stelzer committed
129
   return StatusCode::SUCCESS;
130
131
132
} 


133
134
135
136
137
// Suppress warnings for two functions of this class marked as deprecated.
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
138
139
140
141
142
143
144
145
146
147
148
149
const HLTChainList*
HLTConfigSvc::chainList() const {
   return &m_HLTFrame.getHLTChainList();
}


const HLTSequenceList*
HLTConfigSvc::sequenceList() const {
   return &m_HLTFrame.getHLTSequenceList();
}


150
151
152
153
154
155
156
157
158
159
160
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif


const HLTChainList&
HLTConfigSvc::chains() const {
   return m_HLTFrame.getHLTChainList();
}


161
162
163
164
165
166
167
168
169
const HLTSequenceList&
HLTConfigSvc::sequences() const {
   return m_HLTFrame.getHLTSequenceList();
}


StatusCode
HLTConfigSvc::initialize() {

Joerg Stelzer's avatar
Joerg Stelzer committed
170
171
172
173
174
175
176
177
178
   // ATH_CHECK( m_hltMenuKey.initialize() );

   {
      /// Handle to JobOptionsSvc used to retrieve the DataFlowConfig property
      if( auto joSvc = serviceLocator()->service<TrigConf::IJobOptionsSvc>( "JobOptionsSvc" ) ) {
         if( joSvc->superMasterKey()>0 ) {
            m_inputType = "db";
            m_smk = joSvc->superMasterKey();
            m_dbConnection = joSvc->server();
179
            m_configSourceString = "none";
Joerg Stelzer's avatar
Joerg Stelzer committed
180
181
182
183
         }
      } else {
         ATH_MSG_INFO("Did not locate TrigConf::JobOptionsSvc, not running athenaHLT");
      }
184

Joerg Stelzer's avatar
Joerg Stelzer committed
185
186
187
188
      StatusCode sc = writeConfigToDetectorStore();
      if( !sc.isSuccess() ) {
         ATH_MSG_INFO( "This 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.");
      }
189
   }
190

Joerg Stelzer's avatar
Joerg Stelzer committed
191
192
   ATH_CHECK(ConfigSvcBase::initialize());

Tim Martin's avatar
Tim Martin committed
193
194
   std::string xmlFile(boost::to_lower_copy(m_xmlFile)); // lower case
   if( !fromDB() and (xmlFile=="none" or xmlFile == "")) {
195
196
197
      ATH_MSG_INFO("xml file set to NONE, will not load HLT Menu");
      return StatusCode::SUCCESS;
   }
198
   
199
200
201
202
203
   // get the partition name
   // non-empty job-property overwrite value from DataFlowConfig
   if(m_PartitionName.value() !="") {
      m_partition = m_PartitionName;
   } else {
Joerg Stelzer's avatar
Joerg Stelzer committed
204
      ServiceHandle<::IJobOptionsSvc> jobOptionsSvc("JobOptionsSvc", name());
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
      if (jobOptionsSvc.retrieve().isFailure()) {
         ATH_MSG_WARNING("Cannot retrieve JobOptionsSvc");
      } else {
         const Property* p=Gaudi::Utils::getProperty( jobOptionsSvc->getProperties("DataFlowConfig"), "DF_PartitionName");   
         if (p != 0) m_partition = p->toString();
      }
   }


   if( fromDB() ) {
      ATH_MSG_INFO("    DB HLT PrescaleKey  = " << m_dbHLTPSKey);
      ATH_MSG_INFO("    DB HLT PrescaleKeys = " << m_dbHLTPSKeySet);
   }
   ATH_MSG_INFO("    Partition           = " << m_partition);
   ATH_MSG_INFO("    Run merged HLT      = " << m_setMergedHLT);


222
   if( fromDB() && m_dbHLTPSKey!=0 && !m_dbconfig->m_hltkeys.empty() )
223
224
225
226
      ATH_MSG_WARNING("DBHLTPSKey and DBHLTPSKeySet both specified, the DBHLTPSKeySet will be ignored");

   // Read the HLT configuration
   m_HLTFrame.setMergedHLT( m_setMergedHLT );
227
228
229
230
231
232
233
234
235
236
   if(fromDB())
   {
     m_HLTFrame.setSMK( m_dbSMKey );
     if(m_dbHLTPSKey) {
       m_HLTFrame.thePrescaleSetCollection().set_prescale_key_to_load(
           m_dbHLTPSKey);
     } else {
       m_HLTFrame.thePrescaleSetCollection().set_prescale_keys_to_load(
           m_dbconfig->m_hltkeys);
     }
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
   }

   CHECK(initStorageMgr());

   try {
      if( ! m_storageMgr->hltFrameLoader().load( m_HLTFrame ) ){
         ATH_MSG_ERROR("Selected keys could not be loaded");
         return StatusCode::FAILURE;
      }
      ATH_MSG_VERBOSE("HLTFrame configuration object loaded");
   }
   catch (exception & e) {
      ATH_MSG_ERROR("Standard exception caught: " << e.what());
      return StatusCode::FAILURE;
   }

   CHECK(freeStorageMgr());

   // populate the lower_chain_counter of the EF chains. 
   // to do so, take the EF chain's lower_chain_name and loop through 
   // all L2 chains to find the matching chain_counter 

   for( HLTChain* ch: m_HLTFrame.chains() ) {
      if( ch->level_enum() != EF ) continue;
      unsigned int lower_chain_counter = -1;
      if(ch->lower_chain_name() != "") {
         HLTChain* lowerChain = m_HLTFrame.chains().chain(ch->lower_chain_name());
         if(lowerChain) lower_chain_counter = lowerChain->chain_counter();
      }
      ch->set_lower_chain_counter( lower_chain_counter );
   }
   
   // print the menu after loading 
   if (msgSvc()->outputLevel() <= MSG::VERBOSE) {
      m_HLTFrame.print("  ", 5);
   } else if (msgSvc()->outputLevel() <= MSG::DEBUG) {
      m_HLTFrame.print("  ", 3);
   } else if (msgSvc()->outputLevel() <= MSG::INFO) {
      m_HLTFrame.print("  ",m_printMenuLevel);
   }
   m_HLTFrame.getPrescaleSetCollection().print();

   ServiceHandle<IIncidentSvc> incSvc("IncidentSvc",name());
   if (incSvc.retrieve().isFailure()) {
      ATH_MSG_WARNING("Cannot retrieve IncidentSvc");
   } else {
      string incname("HLTMenu HLTPS");
      incSvc->fireIncident( Incident(incname, "TrigConf") );
      ATH_MSG_INFO("Fired Incident 'TrigConf' - " << incname);
   }

288
289
   CHECK( m_eventStore.retrieve() );

290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
   ATH_MSG_INFO("finish initialize");

   return StatusCode::SUCCESS;
}


/***********************************************************
 * start() is called by Gaudi::ServiceManager::addService()
 * which adds the  service to the active list
 *
 * Online this is called during prepareForRun() going from
 * from CONNECTED to PREPARED
 ***********************************************************/
StatusCode
TrigConf::HLTConfigSvc::start() {

   ATH_MSG_INFO("HLTConfigSvc::start");

   // Book histograms
309
   if (m_doMon) bookHistograms().ignore();
310
311
312
313
314
315
   m_currentLumiblock = 0;

   if( ! fromDB() ) // xml config
      return StatusCode::SUCCESS;

   // also load the prescale set for the initial HLT PSK into the PrescaleSetCollection
316
   if(m_dbHLTPSKey) {
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349

      ATH_MSG_INFO("Loading PrescaleSet for initial HLT PSK: " << m_dbHLTPSKey);

      // Make sure we are starting with an empty prescale set collection
      m_HLTFrame.thePrescaleSetCollection().clear();
 
      HLTPrescaleSet* pss = new HLTPrescaleSet();
      pss->setId(m_dbHLTPSKey);

      CHECK(initStorageMgr());

      bool loadSuccess = dynamic_cast<TrigConf::StorageMgr*>(m_storageMgr)->hltPrescaleSetLoader().load( *pss );

      CHECK(freeStorageMgr());

      if(!loadSuccess) return StatusCode::FAILURE;

      m_HLTFrame.thePrescaleSetCollection().setPrescaleSet(pss); // assumes LB=0

      applyPrescaleSet(*pss);
    
      ATH_MSG_INFO( m_HLTFrame.getPrescaleSetCollection() );
   }
  
   return StatusCode::SUCCESS;
}


void
TrigConf::HLTConfigSvc::applyPrescaleSet(const TrigConf::HLTPrescaleSet& pss) {

  ATH_MSG_INFO("Applying PSK " << pss.id() << " to menu ");
  const EventInfo* pEvent(0);
350
  if ( m_eventStore->retrieve(pEvent).isSuccess() ) {
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
     ATH_MSG_INFO("on event " << *pEvent->event_ID());
  }

  m_HLTFrame.theHLTChainList().applyPrescaleSet(&pss);

  m_currentPSS = pss.id();

}


StatusCode
TrigConf::HLTConfigSvc::bookHistograms() {

   ServiceHandle<ITHistSvc> histSvc("THistSvc", name());

   CHECK(histSvc.retrieve());

   const std::string histPath = "/EXPERT/" + name() + "/";

   m_hist_timePrescaleUpdate = new TH1F("TimePrescaleUpdate",
                                        (m_histProp_timePrescaleUpdate.value().title() + ";time [ms]").c_str(),
                                        m_histProp_timePrescaleUpdate.value().bins(),
                                        m_histProp_timePrescaleUpdate.value().lowEdge(),
                                        m_histProp_timePrescaleUpdate.value().highEdge());
   const int nLB(1), nKey(1);
   m_hist_prescaleLB = new TH2I("PrescaleKey_LB","Prescale key used in LB;Lumiblock;Prescale key",
                                nLB, 0, nLB, nKey, 0, nKey);

   // try to register, delete if failure
   TH1** hists[] = { (TH1**)&m_hist_timePrescaleUpdate,
                     (TH1**)&m_hist_prescaleLB };
   for ( TH1** h: hists ) {
      if ( *h && histSvc->regHist(histPath + (*h)->GetName(), *h).isFailure() ) {
         ATH_MSG_WARNING("Cannot register histogram " << (*h)->GetName());
         delete *h;
         *h = 0;
      }
   }

   return StatusCode::SUCCESS;
}


StatusCode
TrigConf::HLTConfigSvc::finalize() {
   ATH_MSG_DEBUG("Finalizing");
   CHECK(AthService::finalize());
   return StatusCode::SUCCESS;
}


StatusCode
TrigConf::HLTConfigSvc::queryInterface( const InterfaceID& riid, void** ppvIF ) {
   StatusCode sc = StatusCode::FAILURE;

   if (ppvIF) {
      *ppvIF = 0;

      if (riid == IHLTConfigSvc::interfaceID()) {
         try {
            *ppvIF = dynamic_cast<IHLTConfigSvc*>( this );
         }
413
         catch( const std::bad_cast& ) {
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
            return StatusCode::FAILURE;
         }
         sc = StatusCode::SUCCESS;
      }
      else {
         sc = Service::queryInterface( riid, ppvIF );
      }

   }

   if ( sc.isSuccess() ) addRef();
   return sc;

}

uint32_t
TrigConf::HLTConfigSvc::masterKey() const {
   return m_HLTFrame.smk();
}

uint32_t
TrigConf::HLTConfigSvc::hltPrescaleKey() const {
   uint32_t key = m_HLTFrame.getPrescaleSet() ? m_HLTFrame.getPrescaleSet()->id() : 0;
   return key;
}

void
TrigConf::HLTConfigSvc::setL2LowerChainCounter(const CTPConfig* ctpcfg) {
   m_HLTFrame.theHLTChainList().setL2LowerChainCounter(ctpcfg);
}



// query the TriggerDB for the list of lumiblocks and corresponding prescalekeys
// will then load the and prescaleSets that have not yet been loaded
StatusCode
TrigConf::HLTConfigSvc::updatePrescaleSets(uint requestcount) {

   if( ! fromDB() ) { // xml 
      ATH_MSG_WARNING("Configured to not run from the database!");
      return StatusCode::SUCCESS;
   }
   
457
   if( !m_dbconfig->m_hltkeys.empty() ) {
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
      ATH_MSG_WARNING("Has list of [(lb1,psk1), (lb2,psk2),...] defined!");
      return StatusCode::SUCCESS;
   }

   // Start timer
   longlong t1_ms = System::currentTime(System::milliSec);

   // Load prescale set
   CHECK(initStorageMgr());

   bool loadSuccess = dynamic_cast<TrigConf::StorageMgr*>
      (m_storageMgr)->hltPrescaleSetCollectionLoader().load( m_HLTFrame.thePrescaleSetCollection(), requestcount, m_partition );

   CHECK(freeStorageMgr());

   // Stop timer and fill histogram
   uint64_t t2_ms = System::currentTime(System::milliSec);
   if (m_hist_timePrescaleUpdate) m_hist_timePrescaleUpdate->Fill(t2_ms-t1_ms);

   if(!loadSuccess) {
      ATH_MSG_WARNING("HLTConfigSvc::updatePrescaleSets(): loading failed");
      return StatusCode::FAILURE;
   } else {
      ATH_MSG_INFO ( m_HLTFrame.thePrescaleSetCollection() );
   }
   return StatusCode::SUCCESS;
}


// Helper for assignPrescalesToChains
namespace {
   inline void fillPrescaleHist(TH2I* h, uint lb, int psk) {
      if (h==0) return;
      // Use alpha-numeric bin labels to ensure correct gathering
492
      char buf_psk[12], buf_lb[12];
493
494
495
496
497
      snprintf(buf_psk, sizeof(buf_psk), "%d", psk);  // faster than stringstream
      snprintf(buf_lb, sizeof(buf_lb), "%03d", lb);
      // Save number of bins and perform a locked Fill
      int xbins = h->GetNbinsX();    
      int ybins = h->GetNbinsY();
Tim Martin's avatar
Tim Martin committed
498
      oh_lock_histogram<TH2I> locked_hist(h);
499
500

      locked_hist->Fill(buf_lb, buf_psk, 1);
501

502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
      // Need to make sure that all bins have labels (see Savannah #58243)    
      // Label additional bins on x-axis
      if ( h->GetNbinsX()!=xbins ) {
         const int N = h->GetNbinsX();
         for (int b=xbins+2; b<=N; ++b) {
            snprintf(buf_lb, sizeof(buf_lb), "%03d", ++lb);
            locked_hist->GetXaxis()->SetBinLabel(b, buf_lb);
         }
      }
      // Remove additional bins on y-axis
      if ( h->GetNbinsY()!=ybins ) locked_hist->LabelsDeflate("Y");
   }
}


// Assigns the prescales that are valid for a given lumiblock to the chains
// This method is called by TrigSteer on *every* event (keep it fast)
StatusCode
TrigConf::HLTConfigSvc::assignPrescalesToChains(uint lumiblock) {

   if(! fromDB() ) // xml
      return StatusCode::SUCCESS;

   if(lumiblock == m_currentLumiblock) {
      fillPrescaleHist(m_hist_prescaleLB, lumiblock, m_currentPSS);
      return StatusCode::SUCCESS;
   }

   m_currentLumiblock = lumiblock;
   
   // get the HLTPrescaleSet
   const HLTPrescaleSet* pss = m_HLTFrame.getPrescaleSetCollection().prescaleSet(lumiblock);
   if (pss == 0) {
      ATH_MSG_ERROR("Could not retrieve HLT prescale set for lumiblock = " << lumiblock);
      return StatusCode::FAILURE;
   }

   fillPrescaleHist(m_hist_prescaleLB, lumiblock, pss->id());
   
   // still the same HLTPSS -> nothing to do
   if(pss->id() ==  m_currentPSS) {
      return StatusCode::SUCCESS;
   }

   ATH_MSG_INFO("Changing PSK from " << m_currentPSS << " to " << pss->id()
                << " for lumiblock " << lumiblock);

   applyPrescaleSet(*pss);
   
   return StatusCode::SUCCESS;
}