r1 - 10 Jul 2008 - 15:37:49 - StephanieMajewskiYou are here: TWiki >  AtlasSoftware Web > ProofTestBedUsingPyRoot

Using PyROOT with PROOF


Using PyROOT in PROOF

Preparation

Write one main algorithm in python and one wrapper in C++. eg.,
$ cat PyTest.py

import ROOT
class PyTest(ROOT.TSelector):
    def __init__(self):
        ROOT.TSelector.__init__(self)
        self.fChain = None

    def Init(self,tree):
        print "in Init"
        self.fChain = tree

    def Process(self,entry):
        print "in Process"
        self.fChain.GetTree().GetEntry(entry)
        print self.fChain.EventNumber

==============================================

$ cat Wrapper.h

#ifndef Wrapper_h
#define Wrapper_h

#ifndef __CINT__

#if defined(linux)
#include <stdio.h>
#ifdef _POSIX_C_SOURCE
#undef _POSIX_C_SOURCE
#endif
#ifdef _FILE_OFFSET_BITS
#undef _FILE_OFFSET_BITS
#endif
#endif // defined(linux)
#include "Python.h"
#else  // __CINT__
struct _object;
typedef _object PyObject;
#endif // !__CINT__

#include "TSelector.h"

class Wrapper : public TSelector
{
public :
   Wrapper(TTree * /*tree*/ =0) : m_self(NULL) { }
   virtual ~Wrapper() { }
   virtual Int_t   Version() const { return 2; }
   virtual void    Init(TTree *tree);
   virtual Bool_t  Process(Long64_t entry);

private:
   PyObject* m_self;
};

#endif 

==============================================

$ cat Wrapper.C

#include "Wrapper.h"
#include "TPython.h"

#define MY_SELECTOR "PyTest"

void Wrapper::Init(TTree *tree)
{
  if (!tree) return;

  // just to initialize Python/TPython stuff
  TPython::Exec("import ROOT");

  if (m_self == NULL)
    {
      PyObject* module = PyImport_ImportModule(MY_SELECTOR);
      if (module != NULL)
        {
          PyObject* pclass = PyObject_GetAttrString(module,MY_SELECTOR);
          if (pclass != NULL)
            {
              PyObject * args = PyTuple_New(0);
              m_self = PyObject_Call(pclass,args,NULL);
              Py_DECREF(args);
              if (m_self != NULL)
                {
                  /*
                  TClass* klass = tree->IsA();
                  PyObject* ptree = PyROOT::BindRootObject((void*)tree,klass );
                  */
                  char * ptree_name = "_py_tree";
                  TPython::Bind((TObject *)tree, ptree_name);
                  PyObject* pmain = PyImport_AddModule("__main__");
                  PyObject* ptree = PyObject_GetAttrString(pmain, ptree_name);
                  PyObject* result = PyObject_CallMethod(m_self, "Init", "O", ptree);
                  if (result != NULL)
                    Py_DECREF(result);
                  else
                    PyErr_Print();
                  Py_DECREF(ptree);
                }
              else
                  PyErr_Print();
              Py_DECREF(pclass);
            }
          else
              PyErr_Print();
          Py_DECREF(module);
        }
      else
          PyErr_Print();
    }
}

Bool_t Wrapper::Process(Long64_t entry)
{
  if (m_self == NULL) return kFALSE;

  PyObject* result = PyObject_CallMethod( m_self, "Process", "L", entry );
  Py_DECREF(result);
  return kTRUE;
}

==============================================
You may add more methods like SlaveBegin() and may replace PyTest with your algorithm name.

Run

$ root.exe

// open session
TProof::Open("acas0420.usatlas.bnl.gov");

// add Python to LD_LIBRARY_PATH 
gProof->AddDynamicPath("/afs/usatlas.bnl.gov/cernsw/lcg/external/Python/2.4.2/slc4_ia32_gcc34/lib")

// need this one because libpython2.4.so is not loaded automatically
gProof->Exec("gSystem->Load(\"libpython2.4.so\")")

// add Python to include path
gProof->AddIncludePath("/afs/usatlas.bnl.gov/cernsw/lcg/external/Python/2.4.2/slc4_ia32_gcc34/include/python2.4")

// set PYTHONPATH
gProof->Exec("gSystem->Setenv(\"PYTHONPATH\",\"/usatlas/u/maeno/proof:/afs/usatlas.bnl.gov/cernsw/lcg/external/root/5.14.00/slc4_ia32_gcc34/root/lib\");")  
Replace /usatlas/u/maeno/proof with the directory where your python algorithm exists.
  
// create dataset
TDSet *d = new TDSet("TTree","CollectionTree")
d->Add("root://acas0420.usatlas.bnl.gov//data/cache/HPTV/user.TARRADEFabien.trig1_misal1_csc11.005013.J4_pythia_jetjet.Athena_12.0.6.GroupArea_12.0.6.6.Jamboree_II-HightPtView-00-00-30.AAN.AANT0._00065.root");      

# execute the Python algorithm via the wrapper
d->Process("Wrapper.C+")

Caveat

  • The following doesn't work with v.5.14.00g/v5.16.00 because TSelector::GetSelector("PyTest") returns NULL due to G__ClassInfo("PyTest").IsBase("TSelector")==0 and G__ClassInfo("PyTest").New()==NULL
        $ root.exe
        root [0] TPython::LoadMacro("PyTest.py")     
        root [1] ....
        root [N] TDSet *d = ...
        root [M] d->Process("PyTest")
  • PyTest.py would be uploaded to the cluster using TProof::Load() with v5.16.00
  • LD_LIBRARY_PATH and PYTHONPATH could be set on the server side


Major updates:
-- TWikiAdminGroup - 22 Apr 2018

About This Site

Please note that this site is a content mirror of the BNL US ATLAS TWiki. To edit the content of this page, click the Edit this page button at the top of the page and log in with your US ATLAS computing account name and password.


Attachments

 
Powered by TWiki
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback