ATLAS Offline Software
OfflineHistogramProvider.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef AthenaMonitoringKernel_HistogramFiller_OfflineHistogramProvider_h
6 #define AthenaMonitoringKernel_HistogramFiller_OfflineHistogramProvider_h
7 
8 #include <memory>
9 
13 
14 #include "GaudiKernel/ContextSpecificPtr.h"
15 
16 #include "HistogramFactory.h"
17 
18 #include "TTree.h"
19 
20 // this mutex is used to protect access to the metadata trees
21 namespace {
22  static std::mutex s_metadataMutex;
23 }
24 
25 namespace Monitored {
30  public:
39  std::shared_ptr<HistogramFactory> factory,
40  const HistogramDef& histDef)
42  , m_gmTool(gmTool)
43  , m_factory(factory)
44  , m_histDef(new HistogramDef(histDef))
45  , m_objcache({0, 0, nullptr})
46  {}
47 
48  // store metadata trees on object destruction
49  virtual ~OfflineHistogramProvider() override { storeMetadata(); }
50 
60  TNamed* histogram() override {
61  const unsigned runNumber = m_gmTool->runNumber();
62  const unsigned lumiBlock = m_gmTool->lumiBlock();
63 
64  // do we have the object already?
65  std::scoped_lock lock(m_cacheMutex);
66  objcache& objcacheref = m_objcache;
67  if (objcacheref.lumiBlock == lumiBlock
68  && objcacheref.runNumber == runNumber
69  && objcacheref.object) {
70  return objcacheref.object;
71  }
72 
73  std::string lbString;
76  lbString = "";
77  } else if ( period == HistogramDef::RunPeriod::LowStat ) {
78  const unsigned lbBase = lumiBlock-(((int64_t)lumiBlock-1)%20);
79  lbString = "/lowStat_LB"+std::to_string(lbBase)+"-"+std::to_string(lbBase+19);
80  } else {
81  lbString = "/lb_"+std::to_string(lumiBlock);
82  }
83  m_histDef->tld = "/run_"+std::to_string(runNumber)+lbString+"/";
84 
85  objcacheref.lumiBlock = lumiBlock;
86  objcacheref.runNumber = runNumber;
87  objcacheref.object = m_factory->create(*m_histDef);
88  const auto fullName = m_factory->getFullName(*m_histDef);
89  if (std::find(m_storedPaths.begin(), m_storedPaths.end(), fullName) == m_storedPaths.end()) {
90  m_storedPaths.push_back(fullName);
91  }
92  return objcacheref.object;
93  }
94 
95  private:
97  std::shared_ptr<HistogramFactory> m_factory;
98  std::shared_ptr<HistogramDef> m_histDef;
99 
100  struct objcache {
101  unsigned int runNumber;
102  unsigned int lumiBlock;
103  TNamed* object;
104  };
105  mutable Gaudi::Hive::ContextSpecificData<objcache> m_objcache ATLAS_THREAD_SAFE;
107 
108  std::vector<std::string> m_storedPaths;
109 
116  void storeMetadata() const {
117  std::scoped_lock<std::mutex> metadataLock(s_metadataMutex);
118  for (const auto &path : m_storedPaths) {
119  //std::cout << "Path " << path << std::endl;
120  size_t pos = path.find_last_of('/');
121  auto splitPath = std::make_pair(path.substr(0, pos), path.substr(pos + 1));
122  std::string treePath = splitPath.first + "/metadata";
123  auto &histSvc = m_gmTool->histogramService();
124  std::string interval;
125  char triggerData[] = "<none>";
126  const std::string mergeDataStr = m_histDef->merge == "" ? "<default>" : m_histDef->merge;
127  std::vector<char> mergeData{mergeDataStr.begin(), mergeDataStr.end()};
128  mergeData.push_back('\0');
129 
132  interval = "run";
134  interval = "lowStat";
135  } else {
136  interval = "lumiBlock";
137  }
138  if (!histSvc->existsTree(treePath)) {
139  auto tree = std::make_unique<TTree>("metadata", "Monitoring Metadata");
140 
141  tree->Branch("Name", &(splitPath.second[0]), "Name/C");
142  tree->Branch("Interval", &(interval[0]), "Interval/C");
143  tree->Branch("TriggerChain", triggerData, "TriggerChain/C");
144  tree->Branch("MergeMethod", mergeData.data(), "MergeMethod/C");
145  tree->Fill();
146 
147  if (!histSvc->regTree(treePath, std::move(tree))) {
148  MsgStream log(Athena::getMessageSvc(), "OfflineHistogramProvider");
149  log << MSG::ERROR
150  << "Failed to register DQ metadata TTree " << treePath << endmsg;
151  }
152  } else {
153  TTree *tree{nullptr};
154  if (histSvc->getTree(treePath, tree).isSuccess()) {
155  tree->SetBranchAddress("Name", &(splitPath.second[0]));
156  tree->SetBranchAddress("Interval", &(interval[0]));
157  tree->SetBranchAddress("TriggerChain", triggerData);
158  tree->SetBranchAddress("MergeMethod", mergeData.data());
159  tree->Fill();
160  } else {
161  MsgStream log(Athena::getMessageSvc(), "OfflineHistogramProvider");
162  log << MSG::ERROR
163  << "Failed to retrieve DQ metadata TTree " << treePath << " which is reported to exist" << endmsg;
164  }
165  }
166  }
167  }
168  };
169 }
170 
171 #endif /* AthenaMonitoringKernel_HistogramFiller_OfflineHistogramProvider_h */
python.AtlRunQueryAMI.period
period
Definition: AtlRunQueryAMI.py:225
IHistogramProvider.h
Monitored::OfflineHistogramProvider::m_histDef
std::shared_ptr< HistogramDef > m_histDef
Definition: OfflineHistogramProvider.h:98
athena.path
path
python interpreter configuration --------------------------------------—
Definition: athena.py:128
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
Monitored::IHistogramProvider
Interface of the source of ROOT objects for HistogramFillers.
Definition: IHistogramProvider.h:14
Monitored::OfflineHistogramProvider::m_factory
std::shared_ptr< HistogramFactory > m_factory
Definition: OfflineHistogramProvider.h:97
BeamSpot::mutex
std::mutex mutex
Definition: InDetBeamSpotVertex.cxx:18
HistogramDef.h
Monitored::HistogramDef::RunPeriod::Run
@ Run
rebook histogram after each run
WriteCellNoiseToCool.fullName
fullName
Definition: WriteCellNoiseToCool.py:461
tree
TChain * tree
Definition: tile_monitor.h:30
CSV_InDetExporter.new
new
Definition: CSV_InDetExporter.py:145
GenericMonitoringTool::lumiBlock
virtual uint32_t lumiBlock()
Definition: GenericMonitoringTool.cxx:286
Monitored::OfflineHistogramProvider::objcache::object
TNamed * object
Definition: OfflineHistogramProvider.h:103
Monitored::OfflineHistogramProvider::objcache::runNumber
unsigned int runNumber
Definition: OfflineHistogramProvider.h:101
Monitored::HistogramDef
the internal class used to keep parsed Filler properties
Definition: HistogramDef.h:15
Athena::getMessageSvc
IMessageSvc * getMessageSvc(bool quiet=false)
Definition: getMessageSvc.cxx:20
Monitored::OfflineHistogramProvider::objcache::lumiBlock
unsigned int lumiBlock
Definition: OfflineHistogramProvider.h:102
Monitored::HistogramDef::RunPeriod
RunPeriod
Definition: HistogramDef.h:33
GenericMonitoringTool.h
Monitored
Generic monitoring tool for athena components.
Definition: GenericMonitoringTool.h:30
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
getLatestRuns.interval
interval
Definition: getLatestRuns.py:24
Monitored::OfflineHistogramProvider::histogram
TNamed * histogram() override
Getter of ROOT object.
Definition: OfflineHistogramProvider.h:60
Monitored::OfflineHistogramProvider::m_storedPaths
std::vector< std::string > m_storedPaths
Definition: OfflineHistogramProvider.h:108
MuonSegmentReaderConfig.histSvc
histSvc
Definition: MuonSegmentReaderConfig.py:96
Monitored::OfflineHistogramProvider::m_gmTool
GenericMonitoringTool *const m_gmTool
Definition: OfflineHistogramProvider.h:96
Monitored::OfflineHistogramProvider::m_cacheMutex
std::mutex m_cacheMutex
Definition: OfflineHistogramProvider.h:106
GenericMonitoringTool::runNumber
virtual uint32_t runNumber()
Definition: GenericMonitoringTool.cxx:282
GenericMonitoringTool
Definition: GenericMonitoringTool.py:1
Monitored::OfflineHistogramProvider::objcache
Definition: OfflineHistogramProvider.h:100
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
Monitored::OfflineHistogramProvider::storeMetadata
void storeMetadata() const
Store metadata trees.
Definition: OfflineHistogramProvider.h:116
GenericMonitoringTool::histogramService
virtual const ServiceHandle< ITHistSvc > & histogramService() const
Definition: GenericMonitoringTool.h:72
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:18
Monitored::OfflineHistogramProvider
Implementation of IHistogramProvider for offline histograms.
Definition: OfflineHistogramProvider.h:29
DeMoAtlasDataLoss.runNumber
string runNumber
Definition: DeMoAtlasDataLoss.py:64
HistogramFactory.h
Monitored::OfflineHistogramProvider::OfflineHistogramProvider
OfflineHistogramProvider(GenericMonitoringTool *const gmTool, std::shared_ptr< HistogramFactory > factory, const HistogramDef &histDef)
Constructor.
Definition: OfflineHistogramProvider.h:38
Monitored::OfflineHistogramProvider::~OfflineHistogramProvider
virtual ~OfflineHistogramProvider() override
Definition: OfflineHistogramProvider.h:49
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
Monitored::HistogramDef::RunPeriod::LowStat
@ LowStat
rebook histogram after every 20 lumiblocks
xAOD::lumiBlock
setTeId lumiBlock
Definition: L2StandAloneMuon_v1.cxx:327
Monitored::OfflineHistogramProvider::ATLAS_THREAD_SAFE
Gaudi::Hive::ContextSpecificData< objcache > m_objcache ATLAS_THREAD_SAFE
Definition: OfflineHistogramProvider.h:105