ATLAS Offline Software
Loading...
Searching...
No Matches
AthAnalysisAlgorithm.cxx
Go to the documentation of this file.
1
2
3/*
4 Copyright (C) 2002-2025 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
28AthAnalysisAlgorithm::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
42
44
45void 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
72StatusCode AthAnalysisAlgorithm::sysExecute(const EventContext& ctx) {
73 if(!m_doneFirstEvent) {
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
83void 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 ) {
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
147TFile* 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 = static_cast<TObjString*>(tokens->Last());
178 TString sToCompare("");
179 bool shortComparison(false);
180 if(tokens->GetEntries()>1) {
181 TString beforeSlash((static_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 if (!lastToken) {
202 ATH_MSG_ERROR("Cannot cast token string to TObjString");
203 return nullptr;
204 }
205 TString tToCompare = "";
206 bool shortComparison2(false);
207 if(tokens->GetEntries()>1) {
208 TObjString* beforeSlashStr = dynamic_cast<TObjString*>(tokens->At(tokens->GetEntries()-2));
209 if (!beforeSlashStr) {
210 ATH_MSG_ERROR("Cannot cast token string to TObjString");
211 return nullptr;
212 }
213 TString beforeSlash(beforeSlashStr->GetString());
214 if(beforeSlash.Length()>0) tToCompare += beforeSlash;
215 tToCompare += "/";
216 } else {
217 shortComparison2=true;
218 }
219 tToCompare += lastToken->GetString();
220 TString tToCompare_short(lastToken->GetString());
221 delete tokens;
222
223 if(shortComparison || shortComparison2) { //doing short version search, no directories to distinguish files!
224 if(sToCompare_short.EqualTo(tToCompare_short)) {
225 ATH_MSG_VERBOSE("Current File is: " << inputCollectionsName.value()[j]);
226 m_currentFile = g;
227 return g;
228 }
229 } else
230 if(sToCompare.EqualTo(tToCompare)) {
231 ATH_MSG_VERBOSE("Current File is: " << inputCollectionsName.value()[j]);
233 return g;
234 }
235 }
236 }
237
238 } catch(...) {
239 ATH_MSG_ERROR("currentFile(): Couldn't load InputCollections property of " << evtSelName); return 0;
240 }
241
242 ATH_MSG_ERROR("currentFile(): Could not find the current file!");
243 return 0; //something went wrong :-(
244
245}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
virtual TFile * currentFile(const char *evtSelName="EventSelector") final
Function returning the TFile pointer of the currently open file of the given EventSelector (in athena...
virtual StatusCode beginInputFile()
Function called when a new input file is opened user can read input metadata from inputMetaStore()
virtual StatusCode endInputFile()
Function called as an input file is being closed.
virtual StatusCode sysExecute(const EventContext &) override
override to do firstEvent method
ServiceHandle< StoreGateSvc > m_inputMetaStore
Object accessing the input metadata store.
virtual void handle(const Incident &inc) override
Function receiving incidents from IncidentSvc/TEvent Experts can override but they should ensure they...
virtual StatusCode metaDataStop()
Function called before finalize user can read output metadata from outputMetaStore()
void updateEvtStore(Gaudi::Details::PropertyBase &prop)
ServiceHandle< StoreGateSvc > m_outputMetaStore
Object accessing the output metadata store.
virtual ~AthAnalysisAlgorithm() override
virtual StatusCode firstExecute()
Function called when first execute is encountered user can read event information with evtStore()
virtual StatusCode sysInitialize() override
Function initialising the tool in the correct way in Athena.
AthAnalysisAlgorithm(const std::string &name)
Constructor taking just a name.
bool msgLvl(const MSG::Level lvl) const
virtual StatusCode sysInitialize()
Initialization method invoked by the framework.
AthHistogramAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
=============================================================================