PropertiesExample

From Atlas Wiki
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:

 1 class DummyTrackCollection;
 2 class StoreGateSvc;
 3 class ExampleAlg : public Algorithm{
   ...
 4  private :
 5  DummyTrackCollection* m_tracks;
 6  StoreGateSvc*         m_storeGate;
 7  std::string           m_location;
 8  };

Lines 1 and 2 are predeclarations of the classes used in ExampleAlg. Line 5 declares the DummyTrackCollection, line 6 the StoreGateSvc. Line 7 is necessary for later on in this assignment.

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   declareProperty( "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 " << m_location << " !" <<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. In line 6 the value of m_location is used as the key for StoreGate to look for. Line 8 uses m_location as well for output. Do not forget to include the appropriate headers (DummyTrackCollection, StoreGate, string)!

Final note

One more thing: one should first make the DummyTracks and store them in StoreGate under the key. Only then ExampleAlg will find these collections in StoreGate! There already exist a python script in share/GenerateDummyTracks.py who handles this, one should include this in the main jobOptions.py under run:

if(doExample) :
   include("AlgorithmExample/GenerateDummyTracks.py")
   include("AlgorithmExample/AlgorithmExample.py")

Note the order of including the pythonscripts!!!