ATLAS Offline Software
AthAnalysisAlgorithm.cxx
Go to the documentation of this file.
1 
3 /*
4  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
5 */
6 
7 // AthAnalysisAlgorithm.cxx
8 // Implementation file for class AthAnalysisAlgorithm
9 // Exactly like an AthAlgorithm except also has Metadata accessors
10 // and beginInputFile method
11 // Author: W.Buttinger<will@cern.ch>
13 
15 
16 #include "TROOT.h"
17 #include "TObjString.h"
18 
20  : AthAnalysisAlgorithm( name, Gaudi::svcLocator() )
21 {
22  //we assume this constructor is used outside of the framework
23  //therefore we must increment the ref count so that
24  //any SmartIF doesn't "release" the alg and therefore delete it
25  addRef();
26 }
27 
28 AthAnalysisAlgorithm::AthAnalysisAlgorithm( const std::string& name, ISvcLocator* pSvcLocator )
29  : AthHistogramAlgorithm(name,pSvcLocator)
30  , m_inputMetaStore( "StoreGateSvc/InputMetaDataStore", name )
31  , m_outputMetaStore( "StoreGateSvc/MetaDataStore", name )
32 {
33 
34  //declare an update handler for the EvtStore property, to reset the ServiceHandle
35  for(auto prop : getProperties()) {
36  if(prop->name() != "EvtStore") continue;
37  prop->declareUpdateHandler(&AthAnalysisAlgorithm::updateEvtStore, this );
38  break;
39  }
40 
41 }
42 
44 
45 void AthAnalysisAlgorithm::updateEvtStore(Gaudi::Details::PropertyBase& prop) {
46  evtStore().release().ignore();
47  evtStore().setTypeAndName(prop.toString());
48 }
49 
51 
52  // Connect to the IncidentSvc:
53  ServiceHandle< IIncidentSvc > incSvc( "IncidentSvc", name() );
54  ATH_CHECK( incSvc.retrieve() );
55 
56  // Set up the right callbacks: //but ensure we don't double-register if sysInitialize called twice (appears to be the case)
57  incSvc->removeListener( this, IncidentType::BeginInputFile );
58  incSvc->addListener( this, IncidentType::BeginInputFile, 0, true );
59  incSvc->removeListener( this, IncidentType::EndInputFile );
60  incSvc->addListener( this, IncidentType::EndInputFile, 0, true );
61  incSvc->removeListener( this, "MetaDataStop" );
62  incSvc->addListener( this, "MetaDataStop", 0, true );
63 
64 
65  // Let the base class do its thing:
67 
68  // Return gracefully:
69  return StatusCode::SUCCESS;
70 }
71 
72 StatusCode AthAnalysisAlgorithm::sysExecute(const EventContext& ctx) {
73  if(!m_doneFirstEvent) {
74  m_doneFirstEvent=true;
75  if( firstExecute().isFailure() ) {
76  ATH_MSG_FATAL("Failure in firstEvent method");
77  return StatusCode::FAILURE;
78  }
79  }
80  return AthHistogramAlgorithm::sysExecute(ctx);
81 }
82 
83 void AthAnalysisAlgorithm::handle( const Incident& inc ) {
84 
85  // Tell the user what's happening:
86  ATH_MSG_VERBOSE( "Callback received with incident: " << inc.type() );
87 
88  // Call the appropriate member function:
89  if( inc.type() == IncidentType::BeginInputFile ) {
90  m_currentFile=0;
91  if( beginInputFile().isFailure() ) {
92  ATH_MSG_FATAL( "Failed to call beginInputFile()" );
93  throw std::runtime_error( "Couldn't call beginInputFile()" );
94  }
95  } else if(inc.type() == IncidentType::EndInputFile ) {
96  if( endInputFile().isFailure() ) {
97  ATH_MSG_FATAL( "Failed to call endInputFile()" );
98  throw std::runtime_error( "Couldn't call endInputFile()" );
99  }
100  } else if(inc.type() == "MetaDataStop" ) {
101  if( metaDataStop().isFailure() ) {
102  ATH_MSG_FATAL( "Failed to call metaDataStop()" );
103  throw std::runtime_error( "Couldn't call metaDataStop()" );
104  }
105  } else {
106  ATH_MSG_WARNING( "Unknown incident type received: " << inc.type() );
107  }
108 
109  return;
110 }
111 
115 
116  // Return gracefully:
117  return StatusCode::SUCCESS;
118 }
119 
123 
124  // Return gracefully:
125  return StatusCode::SUCCESS;
126 }
127 
131 
132  // Return gracefully:
133  return StatusCode::SUCCESS;
134 }
135 
136 
140 
141  // Return gracefully:
142  return StatusCode::SUCCESS;
143 }
144 
145 
146 
147 TFile* AthAnalysisAlgorithm::currentFile(const char* evtSelName) {
148  if(m_currentFile) return m_currentFile;
149 
150  //get the EventSelector so we can get it's list of input files
151  //dont get it with a ServiceHandle, because that invokes initialize, can get into init loop
152 
153  SmartIF<IProperty> evtSelector{service(evtSelName, false)};
154  if(!evtSelector) {
155  ATH_MSG_ERROR("currentFile(): Couldn't find the service: " << evtSelName);return 0;
156  }
157 
158  try {
159  //get the list of input files - use this to determine which open file is the current input file
160  const StringArrayProperty& inputCollectionsName = dynamic_cast<const StringArrayProperty&>(evtSelector->getProperty("InputCollections"));
161 
162  ATH_MSG_VERBOSE("nOpenFile=" << gROOT->GetListOfFiles()->GetSize() << ". nFilesInInputCollection=" << inputCollectionsName.value().size());
163  if(msgLvl(MSG::VERBOSE)) {
164  for(int i=0;i<gROOT->GetListOfFiles()->GetSize();i++) {
165  ATH_MSG_VERBOSE("Open file: " << gROOT->GetListOfFiles()->At(i)->GetName());
166  }
167  }
168 
169  //look through list of files and find the one from the input collection that is currently open
170 
171  for(int i=0;i<gROOT->GetListOfFiles()->GetSize();i++) {
172  TFile *g = (TFile*)gROOT->GetListOfFiles()->At(i);
173  //see if this file is in the input file list
174  //strip everything except stuff either side of last /
175  TString s(g->GetName());
176  TObjArray* tokens = s.Tokenize("/");
177  TObjString* lastToken = dynamic_cast<TObjString*>(tokens->Last());
178  TString sToCompare("");
179  bool shortComparison(false);
180  if(tokens->GetEntries()>1) {
181  TString beforeSlash((dynamic_cast<TObjString*>(tokens->At(tokens->GetEntries()-2)))->GetString());
182  if(beforeSlash.Length()>0) sToCompare += beforeSlash;
183  sToCompare += "/";
184  } else {
185  shortComparison=true;
186  }
187  sToCompare += lastToken->GetString();
188  TString sToCompare_short(lastToken->GetString()); //short versions search
189  delete tokens;
190 
191  for(unsigned int j=0;j<inputCollectionsName.value().size();j++) {
192  TString t(inputCollectionsName.value()[j].c_str());
193  //try perfect match first
194  if(s.EqualTo(t)) {
195  ATH_MSG_VERBOSE("Current File is: " << inputCollectionsName.value()[j]);
196  m_currentFile = g;
197  return g;
198  }
199  TObjArray* tokens = t.Tokenize("/");
200  TObjString* lastToken = dynamic_cast<TObjString*>(tokens->Last());
201  TString tToCompare = "";
202  bool shortComparison2(false);
203  if(tokens->GetEntries()>1) {
204  TString beforeSlash((dynamic_cast<TObjString*>(tokens->At(tokens->GetEntries()-2)))->GetString());
205  if(beforeSlash.Length()>0) tToCompare += beforeSlash;
206  tToCompare += "/";
207  } else {
208  shortComparison2=true;
209  }
210  tToCompare += lastToken->GetString();
211  TString tToCompare_short(lastToken->GetString());
212  delete tokens;
213 
214  if(shortComparison || shortComparison2) { //doing short version search, no directories to distinguish files!
215  if(sToCompare_short.EqualTo(tToCompare_short)) {
216  ATH_MSG_VERBOSE("Current File is: " << inputCollectionsName.value()[j]);
217  m_currentFile = g;
218  return g;
219  }
220  } else
221  if(sToCompare.EqualTo(tToCompare)) {
222  ATH_MSG_VERBOSE("Current File is: " << inputCollectionsName.value()[j]);
224  return g;
225  }
226  }
227  }
228 
229  } catch(...) {
230  ATH_MSG_ERROR("currentFile(): Couldn't load InputCollections property of " << evtSelName); return 0;
231  }
232 
233  ATH_MSG_ERROR("currentFile(): Could not find the current file!");
234  return 0; //something went wrong :-(
235 
236 }
AthAnalysisAlgorithm::m_currentFile
TFile * m_currentFile
Definition: AthAnalysisAlgorithm.h:120
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
AthAnalysisAlgorithm
Definition: AthAnalysisAlgorithm.h:34
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
AthAnalysisAlgorithm::endInputFile
virtual StatusCode endInputFile()
Function called as an input file is being closed.
Definition: AthAnalysisAlgorithm.cxx:122
AthAnalysisAlgorithm::firstExecute
virtual StatusCode firstExecute()
Function called when first execute is encountered user can read event information with evtStore()
Definition: AthAnalysisAlgorithm.cxx:139
AthAnalysisAlgorithm::sysInitialize
virtual StatusCode sysInitialize() override
Function initialising the tool in the correct way in Athena.
Definition: AthAnalysisAlgorithm.cxx:50
AthAnalysisAlgorithm::~AthAnalysisAlgorithm
virtual ~AthAnalysisAlgorithm() override
Definition: AthAnalysisAlgorithm.cxx:43
AthAnalysisAlgorithm::updateEvtStore
void updateEvtStore(Gaudi::Details::PropertyBase &prop)
Definition: AthAnalysisAlgorithm.cxx:45
AthCommonMsg< Algorithm >::msgLvl
bool msgLvl(const MSG::Level lvl) const
Definition: AthCommonMsg.h:30
AthAnalysisAlgorithm::handle
virtual void handle(const Incident &inc) override
Function receiving incidents from IncidentSvc/TEvent Experts can override but they should ensure they...
Definition: AthAnalysisAlgorithm.cxx:83
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
beamspotman.tokens
tokens
Definition: beamspotman.py:1284
AthAnalysisAlgorithm::AthAnalysisAlgorithm
AthAnalysisAlgorithm(const std::string &name)
Constructor taking just a name.
Definition: AthAnalysisAlgorithm.cxx:19
AthAnalysisAlgorithm::m_doneFirstEvent
bool m_doneFirstEvent
Definition: AthAnalysisAlgorithm.h:122
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
AthCommonDataStore< AthCommonMsg< Algorithm > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
lumiFormat.i
int i
Definition: lumiFormat.py:85
python.CaloCondTools.g
g
Definition: CaloCondTools.py:15
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
AthAnalysisAlgorithm.h
AthAnalysisAlgorithm::metaDataStop
virtual StatusCode metaDataStop()
Function called before finalize user can read output metadata from outputMetaStore()
Definition: AthAnalysisAlgorithm.cxx:130
AthHistogramAlgorithm
Definition: AthHistogramAlgorithm.h:32
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
AthHistogramAlgorithm::sysInitialize
virtual StatusCode sysInitialize()
Initialization method invoked by the framework.
Definition: AthHistogramAlgorithm.cxx:75
AthAnalysisAlgorithm::beginInputFile
virtual StatusCode beginInputFile()
Function called when a new input file is opened user can read input metadata from inputMetaStore()
Definition: AthAnalysisAlgorithm.cxx:114
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
Gaudi
=============================================================================
Definition: CaloGPUClusterAndCellDataMonitorOptions.h:273
AthAnalysisAlgorithm::currentFile
virtual TFile * currentFile(const char *evtSelName="EventSelector") final
Function returning the TFile pointer of the currently open file of the given EventSelector (in athena...
Definition: AthAnalysisAlgorithm.cxx:147
python.Constants.VERBOSE
int VERBOSE
Definition: Control/AthenaCommon/python/Constants.py:14
ServiceHandle< IIncidentSvc >
AthAnalysisAlgorithm::sysExecute
virtual StatusCode sysExecute(const EventContext &) override
override to do firstEvent method
Definition: AthAnalysisAlgorithm.cxx:72