5#ifndef ManagedMonitorToolBase_CXX
6#define ManagedMonitorToolBase_CXX
19#include "TEfficiency.h"
23#include "Gaudi/Interfaces/IOptionsSvc.h"
24#include "GaudiKernel/EventContext.h"
25#include "GaudiKernel/IMessageSvc.h"
26#include "GaudiKernel/ISvcLocator.h"
27#include "GaudiKernel/MsgStream.h"
31#include "GaudiKernel/ITHistSvc.h"
178 std::string strToLower(
const std::string&
str );
194 std::string hName(
h->GetName() );
195 m_map.emplace( hName,
h );
196 return m_tool->regHist(
h, *
this );
199 return StatusCode::FAILURE;
204getHist( TH1*&
h,
const std::string& hName )
207 return m_tool->getHist(
h, hName, *
this );
210 return StatusCode::FAILURE;
216getHist( TH2*&
h,
const std::string& hName )
219 return m_tool->getHist(
h, hName, *
this );
222 return StatusCode::FAILURE;
228 return m_tool->regEfficiency( e, *
this );
230 return StatusCode::FAILURE;
239 return m_tool->regGraph( g, *
this );
242 return StatusCode::FAILURE;
251 return m_tool->regTree( t, *
this );
254 return StatusCode::FAILURE;
263 std::string hName(
h->GetName() );
264 m_map.erase( hName );
265 return m_tool->writeAndDelete(
h, *
this );
268 return StatusCode::FAILURE;
277 std::string hName(
h->GetName() );
278 m_map.erase( hName );
282 return StatusCode::FAILURE;
291 return m_tool->deregGraph( g );
294 return StatusCode::FAILURE;
303 return m_tool->deregObject( objName, *
this );
306 return StatusCode::FAILURE;
315 bool isSuccess(
true);
318 typedef HistMap_t::const_iterator MapIter_t;
319 MapIter_t mapEnd = m_map.end();
320 for( MapIter_t i = m_map.begin(); i != mapEnd; ++i ) {
321 sc = m_tool->deregHist( i->second );
322 if( !
sc.isSuccess() )
329 return StatusCode::SUCCESS;
332 return StatusCode::FAILURE;
338 const IInterface* parent )
371 declareInterface<IMonitorToolBase>(
this);
427 std::map<std::string,OutputMetadata*>::iterator mdend =
m_metadataMap.end();
428 for( std::map<std::string,OutputMetadata*>::iterator i =
m_metadataMap.begin();
441 msg(MSG::ERROR) <<
"!! streamNameFunction() has not been initialized !!" <<
endmsg;
442 msg(MSG::ERROR) <<
" --> neither ManagedMonitorToolBase::initialize() nor" <<
endmsg;
443 msg(MSG::ERROR) <<
" --> ManagedMonitorToolBase::setMonManager() has been called." <<
endmsg;
444 msg(MSG::ERROR) <<
" --> Correct configuration cannot be guaranteed from this point." <<
endmsg;
455 std::string
str(
"file");
497 std::string lcstr( strToLower(
str) );
501 else if( lcstr ==
"fill" )
503 else if( lcstr ==
"run" )
505 else if( lcstr ==
"lowStat" )
507 else if( lcstr ==
"medStat" )
509 else if( lcstr ==
"higStat" )
511 else if( lcstr ==
"lumiBlock" )
513 else if( lcstr ==
"eventsBlock" )
515 else if( lcstr ==
"file" )
521 MsgStream log( ms,
"ManagedMonitorToolBase::intervalStringToEnum()" );
522 log << MSG::WARNING <<
"Unknown ManagedMonitorToolBase::Interval_t \""
523 <<
str <<
"\", returning \"file\"" <<
endmsg;
535 m_d->m_warnAboutMissingInitialize =
false;
537 StatusCode
sc = AthAlgTool::initialize();
538 if( !
sc.isSuccess() )
539 return StatusCode::FAILURE;
546 if( !
sc.isSuccess() ) {
547 msg(MSG::ERROR) <<
"!! Unable to locate the THistSvc service !!" <<
endmsg;
554 if( !
sc.isSuccess() ) {
555 msg(MSG::ERROR) <<
"!! Unable to retrieve the TrigDecisionTool !!" <<
endmsg;
562 if ( !
sc.isSuccess() ) {
571 if(!
sc.isSuccess()) {
572 msg(MSG::WARNING) <<
"Error parsing the trigger chain list, using empty list" <<
endmsg;
585 if(!
sc.isSuccess()) {
586 msg(MSG::WARNING) <<
"Error parsing the trigger group names list, using empty list" <<
endmsg;
602 if( !
sc.isSuccess() ) {
614 auto getProp = [
this,&joSvc](std::string& var,
const std::string& name) {
615 if (joSvc->has(name))
616 var = joSvc->get(name);
620 getProp(
m_fileKey, client +
".FileKey");
624 ATH_MSG_DEBUG(
" * Properties set from " << client <<
" to the values:\n"
641 return StatusCode::SUCCESS;
651 return StatusCode::SUCCESS;
660 if (
m_d->m_warnAboutMissingInitialize) {
661 m_d->m_warnAboutMissingInitialize =
false;
662 msg(MSG::WARNING) <<
"ManagedMonitorToolBase::initialize() never called from reimplementation!" <<
endmsg;
685 isNewEventsBlock =
true;
694 int currentLB =
m_manager->lumiBlockNumber();
695 int LBsLowStat =
m_manager->getLBsLowStat();
696 int LBsMedStat =
m_manager->getLBsMedStat();
697 int LBsHigStat =
m_manager->getLBsHigStat();
699 if( LBsLowStat*LBsMedStat*LBsHigStat == 0) {
700 msg(MSG::WARNING) <<
"zero LBs requested for interval" <<
endmsg;
714 StatusCode sc0( StatusCode::SUCCESS );
715 StatusCode sc1( StatusCode::SUCCESS );
716 StatusCode sc2( StatusCode::SUCCESS );
717 StatusCode sc3( StatusCode::SUCCESS );
733 m_d->benchPreProcHistograms();
735 m_d->benchPostProcHistograms();
738 m_d->benchPreBookHistograms();
744 std::vector<Interval_t> intervals_to_process;
749 for (
const auto interval: intervals_to_process) {
761 it.m_templateHist->LabelsInflate(
"X");
768 TH1* passedHist = it.m_templateHist->GetCopyPassedHisto();
769 TH1* totalHist = it.m_templateHist->GetCopyTotalHisto();
772 passedHist->LabelsInflate(
"X");
773 totalHist->LabelsInflate(
"X");
778 it.m_templateHist->SetPassedHistogram(*passedHist,
"f");
779 it.m_templateHist->SetTotalHistogram(*totalHist,
" ");
787 streamname->updateRunLB();
792 m_d->benchPostBookHistograms();
797 bool filterresult(
true);
800 for (; filterresult && (ifilter != filterend);
802 filterresult = (filterresult && (*ifilter)->accept(ctx));
813 m_d->benchPreFillHistograms();
816 m_d->benchPostFillHistograms();
829 int LBsLowStat =
m_manager->getLBsLowStat();
830 int LBsMedStat =
m_manager->getLBsMedStat();
831 int LBsHigStat =
m_manager->getLBsHigStat();
832 if( LBsLowStat*LBsMedStat*LBsHigStat > 0) {
840 return StatusCode::SUCCESS;
849 std::string mdStreamName( streamName );
850 size_t found=mdStreamName.rfind(
'/');
852 if ( found != std::string::npos )
853 mdStreamName.replace( found, mdStreamName.length(),
"/metadata" );
857 metadata =
new TTree(
"metadata",
"Monitoring Metadata" );
858 if (! metadata)
return StatusCode::FAILURE;
859 StatusCode scmd =
m_THistSvc->regTree( mdStreamName, metadata );
860 if (scmd == StatusCode::FAILURE)
return StatusCode::FAILURE;
864 i->second->fill( hName, group.interval(), group.chain(), group.merge() );
866 return StatusCode::SUCCESS;
891 TH1* theHist = (*it).m_templateHist;
894 TH1*
h =
static_cast<TH1*
>(theHist->Clone());
898 std::string hName =
h->GetName();
904 StatusCode sc1 =
m_THistSvc->deReg( theHist );
905 if (sc1 == StatusCode::FAILURE) allIsOk =
false;
908 StatusCode sc2 =
m_THistSvc->regHist( streamName,
h );
909 if (sc2 == StatusCode::FAILURE) allIsOk =
false;
914 m_manager->writeAndDelete( genericName );
921 if (smd != StatusCode::SUCCESS) allIsOk =
false;
924 StatusCode sc3 =
m_THistSvc->regHist( streamName, theHist );
925 if (sc3 == StatusCode::FAILURE) allIsOk =
false;
929 if (!allIsOk)
return StatusCode::FAILURE;
931 return StatusCode::SUCCESS;
948 bool graphRemoved =
false;
951 TDirectory* dir =
file->GetDirectory(directoryName.c_str());
954 TObject* obj = dir->Remove(theGraph);
960 return StatusCode::FAILURE;
963 return StatusCode::SUCCESS;
977 TGraph* theGraph = (*it).m_templateHist;
980 TGraph* g =
static_cast<TGraph*
>(theGraph->Clone());
984 std::string gName = g->GetName();
990 StatusCode sc1 =
m_THistSvc->deReg( theGraph );
991 if (sc1 == StatusCode::FAILURE)
996 bool doneCleaning =
false;
998 TSeqCollection *filelist=gROOT->GetListOfFiles();
999 for (
int i=0; i<filelist->GetEntries(); i++) {
1000 ATH_MSG_DEBUG(
"List of files: " << filelist->At(i)->GetName());
1001 TFile*
file =
static_cast<TFile*
>(filelist->At(i));
1003 if (sc2 == StatusCode::SUCCESS)
1004 doneCleaning =
true;
1008 if (!doneCleaning) {
1009 ATH_MSG_ERROR(
"THistSvc_deReg_fixTGraph: failed to apply TGraph fix for the THist Svc!");
1015 StatusCode sc3 =
m_THistSvc->regGraph( streamName, g );
1016 if (sc3 == StatusCode::FAILURE)
1023 if (smd != StatusCode::SUCCESS)
1027 StatusCode sc4 =
m_THistSvc->regGraph( streamName, theGraph );
1028 if (sc4 == StatusCode::FAILURE)
1033 if (!allIsOk)
return StatusCode::FAILURE;
1035 return StatusCode::SUCCESS;
1039 bool allIsOk =
true;
1040 for(
auto& it : templateEfficiencies ) {
1043 TEfficiency* theEfficiency = it.m_templateHist;
1044 TEfficiency* e =
static_cast<TEfficiency*
>(theEfficiency->Clone());
1045 int nbins = theEfficiency->GetTotalHistogram()->GetNbinsX();
1046 int xlow = theEfficiency->GetTotalHistogram()->GetXaxis()->GetXmin();
1047 int xhigh = theEfficiency->GetTotalHistogram()->GetXaxis()->GetXmax();
1048 e->SetBins(nbins,xlow,xhigh);
1049 std::string name = e->GetName();
1052 TGraph* theGraph =
reinterpret_cast<TGraph*
>(theEfficiency);
1053 TGraph* g =
reinterpret_cast<TGraph*
>(e);
1060 StatusCode sc1 =
m_THistSvc->deReg( theGraph );
1061 if (sc1 == StatusCode::FAILURE) allIsOk =
false;
1063 bool doneCleaning =
false;
1065 TSeqCollection *filelist=gROOT->GetListOfFiles();
1066 for (
int i=0; i<filelist->GetEntries(); i++) {
1067 ATH_MSG_DEBUG(
"List of files: " << filelist->At(i)->GetName());
1068 TFile*
file =
static_cast<TFile*
>(filelist->At(i));
1070 if (sc2 == StatusCode::SUCCESS) doneCleaning =
true;
1073 if (!doneCleaning) {
1074 ATH_MSG_ERROR(
"THistSvc_deReg_fixTGraph: failed to apply TGraph fix for the THist Svc!");
1078 StatusCode sc3 =
m_THistSvc->regGraph( streamName, g );
1079 if (sc3 == StatusCode::FAILURE)
1086 if (smd != StatusCode::SUCCESS) allIsOk =
false;
1088 StatusCode sc4 =
m_THistSvc->regGraph( streamName, theGraph );
1089 if (sc4 == StatusCode::FAILURE) allIsOk =
false;
1092 if (!allIsOk)
return StatusCode::FAILURE;
1093 return StatusCode::SUCCESS;
1102 bool allIsOk =
true;
1108 TTree* theTree = (*it).m_templateHist;
1111 TTree* t =
static_cast<TTree*
>(theTree->Clone());
1115 std::string name = t->GetName();
1121 StatusCode sc1 =
m_THistSvc->deReg( theTree );
1122 if (sc1 == StatusCode::FAILURE) allIsOk =
false;
1125 StatusCode sc2 =
m_THistSvc->regTree( streamName, t );
1126 if (sc2 == StatusCode::FAILURE) allIsOk =
false;
1130 m_manager->passOwnership( t, genericName );
1131 m_manager->writeAndDelete( genericName );
1138 if (smd != StatusCode::SUCCESS) allIsOk =
false;
1141 StatusCode sc3 =
m_THistSvc->regTree( streamName, theTree );
1142 if (sc3 == StatusCode::FAILURE) allIsOk =
false;
1146 if (!allIsOk)
return StatusCode::FAILURE;
1148 return StatusCode::SUCCESS;
1160 m_d->benchPreProcHistograms();
1171 m_d->benchPostProcHistograms();
1174 return StatusCode::SUCCESS;
1186 return StatusCode::SUCCESS;
1195 return StatusCode::SUCCESS;
1202 return StatusCode::SUCCESS;
1215 return StatusCode::SUCCESS;
1239regHist( TH1*
h,
const std::string& system,
1242 MonGroup group(
this, system, interval, histo_mgmt, chain,
merge );
1251 return StatusCode::FAILURE;
1263 if (group.histo_mgmt() ==
ATTRIB_X_VS_LB && group.merge().empty()) {
1264 ATH_MSG_WARNING(
"HEY! You're attempting to register " <<
h->GetName() <<
" as a per-LB histogram, but you're not setting the merge algorithm! This is a SUPER-BAD idea! Use \"merge\", at least.");
1271 return StatusCode::FAILURE;
1274 std::string hName =
h->GetName();
1282 std::string hName =
h->GetName();
1286 m_manager->writeAndDelete( genericName );
1293 if (smd != StatusCode::SUCCESS)
return StatusCode::FAILURE;
1301getHist( TH1*&
h,
const std::string& hName,
const std::string& system,
1304 MonGroup group(
this, system, interval );
1320getHist( TH2*&
h,
const std::string& hName,
const std::string& system,
1323 MonGroup group(
this, system, interval );
1339 return StatusCode::FAILURE;
1341 TGraph* g =
reinterpret_cast<TGraph*
>(e);
1342 std::string name = e->GetName();
1347 if (group.histo_mgmt() ==
ATTRIB_X_VS_LB && group.merge().empty()) {
1348 ATH_MSG_WARNING(
"HEY! Attempting to register "<<name<<
" as a per-LB histogram, but not setting the merge algorithm! Use \"merge\", at least.");
1355 return StatusCode::FAILURE;
1361 return m_THistSvc->regGraph( streamName, g );
1366 m_manager->writeAndDelete( genericName );
1367 m_manager->passOwnership( e, genericName );
1372 if (smd != StatusCode::SUCCESS)
1373 return StatusCode::FAILURE;
1375 return m_THistSvc->regGraph( streamName, g );
1382regGraph( TGraph* g,
const std::string& system,
1385 MonGroup group(
this, system, interval, histo_mgmt, chain,
merge );
1395 return StatusCode::FAILURE;
1408 return StatusCode::FAILURE;
1411 std::string name = g->GetName();
1414 return m_THistSvc->regGraph( streamName, g );
1418 std::string gName = g->GetName();
1422 m_manager->writeAndDelete( genericName );
1423 m_manager->passOwnership( g, genericName );
1429 if (smd != StatusCode::SUCCESS)
return StatusCode::FAILURE;
1431 return m_THistSvc->regGraph( streamName, g );
1437regTree( TTree* t,
const std::string& system,
1440 MonGroup group(
this, system, interval, histo_mgmt, chain,
merge );
1461 return StatusCode::FAILURE;
1464 std::string name = t->GetName();
1473 std::string tName = t->GetName();
1477 m_manager->writeAndDelete( genericName );
1478 m_manager->passOwnership( t, genericName );
1484 if (smd != StatusCode::SUCCESS)
return StatusCode::FAILURE;
1494 return StatusCode::FAILURE;
1496 std::string hName =
h->GetName();
1500 m_manager->writeAndDelete( genericName );
1502 return StatusCode::SUCCESS;
1524deregObject(
const std::string& objName,
const std::string& system,
1527 MonGroup group(
this, system, interval );
1548 return StatusCode::SUCCESS;
1556 return StatusCode::SUCCESS;
1566 return StatusCode::SUCCESS;
1588 return lumi->lbAverageInteractionsPerCrossing();
1590 ATH_MSG_DEBUG(
"Warning: lbAverageInteractionsPerCrossing() - luminosity tools are not retrieved or turned on (i.e. EnableLumi = False)");
1603 float muToLumi = lumi->muToLumi();
1605 return lumi->lbLuminosityPerBCIDVector().at (ctx.eventID().bunch_crossing_id()) / muToLumi;
1609 ATH_MSG_DEBUG(
"Warning: lbInteractionsPerCrossing() - luminosity tools are not retrieved or turned on (i.e. EnableLumi = False)");
1622 return lumi->lbAverageLuminosity();
1624 ATH_MSG_DEBUG(
"Warning: lbAverageLuminosity() - luminosity tools are not retrieved or turned on (i.e. EnableLumi = False)");
1637 return lumi->lbLuminosityPerBCIDVector().at (ctx.eventID().bunch_crossing_id());
1639 ATH_MSG_DEBUG(
"Warning: lbLuminosityPerBCID() - luminosity tools are not retrieved or turned on (i.e. EnableLumi = False)");
1656 return live->lbAverageLiveFraction();
1658 ATH_MSG_DEBUG(
"Warning: lbAverageLivefraction() - luminosity not availble (i.e. EnableLumi = False)");
1674 return live->l1LiveFractionVector().at (ctx.eventID().bunch_crossing_id());
1676 ATH_MSG_DEBUG(
"Warning: livefractionPerBCID() - luminosity retrieved available (i.e. EnableLumi = False)");
1690 ATH_MSG_DEBUG(
"Warning: lbLumiWeight() - luminosity tools are not retrieved or turned on (i.e. EnableLumi = False)");
1707 return dur->lbDuration();
1709 ATH_MSG_DEBUG(
"Warning: lbDuration() - luminosity tools are not retrieved or turned on (i.e. EnableLumi = False)");
1752fill(
const std::string& name,
1754 std::string trigger,
1761 if( trigger.empty() )
1764 merge =
"<default>";
1776copyString(
char* to,
const std::string& from )
1779 const char* f = from.c_str();
1791 std::ostringstream streamName;
1795 streamName << group.system() <<
"/" << objName;
1796 return streamName.str();
1803 std::string streamName =
getStreamName(tool, group, objName, usePreviousInterval);
1804 std::string root, rem;
1808 rem.erase(rem.rfind(
'/'), rem.length());
1817 std::ostringstream streamName;
1818 streamName << group.system() <<
"/" << objName;
1819 return streamName.str();
1827 return getStreamName(tool, group, objName, usePreviousInterval);
1834 std::ostringstream streamName;
1838 streamName << group.system() <<
"/" << objName;
1839 return streamName.str();
1846 return getStreamName(tool, group, objName, usePreviousInterval);
1854 *end = *start +
length - 1;
1862 std::ostringstream streamName;
1864 bool isTemp =
false;
1866 bool useRunFolders = group.interval() !=
all;
1868 bool useLBFolders = ( useRunFolders )
1871 bool useLowStatInterval = ( useRunFolders )
1874 bool useMedStatInterval = ( useRunFolders )
1877 bool useHigStatInterval = ( useRunFolders )
1886 if( useRunFolders ) {
1894 if( useLBFolders ) {
1898 streamName <<
"lb_" << currentLB <<
"/";
1900 else if( useLowStatInterval ) {
1906 streamName <<
"lowStat_LB" << start <<
"-" << end <<
"/";
1908 else if( useMedStatInterval ) {
1911 streamName <<
"medStat_LB" << start <<
"-" << end <<
"/";
1913 else if( useHigStatInterval ) {
1916 streamName <<
"higStat_LB" << start <<
"-" << end <<
"/";
1918 else if( useEBFolders ) {
1923 long eventsBlockNumber = 1;
1924 long procNEventsProp = tool->get_procNEventsProp();
1925 unsigned int nEvents = tool->get_nEvents();
1926 if (procNEventsProp > 0) {
1927 eventsBlockNumber = (long)
nEvents / procNEventsProp;
1928 if ((
nEvents % procNEventsProp) != 0)
1929 eventsBlockNumber++;
1933 if (usePreviousInterval) {
1934 eventsBlockNumber--;
1937 streamName <<
"eb_" << eventsBlockNumber <<
"/";
1940 streamName << group.system() <<
"/" << objName;
1942 return streamName.str();
1949 std::string streamName =
getStreamName(tool, group, objName, usePreviousInterval);
1950 std::string root, rem;
1954 rem.erase(rem.rfind(
'/'), rem.length());
1969 ATH_MSG_DEBUG(
"ManagedMonitorToolBase::trigChainsArePassed:");
1971 for(
unsigned int i=0; i<vTrigNames.size(); i++) {
1973 ATH_MSG_DEBUG(
" + \"" << vTrigNames[i] <<
"\" passed, returning \'true\'");
1977 ATH_MSG_DEBUG(
" - \"" << vTrigNames[i] <<
"\" did not pass");
1986parseList(
const std::string& line, std::vector<std::string>& result) {
1988 std::stringstream
ss(line);
1990 if (
msgLvl(MSG::DEBUG))
msg(MSG::DEBUG) <<
"ManagedMonitorToolBase::parseList:";
1992 while ( std::getline(
ss, item,
',') ) {
1993 std::stringstream iss(item);
1995 if (
msgLvl(MSG::DEBUG))
msg(MSG::DEBUG) <<
" " << item;
1996 result.push_back(item);
2000 return StatusCode::SUCCESS;
2006 for (
size_t i = 0; i < vTrigChainNames.size(); ++i) {
2007 std::string& thisName = vTrigChainNames[i];
2008 if (thisName.compare(0, 9,
"CATEGORY_") ==0) {
2009 ATH_MSG_DEBUG(
"Found a trigger category: " << thisName <<
". We will unpack it.");
2010 std::vector<std::string> triggers =
m_trigTranslator->translate(thisName.substr(9,std::string::npos));
2011 std::ostringstream oss;
2013 for (
size_t itrig = 0; itrig < triggers.size(); ++itrig) {
2017 oss << triggers[itrig];
2021 std::string newval = oss.str();
2023 vTrigChainNames[i] = std::move(newval);
2058parseString(
const std::string& streamName, std::string& root, std::string& rem) {
2059 std::string::size_type pos = streamName.find(
'/');
2061 if (pos == std::string::npos) {
2068 parseString(streamName.substr(1,streamName.length()),root,rem);
2070 root = streamName.substr(0,pos);
2071 rem = streamName.substr(pos+1,streamName.length());
2098 std::string strToLower(
const std::string&
str )
2100 std::string lstr(
str);
2101 std::string::const_iterator from =
str.begin();
2102 std::string::const_iterator strend =
str.end();
2103 std::string::iterator to = lstr.begin();
2104 while( from != strend ) {
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_ERROR(x,...)
#define ATH_MSG_WARNING(x,...)
void tolower(std::string &s)
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
bool msgLvl(const MSG::Level lvl) const
Header file for AthHistogramAlgorithm.
static const MSG::Level s_resourceMonThreshold
An Algorithm that manages a set of modules, each inheriting from ManagedMonitorToolBase,...
static unsigned int getLBsLowStat()
static Environment_t envStringToEnum(const std::string &str)
Converts a string to an Environment_t of the same name.
static DataType_t dataTypeStringToEnum(const std::string &str)
Converts a string to a DataType_t of the same name.
static unsigned int getLBsHigStat()
static unsigned int getLBsMedStat()
static unsigned int lumiBlockNumber()
static unsigned int runNumber()