Commit ed27be02 authored by Paolo Calafiura's avatar Paolo Calafiura Committed by Graeme Stewart
Browse files

new VarHandleProperty format in jo (AthExStoreGateExample-00-07-08)

parent 0742f821
// -*- C++ -*-
/*
Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
*/
#ifndef ATHEXSTOREGATEEXAMPLE_ATHEXSTOREGATEEXAMPLEDICT_H
#define ATHEXSTOREGATEEXAMPLE_ATHEXSTOREGATEEXAMPLEDICT_H
#include "StoreGate/StoreGateSvc.h"
#include "AthExStoreGateExample/MyDataObj.h"
#include "AthExStoreGateExample/MyLockableDataObj.h"
#endif // not ATHEXSTOREGATEEXAMPLE_ATHEXSTOREGATEEXAMPLEDICT_H
/*
Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
*/
#ifndef _MYDATAOBJ_
#define _MYDATAOBJ_
//sample data class for the Read/Write example
//it just wraps an int. Notice that is does not inherit from Gaudi DataObject
//a dummy base class to test the symlinks
class BaseClass { };
class MyDataObj : public BaseClass {
public:
MyDataObj(): BaseClass(), m_val(0) {};
MyDataObj(int i): m_val(i) {};
virtual ~MyDataObj(){};
void val(int i) { m_val = i; }
int val() const { return m_val; }
private:
int m_val;
};
//using the macros below we can assign an identifier (and a version)
//to the type MyDataObj
//This is required and checked at compile time when you try to record/retrieve
#include "SGTools/CLASS_DEF.h"
CLASS_DEF(MyDataObj, 8000, 1)
CLASS_DEF(BaseClass, 1434, 1)
#endif
// This file's extension implies that it's C, but it's really -*- C++ -*-.
/*
Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
*/
#ifndef ATHEXSTOREGATEEXAMPLE_MYLOCKABLEDATAOBJ
#define ATHEXSTOREGATEEXAMPLE_MYLOCKABLEDATAOBJ
#include "AthenaKernel/ILockable.h"
/**
* @brief To test SG lock functionality.
*/
class MyLockableDataObj : public ILockable
{
public:
MyLockableDataObj() : m_locked (false) {}
virtual void lock() { m_locked = true; }
bool m_locked;
};
#include "SGTools/CLASS_DEF.h"
CLASS_DEF(MyLockableDataObj, 8009, 1)
#endif // not ATHEXSTOREGATEEXAMPLE_MYLOCKABLEDATAOBJ
<lcgdict>
<class name="MyDataObj"/>
<class name="MyLockableDataObj"/>
</lcgdict>
/*
Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
*/
#include "LinkObj.h"
#include "Tutorial_ClassDEF.h"
using namespace SGTutorial;
using std::vector;
//sample data class for the Link example
// Set links to an object
void LinkObj::setObjLink(MyDataObj&) //dobj
{
// Link to a data object
// set it to refer to its target
}
// Set links to an element of a collection
void LinkObj::setContLink(vector<MyElement>&, //coll
const MyElement&) //cobj
{
// Link to an element of a collection
// set it to refer to its target
// Here we pass the element to the DataLink and it figures out the
// index... which locates the element in the collection.
}
void LinkObj::setContLink(vector<MyElement>&, //coll
int) //index
{
// Link to an element of a collection
// set it to refer to its target
// Faster method: we pass the index directly
}
// Access linked object/collection
const MyDataObj& LinkObj::myLinkedObj()
{
static MyDataObj replaceMe;
return replaceMe;
}
const MyElement& LinkObj::myLinkedElement()
{
static MyElement replaceMe;
return replaceMe;
}
#include "LinkObj.h"
#include "Tutorial_ClassDEF.h"
using namespace SGTutorial;
using std::vector;
//sample data class for the Link example
// Set links to an object
void LinkObj::setObjLink(MyDataObj& dobj)
{
// Link to a data object
// set it to refer to its target
m_objLink.toStorableObject(dobj);
}
// Set links to an element of a collection
void LinkObj::setContLink(vector<MyElement>& coll,
const MyElement& cobj)
{
// Link to an element of a collection
// set it to refer to its target
// Here we pass the element to the DataLink and it figures out the
// index... which locates the element in the collection.
m_contLink.toContainedElement(coll, cobj);
}
void LinkObj::setContLink(vector<MyElement>& coll,
int index)
{
// Link to an element of a collection
// set it to refer to its target
// Faster method: we pass the index directly
m_contLink.toIndexedElement(coll, index);
}
// Access linked object/collection
const MyDataObj& LinkObj::myLinkedObj()
{
return *m_objLink; // dereference the link to get to the object
}
const MyElement& LinkObj::myLinkedElement()
{
return *m_contLink; // dereference the link to get to the collection
}
/*
Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
*/
#ifndef _LinkObj_
#define _LinkObj_
#include <vector>
#include "DataModel/DataLink.h"
#include "DataModel/ElementLink.h"
#include "MyDataObj.h"
#include "MyElement.h"
//sample data class for the Read/Write example
//it just wraps an int. Notice that is does not inherit from Gaudi DataObject
namespace SGTutorial {
class LinkObj {
public:
LinkObj() {}
~LinkObj(){}
// Set links to either an object or a collection
// Link to a data object
void setObjLink(MyDataObj&);
// Link to an element of a collection
void setContLink(std::vector<MyElement>&,
const MyElement&);
// Link to an element of a collection using an index
void setContLink(std::vector<MyElement>&,
int);
// Access linked object/collection
const MyDataObj& myLinkedObj();
const MyElement& myLinkedElement();
template <class OST>
friend OST& operator<<(OST& ost, LinkObj& rhs);
private:
DataLink<MyDataObj> m_objLink;
ElementLink< std::vector<MyElement> > m_contLink;
};
#include <iostream>
template <class OST>
inline OST& operator<<(OST& ost, LinkObj& rhs) {
ost << "LinkObj: link to MyDataObj -> " << rhs.myLinkedObj()
<< "\n link to MyElement[" << rhs.m_contLink.index() << "] -> "
<< *rhs.m_contLink;
return ost;
}
} //end ns SGTutorial
//using the macros below we can assign an identifier (and a version)
//to the type LinkObj
//This is required and checked at compile time when you try to record/retrieve
#include "SGTools/CLASS_DEF.h"
CLASS_DEF(SGTutorial::LinkObj, 9909, 1)
//class version is not currently used
#endif
/*
Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
*/
#ifndef _MYDATAOBJ_
#define _MYDATAOBJ_
//sample data class for the Read/Write example
//it just wraps an int. Notice that is does not inherit from Gaudi DataObject
namespace SGTutorial {
class MyDataObj {
public:
MyDataObj(): m_val(0) {}
MyDataObj(int i): m_val(i) {}
void val(int i) { m_val = i; }
int val() const { return m_val; }
private:
int m_val;
};
#include <iostream>
inline std::ostream& operator<<(std::ostream& ost, const MyDataObj& rhs) {
ost << "Val: " << rhs.val();
return ost;
}
} //end ns SGTutorial
//using the macros below we can assign an identifier (and a version)
//to the type MyDataObj
//This is required and checked at compile time when you try to record/retrieve
#include "SGTools/CLASS_DEF.h"
CLASS_DEF(SGTutorial::MyDataObj, 9901, 1)
#endif
/*
Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
*/
#ifndef _MyElement_
#define _MyElement_
//sample data class for the Read/Write example
//Notice that is does not inherit from Gaudi ContainedObject
namespace SGTutorial {
class MyElement {
public:
MyElement() {set(0,0);}
MyElement(float t, int i){set(t,i);}
void set(float t, int i) { m_time = t; m_channelID = i; }
float time() const { return m_time; }
int id() const { return m_channelID; }
bool operator==(const MyElement& rhs) const {
return m_time == rhs.m_time && m_channelID == rhs.m_channelID;
}
private:
float m_time;
int m_channelID;
};
#include <iostream>
template <class OST>
inline OST& operator<<(OST& ost, const MyElement& rhs) {
ost << "Time: " << rhs.time() << " ID: " << rhs.id();
return ost;
}
}
#endif
Source files of the StoreGate tutorial.
Slides are at
http://atlas.web.cern.ch/Atlas/GROUPS/SOFTWARE/OO/architecture/EventDataModel/Tutorial/
July 9 2002
/*
Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
*/
#include "SGRead.h"
#include "MyDataObj.h"
#include "LinkObj.h"
#include "Tutorial_ClassDEF.h"
#include "GaudiKernel/ISvcLocator.h"
#include "DataModel/DataLink.h"
#include "DataModel/ElementLink.h"
#include "StoreGate/StoreGateSvc.h"
using namespace SGTutorial;
/////////////////////////////////////////////////////////////////////////////
SGRead::SGRead(const std::string& name, ISvcLocator* pSvcLocator) :
AthAlgorithm(name, pSvcLocator)
{
// Declare the properties
// Key of the Data Object in the TDS:
declareProperty("DataObjKey", m_DataObjKey);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
StatusCode SGRead::initialize()
{
ATH_MSG_INFO ("in initialize()");
// Print out the key of the data objects
ATH_MSG_INFO ("Data Object key: " << m_DataObjKey);
StatusCode sc = evtStore().retrieve();
if (sc.isFailure())
{
ATH_MSG_ERROR ("Unable to retrieve pointer to StoreGate Service");
return sc;
}
return StatusCode::SUCCESS;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
StatusCode SGRead::execute()
{
ATH_MSG_INFO ("in execute()");
/////////////////////////////////////////////////////////////////////
// PART1 Objective:
// Retrieve the Data Object "MyDataObj" from SG (which was
// registered in SG by the SGWrite algorithm.
// Print out the value set in the MyDataObj
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
// PART2 Objective:
// Retrieve the Collection of MyElement objects from the SG which was
// registered in SG by the SGWrite algorithm.
// Iterate over the collection to gain access to MyElement.
// Print out each MyElement.
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
// PART3 Objective:
// Retrieve LinkObj and dump its content.
// LinkObj contains a DataLink to MyDataObj and a DataLink to a MyElement
// in a vector. It provides an accessor method myLinkedObj() that returns
// a reference to the linked MyDataObj.
// Another accessor myLinkedElement() returns a reference to the linked
// MyElement.
// Verify that the contents (time and id) match with the
// direct-dump of MyDataObj in PART1 and MyElement[0] in PART2.
/////////////////////////////////////////////////////////////////////
return StatusCode::SUCCESS;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
StatusCode SGRead::finalize()
{
ATH_MSG_INFO ("in finalize()");
return StatusCode::SUCCESS;
}
#include "AthExStoreGateExample/SGRead.h"
#include "MyDataObj.h"
#include "LinkObj.h"
#include "Tutorial_ClassDEF.h"
#include "GaudiKernel/AlgFactory.h"
#include "GaudiKernel/ISvcLocator.h"
#include "StoreGate/DataLink.h"
#include "StoreGate/tools/STLlinks.h"
#include "StoreGate/StoreGateSvc.h"
using namespace SGTutorial;
static const AlgFactory<SGRead> Factory;
const IAlgFactory& SGReadFactory = Factory;
/////////////////////////////////////////////////////////////////////////////
SGRead::SGRead(const std::string& name, ISvcLocator* pSvcLocator) :
AthAlgorithm(name, pSvcLocator)
{
// Declare the properties
// Key of the Data Object in the TDS:
declareProperty("DataObjKey", m_DataObjKey);
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
StatusCode SGRead::initialize()
{
ATH_MSG_INFO ("in initialize()");
// Print out the key of the data objects
ATH_MSG_INFO ("Data Object key: " << m_DataObjKey);
StatusCode sc = evtStore().retrieve();
if (sc.isFailure())
{
ATH_MSG_ERROR ("Unable to retrieve pointer to StoreGate Service");
return sc;
}
return StatusCode::SUCCESS;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
StatusCode SGRead::execute()
{
ATH_MSG_INFO ("in execute()");
/////////////////////////////////////////////////////////////////////
// PART1 Objective:
// Retrieve the Data Object "MyDataObj" from SG (which was
// registered in SG by the SGWrite algorithm.
// Write out the value set in the MyDataObj
/////////////////////////////////////////////////////////////////////
const DataHandle<MyDataObj> dobj;
if (!(evtStore()->retrieve(dobj, m_DataObjKey)).isSuccess()) {
ATH_MSG_ERROR ("Could not find DataObject");
return( StatusCode::FAILURE);
}
// Print out the value:
ATH_MSG_INFO
(" Retrieved MyDataObj "<< m_DataObjKey << " Val: " << dobj->val());
/////////////////////////////////////////////////////////////////////
// PART2 Objective:
// Retrieve the Collection of MyElement objects from the SG which was
// registered in SG by the SGWrite algorithm.
// Iterate over the collection to gain access to MyElement.
// Write out the value set in MyElement.
/////////////////////////////////////////////////////////////////////
const DataHandle<std::vector<MyElement> > coll;
if (!evtStore()->retrieve(coll).isSuccess()) {
// coll is equivalent to a pointer to a vector<MyElement>
ATH_MSG_ERROR ("can't retrieve default std::vector<MyElement>");
return StatusCode::FAILURE;
} else {
ATH_MSG_INFO ("retrieved default std::vector<MyElement>. It contains "
<< coll->size() << " elements ");
}
// Iterate over the collection and dump contents:
for (std::vector<MyElement>::size_type i=0; i<coll->size(); ++i) {
ATH_MSG_INFO ("MyElement[" << i <<"] :" << (*coll)[i]);
}
/////////////////////////////////////////////////////////////////////
// PART3 Objective:
// Retrieve LinkObj and dump its content.
// LinkObj contains a DataLink to MyDataObj and a DataLink to a MyElement
// in a vector. It provides an accessor method myLinkedObj() that returns
// a reference to the linked MyDataObj.
// Another accessor myLinkedElement() returns a reference to the linked
// MyElement.
// Verify that the contents (time and id) match with the
// direct-dump of MyDataObj in PART1 and MyElement[0] in PART2.
/////////////////////////////////////////////////////////////////////
const DataHandle<LinkObj> linkObj;
if (evtStore()->retrieve(linkObj).isFailure()) {
// linkObj is equivalent to a pointer to a LinkObj
ATH_MSG_ERROR ("can't retrieve default LinkObj");
return StatusCode::FAILURE;
} else {
ATH_MSG_INFO ("retrieved default LinkObj:\n" << *linkObj);
}
return StatusCode::SUCCESS;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *