PropertiesExample

From Atlas Wiki
Revision as of 14:05, 25 October 2006 by Zkestere (talk | contribs)
Jump to navigation Jump to search

Introduction

Setting properties allows the user to steer the Algorithms with help of the jobOptions.py. The advantage of this that you don't need to recompile your algorithms/tool every time you think of a change. Properties are handy too if one wants to set cut-offs, point to different files in CASTOR, use different names for ntuples and trees, and the like.

Accessing StoreGate

To let your Algorithm point to StoreGate, one should first concider two things:

  • What kind of DataObject do I want to retrieve? (answer: a DummyTrackCollection)
  • What is the key under what StoreGate has recorded it? (answer: "DummyTrackCollection")

So the ExampleAlg should have an instance of the DummyTrackCollection (to store the DummyTracks in): declare this in the header ExampleAlg.h:

class DummyTrackCollection;
class StoreGateSvc;
class ExampleAlg : public Algorithm{
  ...
  private :
  DummyTrackCollection* m_tracks;
  StoreGateSvc*         m_storeGate;
  std::string           m_location;
  };

In the implementation of ExampleAlg::initialize(), retrieve the StoregateService:

  sc=service("StoreGateSvc",m_StoreGate);
  if (sc.isFailure()) {
    log << MSG::FATAL << "StoreGate service not found !" << endreq;
    return StatusCode::FAILURE;
  } 

Every event, we want to retrieve the collection of DummyTracks:

  StatusCode ExampleAlg::execute(){
    StatusCode sc = m_storeGate->retrieve( m_tracks, "DummyTrackCollection" );
    if (sc.isFailure()) {
      log << MSG::WARNING << "Could not find DummyTrackCollection at location DummyTrackCollection !" << endreq;
    } else log << MSG::INFO << "Found a DummyTrackCollection containing " << m_tracks->size() << " tracks." << endreq;
    return sc;
  } 

Using properties

For so far, the algorithm can retrieve DummyTrackCollections from StoreGate under the location "DummyTrackCollection". The location of the collection in hard-coded in the code. If the location of the DummyTrackCollection changes in time, or if there are more DummyTrackCollections available within StoreGate, it might come in handy to be able to change the location via the jobOptions.

The jobOptions file has to be able to 'communicate' with the package. To achieve this we invoke a python variable with an arbirairy name (let's take 'LocationGivenByPython'). The variable is a string, able to hold the location of the DummyTrackCollection. This variable is a member of the AlgorithmExample package (more precisely: the ExampleAlg class).

In share/AlgorithmExample one sets the variable:

ExampleAlg = Algorithm( "ExampleAlg" )
ExampleAlg.LocationGivenByPython = "DummyTrackCollection"

Which will be used in the source file. The properties are set in the constructor of ExampleAlg:

 1 ExampleAlg::ExampleAlg(const std::string& name, ISvcLocator* pSvcLocator) : 
 2 Algorithm(name, pSvcLocator), m_tracks(0), m_storeGate(0) {
 3   declareProperties( "LocationGivenByPython", m_location );
 4 }
 ...
 5 StatusCode ExampleAlg::execute(){
 6   StatusCode sc = m_storeGate->retrieve( m_tracks, m_location );
 7   if (sc.isFailure()) {
 8     log << MSG::WARNING << "Could not find DummyTrackCollection at location DummyTrackCollection !" << endreq;
 9   } else log << MSG::INFO << "Found a DummyTrackCollection containing " << m_tracks->size() << " tracks." << endreq;
10   return sc;
11 } 

provided that m_location is already declared in the header file (as a std::string, by the way). Now, m_location carries a string with the location of where the DummyTrackCollection is to be found in StoreGate. Note, that in line 2 both constructors of DummyTrackCollection (m_tracks) and StoreGateSvc (m_storeGate) are invoked.