210 likes | 321 Views
StoreGate Tutorial. March 8, 2002 CERN. Outline. TDS access using StoreGate record/retrieve by TYPE Data Objects can be keyed in SG Retrieval of all data objects of a given type Data Links Inter-Object relationships. Preliminaries: Accessing StoreGate.
E N D
StoreGate Tutorial March 8, 2002 CERN
Outline • TDS access using StoreGate • record/retrieve by TYPE • Data Objects can be keyed in SG • Retrieval of all data objects of a given type • Data Links • Inter-Object relationships
Preliminaries:Accessing StoreGate • StoreGateSvc is the Gaudi service that allows to record and retrieve data objects. • Obtain the pointer to the StoreGateSvc: In the initialize method of your algorithm: StatusCode sc = service(“StoreGateSvc”, m_storeGate); declare m_storeGate as a private data member of type StoreGateSvc* This caches m_storeGate and you do not have to call the serviceLocator to retrieve the pointer to StoreGateSvc every event in your execute method.
Recording an Object • Providing a Key: StatusCode sc = m_storeGate record(TrkColl, TrkCollName); • where : • TrkColl is a pointer to your TrackCollection (a DataObject) • TrkCollName is an identifying name for your data object also specified in your jobOptions in a similar way. It can be something like: “MyTrackCollection” • Keyless: StatusCode sc = m_storeGate record(TrkColl); • Locking an Object StatusCode sc = m_storeGate setConst(TrkColl);
Retrieving a Data Object • Specifying a Key: DataHandle<TrackCollection> TrkColl; sc = m_storeGate retrieve( TrkColl, TrkCollName); We create an instance of the DataHandle (TrkColl) and pass it to the retrieve method. It is returned to you and can be used as a C++ pointer: TrkColl some_method_in_TrackCollection(); Iterate over Tracks in TrackCollection just as you used to. • Retrieving the Default instance: DataHandle<TrackCollection> DefaultTrkColl; sc = m_storeGate retrieve( DefaultTrkColl);
Retrieving all Data Objects • If you have several TrackCollections recorded in the SG and you wish to retrieve ALL of them, this is what you do: • DataHandle<TrackCollection> dbegin, dend; • m_storegate retrieve(dbegin, dend); • for (; dbegin != dend; ++ dbegin) // loop over TrackCollections • { • dbegin method_in_trackCollection(); • TrackCollection::iterator iter dbegin begin(); • for (; iter != dbegin end(); ++iter) // loop over TrackObjects • { • (*iter) TrackPT(); // call some method in Track • } • } • This is not a part of the exercise today, but for your information.
Store Access Policy An object in SG may be modified until it is setConst • Access to a const Data Object: const DataHandle<TrackCollection> trackHandle; m_storegate retrieve( trackHandle, key ); • Only const methods in the Data Object are accessible! • class Track { void set_pT(float pT) { m_pT = pT; } // NO ACCESS float get_pT const { return m_pT; } // ACCESS OK … } • Must use const-iterators to iterate over TrackCollection • If you do not specify const, this will force a check. If the data object you are accessing is ‘const’, then an error is returned if accessing it in a non-const way.
Tutorial Setup - 1 • We use accounts on atlas.cern.ch (lxplus cluster) • User: atltr<x> where <x> is 0- 9 and a-e • Password: ATLtrain • Machine: atlas.cern.ch • Working directory in students AFS accounts
Tutorial Setup - 2 • Copy CMT requirements file in your home directory (~/requirements) • Already done for you - basic one from Atlas CMT primer • Defines: • Your site • Your software distribution area • The release number of Atlas software • Setup CMT (once if not done already) • sh // CERN tcsh is obsolete and gives problems • source /afs/cern.ch/sw/contrib/CMT/v1r10p20011126/mgr/setup.sh • cmt config • Check g++ -v • We want 2.95.2!
Tutorial Setup - 3 • Go to our work directory • cd maxidisk/Athena // create it if not there • cmt co -r AthExStoreGateExample-00-00-20 Control/AthenaExamples/AthExStoreGateExample • cmt co TestRelease • Go to TestRelease cmt directory • source setup.sh • cmt bro gmake • athena SGTutorial_jobOptions.txt • If using release 3.0.1 you need also to check out • StoreGate-02-00-16
Hands On • In AthExStoreGateExample/[…]/Tutorial, you will find: • Two algorithms: • SGWrite • SGRead • A data object class: MyDataObj • A contained object: MyElement • A object for data links: LinkObj • The ClassDEF macros Tutorial_ClassDEF.h • You do not have to modify any Data classes, • only algorithms, although they are worth a read.
Hands On (contd.) • SGWrite: • Create a MyDataObj(PART 1) • set the data member in MyDataObj • Register MyDataObj using a key • Create a std::vector of MyElement(s)(PART 2) • Create and Fills two MyElement(s) • (new std::vector<MyElement>(2)) • push MyElement(s) into vector • Register key-less (default key) • Create LinkObjand set data link members(PART 3) • Set links to: • MyDataObj • one specific MyElement in the vector collection • SGRead: (PART 1 - PART 3) • Retrieves the objects from the transient store: MyDataObj, MyElement and LinkObj • Interrogates elements and dumps contents on screen.
Hands On (contd.) • Take a look at Tutorial_ClassDEF.h • It contains the CLASS_DEF macros for the “external” data object your package uses (typically the STL containers) In this case we have (omitting namespaces) CLASS_DEF(vector<MyElement, 9903, 1) “9903” is the famous CLID “1” is the version number (currently ignored by SG) • More examples are in ../src/StoreGateExample_ClassDEF.h
Hands On (contd.) • Fill in the algorithms SGWrite and SGRead (PART 1-2) • In SGWrite you need to : • obtain the StoreGateSvc pointer and cache it in initialize() • StatusCode sc = service("StoreGateSvc", p_eventStore); • Record by data type • with a key = (property from jobOptions) • StatusCode sc = p_eventStore->record(pdobj, m_DataObjKey); • without a key • StatusCode sc = p_eventStore->record(pcoll);
Hands On (contd.) • Fill in the algorithms SGWrite and SGRead (PART 1-2) • In SGWrite you need to : • obtain the StoreGateSvc pointer and cache it in initialize() • StatusCode sc = service("StoreGateSvc", p_eventStore); • Record by data type • with a key = (property from jobOptions) • StatusCode sc = p_eventStore->record(pdobj, m_DataObjKey); • without a key • StatusCode sc = p_eventStore->record(pcoll);
Hands On (contd.) • Fill in the algorithms SGRead (PART 1-2) • You need to : • Create a “const” DataHandle and pass it to the retrieve method • Using same key! • const DataHandle<MyDataObj> dobj; • StatusCode sc = m_storeGate->retrieve(dobj, m_DataObjKey); • On return, dobj can be used as a pointer to the DataObject • Retrieve the vector collection • Iterate over the collection to access MyElement(s) • for (std::vector<MyElement>::size_type i=0; i<coll->size(); ++i) • Dump the contents of MyDataObj (PART 1) and MyElement (PART 2). Use the MsgStream.
Hands On (contd.) • From TestRelease/<version>/cmt • cmt broadcast gmake • cd to the run area • Where the jobOption files are • In your jobOptions file, you need to • Declare a string key for data objects in the TS, e.g., SGWrite.DataObjKey = “MyData” • Same for SGRead
Links • Allow to establish a persistable inter objects elationship • A Track holding pointers to a set of associated hits. • A link can be to a single object/collection • DataLink<MyDataObj> m_objLink; • DataLink<MyColl> m_collectionLink; • Or an element in a list/vector (SequenceLink), a map (MapLink) or a set (SetLink) or non-STL container like HepMC • SequenceLink<std::vector<MyElement> >::type m_contLink; • The data link can be used as though it was a pointer to the data object or to the element. To get to the ‘real’ object/element one has to dereference the link • *m_objLink • *m_contLink • Collections must not be modified after a DataLink to an element in the collection has been established.
Creating a DataLink • Example: • Link to a data object m_objLink.toStorableObject(dobj); • Link to a contained object std::vector<MyElement>* pVect; SequenceLink< vector<MyElement> >::type seqLink; seqLink.toContainedElement(*pVect, pElement); • time consuming index search • A faster way: seqLink.toIndexedElement(*pv, index); • index = 0 for first element, 1 for second element, … • Not in 3.0.0: use StoreGate-02-00-16
Hands On • In LinkObj.h: Insert as data members a DataLink to a MyDataObj and a SequenceLink to a MyElement in the vector created in part 2 Implement LinkObj methods using • m_objLink.toStorableObject(dobj); • m_contLink.toContainedElement(coll, cobj); • Or better • m_contLink.toIndexedElement(coll, index); Implement a LinkObj extractor (operator <<) that invokes the linked-to object extractors. For the SequenceLink also print the index value • ost << “ link to MyElement[“ << rhs.m_contLink.index()”] :” • << *rhs.m_contLink;
Hands On #include "StoreGate/DataLink.h" #include "StoreGate/tools/STLlinks.h” #include “Tutorial_ClassDef.h • In SGWrite (Part 3): Instantiate a LinkObj • Establish the data links in LinkObj to MyDataObj and an element within vector<MyElement>. • In SGRead (Part 3): • Retrieve the LinkObj and verify that it has worked. log << MSG::INFO << "retrieved default LinkObj:\n" << *linkObj << endreq;