ATLAS Offline Software
TrigSORFromPtreeHelper.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
11 
12 #include <ctime>
13 #include <eformat/DetectorMask.h>
14 
15 using namespace boost::property_tree;
16 
17 namespace
18 {
19  const std::string CLNAME{"TrigSORFromPtreeHelper"};
20 }
21 
25  const std::string& sorpath,
26  const ptree& rparams) :
27  AthMessaging(msgSvc, CLNAME),
28  m_detStore(detStore),
29  m_sorpath(sorpath),
30  m_rparams(rparams)
31 {
32  // Set run number and timestamp from RunParams. Can be overwritten later.
33  m_runNumber = rparams.get<unsigned int>("run_number");
34 
35  std::istringstream ss(rparams.get_child("timeSOR").data());
36  std::tm t = {};
37  t.tm_isdst = -1; // auto-detect daylight savings time
38  unsigned int microseconds = 0;
39 
40  // Format: 08/05/24 22:14:38.000000 (see OWLTime in tdaq)
41  ss >> std::get_time(&t, "%d/%m/%y %H:%M:%S.") >> microseconds;
42  if (ss.fail()) {
43  ATH_MSG_ERROR("Cannot parse timeSOR: " << ss.str());
44  }
45  m_sorTime_ns = std::mktime(&t)*std::nano::den + microseconds*1000;
46 }
47 
49 StatusCode TrigSORFromPtreeHelper::fillSOR(const EventContext& ctx) const
50 {
51  ATH_MSG_DEBUG("Setup SOR in DetectorStore");
52 
53  // get handle to the IOVDbSvc
54  ServiceHandle<IIOVDbSvc> iovdbsvc("IOVDbSvc", CLNAME);
55  if ((iovdbsvc.retrieve()).isFailure()) {
56  ATH_MSG_ERROR("Could not find IOVDbSvc. Time dependent conditions data may be not properly handled.");
57  } else {
58  IOVTime currentIOVTime(ctx.eventID());
59  // Signal BeginRun directly to IOVDbSvc to set complete IOV start time
60  if ( iovdbsvc->signalBeginRun(currentIOVTime, ctx).isFailure() ) {
61  ATH_MSG_ERROR("Unable to signal begin run IOVTime to IOVDbSvc. IOVTime = " << currentIOVTime);
62  } else {
63  ATH_MSG_DEBUG("Set start of run time to IOVTime = " << currentIOVTime);
64  }
65  }
66 
67  // createSOR marked unsafe due to copying/deletion of coral::AttributeList
68  // objects. There's no sharing of specificiations, though, so it's ok.
69  StatusCode sc ATLAS_THREAD_SAFE = createSOR();
70  ATH_CHECK( sc );
71 
72  return StatusCode::SUCCESS;
73 }
74 
77 {
78  EventIDBase eid;
79 
80  // Set run and timestamp
81  eid.set_run_number( m_runNumber );
82  eid.set_time_stamp( m_sorTime_ns / (1000*1000*1000) );
83  eid.set_time_stamp_ns_offset( m_sorTime_ns % (1000*1000*1000) );
84 
85  eid.set_lumi_block(0); // our best guess as this is not part of RunParams
86 
87  return eid;
88 }
89 
91 StatusCode TrigSORFromPtreeHelper::createSOR ATLAS_NOT_THREAD_SAFE () const
92 {
93  // obtain SOR contents from ptree
94  auto attrList = getAttrList();
95 
96  // Validity
97  IOVTime iovTimeStart(attrList["RunNumber"].data<unsigned int>(),0);
98  IOVTime iovTimeStop(attrList["RunNumber"].data<unsigned int>()+1,0);
99  IOVRange iovRange(iovTimeStart, iovTimeStop);
100 
101  auto sor = new SOR(/*hasRunLumiBlockTime*/true);
102  sor->add(SOR::ChanNum{0}, attrList);
103  sor->add(SOR::ChanNum{0}, iovRange);
104  sor->resetMinRange();
105  sor->addNewStart(iovTimeStart);
106  sor->addNewStop(iovTimeStop);
107 
108  // Record or overwrite existing SOR
109  if ( m_detStore->transientContains<SOR>(m_sorpath) ) {
110  const SOR * oldsor = m_detStore->retrieve<const SOR>(m_sorpath);
111  ATH_MSG_INFO("Overwriting SOR contents (a dump of the old one follows):");
112  oldsor->dump();
113  ATH_CHECK( m_detStore->overwrite(sor, m_sorpath, true) );
114  }
115  else {
116  ATH_MSG_DEBUG("Recording new SOR");
117  ATH_CHECK( m_detStore->record(sor, m_sorpath, true) );
118  }
119 
120  ATH_CHECK( setIOVRange(iovRange) );
121  ATH_CHECK( updateProxy(sor) );
122 
123  ATH_MSG_INFO("Successfully setup SOR:");
124  sor->dump();
125 
126  return StatusCode::SUCCESS;
127 }
128 
129 
131 coral::AttributeList TrigSORFromPtreeHelper::getAttrList ATLAS_NOT_THREAD_SAFE () const
132 {
133  // First create attribute specification
134  // ugly new needed:
135  // dtor is protected, have to use ptr and release it explicitly... go figure
136  auto attrSpec = new coral::AttributeListSpecification{};
137  attrSpec->extend("RunNumber", "unsigned int");
138  attrSpec->extend("SORTime", "unsigned long long");
139  attrSpec->extend("RunType", "string");
140  attrSpec->extend("DetectorMaskFst", "unsigned long long");
141  attrSpec->extend("DetectorMaskSnd", "unsigned long long");
142  attrSpec->extend("RecordingEnabled", "bool");
143 
144  // now create the attribute list and fill it in
145  coral::AttributeList attrList(*attrSpec);
146 
147  attrList["RunNumber"].data<unsigned int>() = m_runNumber;
148  attrList["RunType"].data<std::string>() = m_rparams.get<std::string>("run_type");
149  attrList["RecordingEnabled"].data<bool>() = m_rparams.get<bool>("recording_enabled");
150 
151  attrList["SORTime"].data<unsigned long long>() = m_sorTime_ns;
152 
153  std::pair<uint64_t, uint64_t> dm = eformat::helper::DetectorMask(m_rparams.get_child("det_mask").data()).serialize();
154  attrList["DetectorMaskFst"].data<unsigned long long>() = dm.first;
155  attrList["DetectorMaskSnd"].data<unsigned long long>() = dm.second;
156 
157  // coral wants us to have to do this explicitly...
158  attrSpec->release(); // we don't delete because the dtor is private
159  // we have to hope the CORAL framework will
160 
161  return attrList;
162 }
163 
166 {
167  // set IOVRange on the IOVSvc
168  ServiceHandle<IIOVSvc> iovsvc("IOVSvc", CLNAME);
169  ATH_CHECK( iovsvc.retrieve() );
170 
171  auto clid = ClassID_traits<SOR>::ID();
172  ATH_CHECK( iovsvc->setRange(clid, m_sorpath, iovRange, "StoreGateSvc") );
173 
174  return StatusCode::SUCCESS;
175 }
176 
179 {
180  // check the SOR_Params proxy and add if necessary an IAddressProvider (typically for MC)
181  auto proxy = m_detStore->proxy(sor);
182  if (!proxy) {
183  ATH_MSG_ERROR("Could not find proxy for SOR_Params folder.");
184  return StatusCode::FAILURE;
185  }
186 
187  // check if the transient address has an IAddressProvider, if not set IOVDbSvc as provider
188  if (!proxy->provider()) {
189  // get handle to the IOVDbSvc
190  ServiceHandle<IIOVDbSvc> iovdbsvc("IOVDbSvc", CLNAME);
191  ATH_CHECK( iovdbsvc.retrieve() );
192 
193  IAddressProvider* provider = dynamic_cast<IAddressProvider*>(&*iovdbsvc);
194  if (!provider) {
195  ATH_MSG_ERROR("Could not cast to IAddressProvider interface and set the provider for SOR_Params.");
196  return StatusCode::FAILURE;
197  }
198  proxy->setProvider(provider, proxy->storeID());
199  }
200 
201  return StatusCode::SUCCESS;
202 }
203 
TrigSORFromPtreeHelper::fillSOR
StatusCode fillSOR(const EventContext &ctx) const
Fill SOR record in Detector Store, reusing if present or creating new one otherwise/ @params ctx Even...
Definition: TrigSORFromPtreeHelper.cxx:49
TrigSORFromPtreeHelper::updateProxy
StatusCode updateProxy(SOR *sor) const
Definition: TrigSORFromPtreeHelper.cxx:178
StateLessPT_NewConfig.proxy
proxy
Definition: StateLessPT_NewConfig.py:392
PowhegControl_ttHplus_NLO.ss
ss
Definition: PowhegControl_ttHplus_NLO.py:83
AthCheckMacros.h
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
TrigSORFromPtreeHelper::TrigSORFromPtreeHelper
TrigSORFromPtreeHelper(IMessageSvc *msgSvc, const ServiceHandle< StoreGateSvc > &detStore, const std::string &sorpath, const boost::property_tree::ptree &rparams)
Create the SOR helper @params msgSvc Pointer to MessageSvc @params detStore Handle to DetectorStore @...
Definition: TrigSORFromPtreeHelper.cxx:23
ATLAS_NOT_THREAD_SAFE
StatusCode TrigSORFromPtreeHelper::createSOR ATLAS_NOT_THREAD_SAFE() const
Definition: TrigSORFromPtreeHelper.cxx:91
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
python.PyKernel.AttributeList
AttributeList
Definition: PyKernel.py:36
MMMonUtils.get_time
def get_time(histo)
Definition: MMMonUtils.py:50
IIOVDbSvc.h
Abstract interface to IOVDbSvc to access IOVRange and tag information.
CondAttrListCollection
This class is a collection of AttributeLists where each one is associated with a channel number....
Definition: CondAttrListCollection.h:52
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
AthMessaging::ATLAS_THREAD_SAFE
std::atomic_flag m_initialized ATLAS_THREAD_SAFE
Messaging initialized (initMessaging)
Definition: AthMessaging.h:141
ClassID_traits::ID
static const CLID & ID()
the CLID of T
Definition: Control/AthenaKernel/AthenaKernel/ClassID_traits.h:50
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
StdJOSetup.msgSvc
msgSvc
Provide convenience handles for various services.
Definition: StdJOSetup.py:36
TrigSORFromPtreeHelper::m_sorTime_ns
unsigned long long m_sorTime_ns
Definition: TrigSORFromPtreeHelper.h:72
TrigSORFromPtreeHelper::m_detStore
ServiceHandle< StoreGateSvc > m_detStore
Definition: TrigSORFromPtreeHelper.h:68
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
IAddressProvider
interface for IOA providers
Definition: IAddressProvider.h:28
StateLessPT_NewConfig.iovdbsvc
iovdbsvc
Definition: StateLessPT_NewConfig.py:430
python.BackTrackingConfig.iovsvc
iovsvc
BackTracking Configuration ##############################################.
Definition: BackTrackingConfig.py:98
AthMessaging
Class to provide easy MsgStream access and capabilities.
Definition: AthMessaging.h:55
TrigSORFromPtreeHelper::m_sorpath
std::string m_sorpath
Definition: TrigSORFromPtreeHelper.h:69
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
ReadCellNoiseFromCool.dm
dm
Definition: ReadCellNoiseFromCool.py:235
TrigSORFromPtreeHelper::m_runNumber
unsigned int m_runNumber
Definition: TrigSORFromPtreeHelper.h:71
python.PyKernel.detStore
detStore
Definition: PyKernel.py:41
ptree
boost::property_tree::ptree ptree
Definition: JsonFileLoader.cxx:16
TrigSORFromPtreeHelper::setIOVRange
StatusCode setIOVRange(IOVRange &iovRange) const
Definition: TrigSORFromPtreeHelper.cxx:165
TrigSORFromPtreeHelper.h
TrigSORFromPtreeHelper::eventID
EventIDBase eventID() const
Create an EventIDBase filled with the value from rparams.
Definition: TrigSORFromPtreeHelper.cxx:76
IAddressProvider.h
checker_macros.h
Define macros for attributes used to control the static checker.
ServiceHandle< StoreGateSvc >