12#include "Gaudi/Interfaces/IOptionsSvc.h"
13#include "GaudiKernel/IAlgManager.h"
14#include "GaudiKernel/ISvcLocator.h"
15#include "GaudiKernel/IAddressCreator.h"
16#include "GaudiKernel/IOpaqueAddress.h"
17#include "GaudiKernel/ITHistSvc.h"
18#include "GaudiKernel/ServiceHandle.h"
19#include "GaudiKernel/IIoComponentMgr.h"
25#include "CoralBase/AttributeList.h"
26#include "CoralBase/AttributeListSpecification.h"
28#include "EventInfo/EventInfo.h"
36#include "TInterpreter.h"
46 : base_class::base_class(name, pSvcLocator),
54 declareProperty(
"OutputName",
m_fileName=
"AANTuple.root");
57 declareProperty(
"ExistDataHeader",
m_existDH=
true);
59 declareProperty(
"LateSchemaWriting",
m_lateSchema=
false);
61 declareProperty(
"Macro",
m_macro=
"");
84 if ( !iomgr->io_register (
this,
85 IIoComponentMgr::IoMode::WRITE,
88 <<
m_fileName <<
"] with the I/O component manager !");
89 return StatusCode::FAILURE;
102 m_attribSpec->extend( name_EventNumber,
"unsigned int" );
103 const std::string refStr{
"_ref"};
104 const std::string stringTypeStr{
"string"};
121 return StatusCode::SUCCESS;
138 return StatusCode::SUCCESS;
151 std::string curDir = gDirectory->GetPath();
157 gInterpreter->ExecuteMacro(
m_macro.c_str());
159 gDirectory->cd(curDir.c_str());
162 return StatusCode::SUCCESS;
177 std::map<std::string,std::string> inputRefs;
186 StatusCode status = evtStore()->retrieve(beg,ending);
187 if (status.isFailure() || beg==ending)
190 return StatusCode::FAILURE;
194 for (; beg != ending; ++beg)
200 return StatusCode::FAILURE;
210 return StatusCode::FAILURE;
220 std::vector<DataHeaderElement>::const_iterator it =
222 std::vector<DataHeaderElement>::const_iterator itE =
225 for (; it!=itE; ++it)
227 std::string tmpRef = (*it).getToken() != NULL
228 ? (*it).getToken()->toString()
230 inputRefs.insert(std::make_pair(it->getKey(),tmpRef));
231 ATH_MSG_DEBUG (
"Found ref for input header: Key=" << it->getKey()
232 <<
", Ref=" << tmpRef);
243 unsigned int runNumber = 0;
244 unsigned int eventNumber = 0;
248 eventInfo = evtStore()->tryConstRetrieve<
EventInfo>();
253 ATH_CHECK( evtStore()->retrieve(eventInfoX) );
259 runNumber = eventInfo->
event_ID()->run_number();
260 eventNumber = eventInfo->
event_ID()->event_number();
264 newAttr[ name_RunNumber ].setValue( runNumber );
265 newAttr[ name_EventNumber ].setValue( eventNumber );
270 std::map<std::string,std::string>::iterator itRef = inputRefs.begin();
271 std::map<std::string,std::string>::iterator itRefE = inputRefs.end();
272 for (; itRef != itRefE; ++itRef)
274 std::string name = itRef->first +
"_ref";
275 if (extraRefs.end() != std::find(extraRefs.begin(),extraRefs.end(),itRef->first))
277 newAttr[name].setValue(std::string(itRef->second));
278 ATH_MSG_DEBUG (
" Added extra ref to new attribute list - name: "
279 << name <<
" " << itRef->second);
293 catch (std::exception &e)
296 (
"Caught exception from collection add of Pool attributes. "
297 "Message: " << e.what());
298 return StatusCode::FAILURE;
304 return StatusCode::SUCCESS;
311 TDirectory::TContext save;
318 static const std::map<std::string,char> typeDict = {
320 {
"long double",
'D'},
323 {
"unsigned int",
'I'},
325 {
"unsigned long",
'I'},
327 {
"unsigned short",
'S'},
329 {
"unsigned char",
'B'},
336 TDirectory::TContext save;
343 for(coral::AttributeListSpecification::const_iterator iter=
m_attribSpec->begin(); iter!=
m_attribSpec->end(); ++iter)
345 std::string
type =
"/?";
346 type[1] = typeDict.at(iter->typeName());
347 std::string leaflist = iter->name() +
type;
348 m_tree->Branch(iter->name().c_str(),0,leaflist.c_str());
352 std::string leaflist = c_tokenBranchName;
354 m_tree->Branch(c_tokenBranchName,0,leaflist.c_str());
367 itemProxy = evtStore()->proxy(
id, key);
371 return(StatusCode::FAILURE);
375 ATH_MSG_DEBUG (
" found proxy for ("<<
id <<
",\"" << key <<
"\") ");
381 return(StatusCode::FAILURE);
385 IOpaqueAddress* addr = itemProxy->
address();
389 << itemProxy->
clID() <<
" " << itemProxy->
name());
390 return( StatusCode::FAILURE);
397 StatusCode
sc =
m_persSvc->convertAddress(addr, saddr);
401 (
"Could not get string from IOpaqueAddress for clid " <<
id
403 <<
" is BAD_STORAGE_TYPE: "<< (
sc == IConversionSvc::Status::BAD_STORAGE_TYPE));
404 return( StatusCode::FAILURE);
414 ATH_MSG_ERROR (
"Could not get split off token. string address " << saddr);
415 return( StatusCode::FAILURE);
420 return StatusCode::SUCCESS;
427 const std::string&
match,
428 std::string& address_header,
429 std::string& address_data )
const
432 <<
" address : " << address
433 <<
" match : " <<
match);
435 std::string::size_type p1;
437 p1 = address.find(
match );
438 if (p1 != std::string::npos)
440 address_header = address.substr( 0, p1+
match.size());
442 address_data = address.substr( p1+
match.size() );
446 return StatusCode::FAILURE;
451 <<
" address_data : " << address_data);
453 return StatusCode::SUCCESS;
461 coral::AttributeList::const_iterator attIter = attributeList.begin();
462 const TObjArray* branches =
m_tree->GetListOfBranches();
463 std::vector<std::string> tokenStringDump;
465 for (
unsigned int i=0; i<attributeList.size(); ++i)
467 TBranch* branch = (TBranch*)branches->UncheckedAt(i);
469 if (attIter->specification().type() ==
typeid(std::string))
471 const std::string&
str = attIter->data<std::string>();
474 throw std::runtime_error(
"String is too long : RootCollection::pool2Root<std::string>");
476 branch->SetAddress( (
void*)
str.c_str() );
479 else if(attIter->specification().typeName() ==
"Token")
481 tokenStringDump.push_back( attIter->data<std::string>() );
482 branch->SetAddress( (
void*) tokenStringDump.back().c_str() );
487 branch->SetAddress( (
void*)attIter->addressOfData() );
512 return StatusCode::SUCCESS;
520 SmartIF<IAlgorithm> ialg{theAlgMgr->algorithm(name)};
524 return StatusCode::FAILURE;
530 return StatusCode::SUCCESS;
544 const EventContext& ctx = Gaudi::Hive::currentContext();
549 if ( alg->execState(ctx).state() == AlgExecState::State::Done &&
550 ( ! alg->execState(ctx).filterPassed() ) )
573 return StatusCode::SUCCESS;
586 if ( !iomgr->io_hasitem (
this) ) {
587 ATH_MSG_ERROR (
"I/O component mgr does not know about ourselves !");
588 return StatusCode::FAILURE;
592 if ( !iomgr->io_contains (
this,
m_fileName) ) {
594 (
"I/O component mgr does not have any entry about ["<<
m_fileName<<
"]");
595 return StatusCode::SUCCESS;
598 if ( !iomgr->io_retrieve (
this,
m_fileName).isSuccess() ) {
600 return StatusCode::FAILURE;
604 std::vector<std::string> outvec = {
"AANT DATAFILE='" +
m_fileName +
"' OPT='RECREATE'"};
605 josvc->set(
"THistSvc.Output", Gaudi::Utils::toString(outvec));
614 return StatusCode::SUCCESS;
const std::regex ref(r_ef)
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_WARNING(x)
This class provides a unique identification for each event, in terms of run/event number and/or a tim...
uint32_t CLID
The Class ID type.
static void setTree(const std::string &stream, TTree *tree)
void setupTree()
setup Tree
std::string m_treeName
Name of the output tree.
BooleanProperty m_lateSchema
write schema in execute()
AANTupleStream(const std::string &name, ISvcLocator *pSvcLocator)
Standard AANTupleStream Constructor.
virtual ~AANTupleStream()
Standard Destructor.
StatusCode initCollection()
Initialize the output collection.
virtual StatusCode io_reinit() override
callback method to reinitialize the internal state of the component for I/O purposes (e....
bool writeTokenAttrList(const std::string &token, const coral::AttributeList &attributeList)
write Token and AttributeList
bool m_schemaDone
first event
StatusCode getFilters()
get filter alogs
ServiceHandle< IAddressCreator > m_persSvc
Persistency service.
virtual StatusCode initialize() override
Initialize AANTupleStream.
StatusCode splitAddress(const std::string &address, const std::string &match, std::string &address_header, std::string &address_data) const
Split the address to get pool token.
std::vector< IAlgorithm * > m_acceptAlgs
Vector of Algorithms that this stream accepts.
virtual StatusCode execute(const EventContext &ctx) override
Working entry point.
BooleanProperty m_writeInputDH
Flag to signal whether or not to write input data header.
TBranch * m_tokenBranch
TBranch which contains Token info.
char m_tokenCString[s_maxLengthOfStrings]
StringArrayProperty m_extraRefNames
Vector of extra parent ref names to register in collection.
static const unsigned int s_maxLengthOfStrings
for string branches
std::vector< std::string > m_acceptNames
Vector of names of Algorithms that this stream accepts.
coral::AttributeListSpecification * m_attribSpec
Collection attribute specification.
BooleanProperty m_existDH
Flag to signal whether data header exists.
std::string m_fileName
Name of the output collection.
virtual StatusCode finalize() override
Terminate AANTupleStream.
StatusCode getRef(CLID id, const std::string &key, std::string &ref)
Get ref from a proxy.
ServiceHandle< ITHistSvc > m_tHistSvc
THistSvc.
std::string m_streamName
Name of Stream.
bool isEventAccepted() const
check filters passed
void writeAttributeListSpecification()
write schema of AttributeList
EventID * event_ID()
the unique identification of the event.
a const_iterator facade to DataHandle.
CLID clID() const
Retrieve clid.
virtual const name_type & name() const override final
Retrieve data object key == string.
virtual IOpaqueAddress * address() const override final
Retrieve IOpaqueAddress.
uint32_t runNumber() const
The current event's run number.
uint64_t eventNumber() const
The current event's event number.
bool match(std::string s1, std::string s2)
match the individual directories of two strings
static const std::string c_streamName
static const std::string c_treeName
EventInfo_v1 EventInfo
Definition of the latest event info version.
static constexpr CLID ID()