ATLAS Offline Software
AthAnalysisAlgorithm.cxx
Go to the documentation of this file.
1 
3 /*
4  Copyright (C) 2002-2022 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  IProperty* evtSelector = 0;
154  if(service(evtSelName,evtSelector,false).isFailure()) {
155  ATH_MSG_ERROR("currentFile(): Couldn't find the service: " << evtSelName);return 0;
156  }
157  //SmartIF<IProperty> evtSelector(mysel);
158  /*
159  ServiceHandle<IProperty> evtSelector(evtSelName,name());
160 
161  if(evtSelector.retrieve().isFailure()) {
162  ATH_MSG_ERROR("currentFile(): Couldn't find the service: " << evtSelName);return 0;
163  }*/
164 
165  try {
166  //get the list of input files - use this to determine which open file is the current input file
167  const StringArrayProperty& inputCollectionsName = dynamic_cast<const StringArrayProperty&>(evtSelector->getProperty("InputCollections"));
168 
169  ATH_MSG_VERBOSE("nOpenFile=" << gROOT->GetListOfFiles()->GetSize() << ". nFilesInInputCollection=" << inputCollectionsName.value().size());
170  if(msgLvl(MSG::VERBOSE)) {
171  for(int i=0;i<gROOT->GetListOfFiles()->GetSize();i++) {
172  ATH_MSG_VERBOSE("Open file: " << gROOT->GetListOfFiles()->At(i)->GetName());
173  }
174  }
175 
176  //look through list of files and find the one from the input collection that is currently open
177 
178  for(int i=0;i<gROOT->GetListOfFiles()->GetSize();i++) {
179  TFile *g = (TFile*)gROOT->GetListOfFiles()->At(i);
180  //see if this file is in the input file list
181  //strip everything except stuff either side of last /
182  TString s(g->GetName());
183  TObjArray* tokens = s.Tokenize("/");
184  TObjString* lastToken = dynamic_cast<TObjString*>(tokens->Last());
185  TString sToCompare("");
186  bool shortComparison(false);
187  if(tokens->GetEntries()>1) {
188  TString beforeSlash((dynamic_cast<TObjString*>(tokens->At(tokens->GetEntries()-2)))->GetString());
189  if(beforeSlash.Length()>0) sToCompare += beforeSlash;
190  sToCompare += "/";
191  } else {
192  shortComparison=true;
193  }
194  sToCompare += lastToken->GetString();
195  TString sToCompare_short(lastToken->GetString()); //short versions search
196  delete tokens;
197 
198  for(unsigned int j=0;j<inputCollectionsName.value().size();j++) {
199  TString t(inputCollectionsName.value()[j].c_str());
200  //try perfect match first
201  if(s.EqualTo(t)) {
202  ATH_MSG_VERBOSE("Current File is: " << inputCollectionsName.value()[j]);
203  m_currentFile = g;
204  return g;
205  }
206  TObjArray* tokens = t.Tokenize("/");
207  TObjString* lastToken = dynamic_cast<TObjString*>(tokens->Last());
208  TString tToCompare = "";
209  bool shortComparison2(false);
210  if(tokens->GetEntries()>1) {
211  TString beforeSlash((dynamic_cast<TObjString*>(tokens->At(tokens->GetEntries()-2)))->GetString());
212  if(beforeSlash.Length()>0) tToCompare += beforeSlash;
213  tToCompare += "/";
214  } else {
215  shortComparison2=true;
216  }
217  tToCompare += lastToken->GetString();
218  TString tToCompare_short(lastToken->GetString());
219  delete tokens;
220 
221  if(shortComparison || shortComparison2) { //doing short version search, no directories to distinguish files!
222  if(sToCompare_short.EqualTo(tToCompare_short)) {
223  ATH_MSG_VERBOSE("Current File is: " << inputCollectionsName.value()[j]);
224  m_currentFile = g;
225  return g;
226  }
227  } else
228  if(sToCompare.EqualTo(tToCompare)) {
229  ATH_MSG_VERBOSE("Current File is: " << inputCollectionsName.value()[j]);
231  return g;
232  }
233  }
234  }
235 
236  } catch(...) {
237  ATH_MSG_ERROR("currentFile(): Couldn't load InputCollections property of " << evtSelName); return 0;
238  }
239 
240  ATH_MSG_ERROR("currentFile(): Could not find the current file!");
241  return 0; //something went wrong :-(
242 
243 }
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:92
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:195
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