Logo ROOT  
Reference Guide
ProofAux.C File Reference

Detailed Description

Selector used for auxiliary actions in the PROOF tutorials.

#define ProofAux_cxx
#include "ProofAux.h"
#include "TDSet.h"
#include "TProofServ.h"
#include "TMap.h"
#include "TString.h"
#include "TSystem.h"
#include "TParameter.h"
#include "TFile.h"
#include "TUrl.h"
#include "TTree.h"
#include "TRandom.h"
#include "TMath.h"
// Constructor
fAction = -1;
fNEvents= -1;
fMainList = 0;
fFriendList = 0;
fDir = "";
// Destructor
Int_t ProofAux::GetAction(TList *input)
// Get the required action.
// Returns -1 if unknown.
Int_t action = -1;
// Determine the test type
TNamed *ntype = dynamic_cast<TNamed*>(input->FindObject("ProofAux_Action"));
if (ntype) {
TString act(ntype->GetTitle());
if (act == "GenerateTreesSameFile") {
action = 1;
} else if (act.BeginsWith("GenerateTrees")) {
action = 0;
// Extract directory, if any
Ssiz_t icol = act.Index(":");
if (icol != kNPOS) {
action = 2;
act.Remove(0, icol+1);
if (!act.IsNull()) fDir = act;
} else {
Warning("GetAction", "unknown action: '%s'", ntype->GetTitle());
// Done
return action;
void ProofAux::Begin(TTree * /*tree*/)
// The Begin() function is called at the start of the query.
// When running with PROOF Begin() is only called on the client.
// The tree argument is deprecated (on PROOF 0 is passed).
void ProofAux::SlaveBegin(TTree * /*tree*/)
// The SlaveBegin() function is called after the Begin() function.
// When running with PROOF SlaveBegin() is called on each slave server.
// The tree argument is deprecated (on PROOF 0 is passed).
TString option = GetOption();
// Determine the action type
fAction = GetAction(fInput);
// Get the number of events
TParameter<Long64_t> *a = (TParameter<Long64_t> *) fInput->FindObject("ProofAux_NEvents");
if (a) fNEvents = a->GetVal();
// Create lists
fMainList = new TList;
if (gProofServ) fMainList->SetName(TString::Format("MainList-%s", gProofServ->GetOrdinal()));
if (fAction < 2) {
fFriendList = new TList;
if (gProofServ) fFriendList->SetName(TString::Format("FriendList-%s", gProofServ->GetOrdinal()));
Bool_t ProofAux::Process(Long64_t entry)
// The Process() function is called for each entry in the tree (or possibly
// keyed object in the case of PROOF) to be processed. The entry argument
// specifies which entry in the currently loaded tree is to be processed.
// It can be passed to either ProofAux::GetEntry() or TBranch::GetEntry()
// to read either all or the required parts of the data. When processing
// keyed objects with PROOF, the object is already loaded and is available
// via the fObject pointer.
// This function should contain the "body" of the analysis. It can contain
// simple or elaborate selection criteria, run algorithms on the data
// of the event and typically fill histograms.
// The processing can be stopped by calling Abort().
// Use fStatus to set the return value of TTree::Process().
// The return value is currently not used.
// Nothing to do if the action if not defined
if (fAction < 0) {
Error("Process", "action not specified!");
return kFALSE;
// Link to current element, if any
TDSetElement *fCurrent = 0;
TPair *elemPair = 0;
if (fInput && (elemPair = dynamic_cast<TPair *>(fInput->FindObject("PROOF_CurrentElement")))) {
if ((fCurrent = dynamic_cast<TDSetElement *>(elemPair->Value()))) {
Info("Process", "entry %lld: file: '%s'", entry, fCurrent->GetName());
} else {
Error("Process", "entry %lld: no file specified!", entry);
return kFALSE;
// Act now
if (fAction == 0) {
TString fnt;
// Generate the TTree and save it in the specified file
if (GenerateTree(fCurrent->GetName(), fNEvents, fnt) != 0) {
Error("Process", "problems generating tree (%lld, %s, %lld)",
entry, fCurrent->GetName(), fNEvents);
return kFALSE;
// The output filename
TString fnf(fnt);
TString xf = gSystem->BaseName(fnf);
fnf = gSystem->GetDirName(fnf);
if (xf.Contains("tree")) {
xf.ReplaceAll("tree", "friend");
} else {
if (xf.EndsWith(".root")) {
xf.ReplaceAll(".root", "_friend.root");
} else {
xf += "_friend";
fnf += TString::Format("/%s", xf.Data());
// Generate the TTree friend and save it in the specified file
if (GenerateFriend(fnt, fnf) != 0) {
Error("Process", "problems generating friend tree for %s (%s)",
fCurrent->GetName(), fnt.Data());
return kFALSE;
} else if (fAction == 1) {
TString fnt;
// Generate the TTree and save it in the specified file
if (GenerateTree(fCurrent->GetName(), fNEvents, fnt) != 0) {
Error("Process", "problems generating tree (%lld, %s, %lld)",
entry, fCurrent->GetName(), fNEvents);
return kFALSE;
// Generate the TTree friend and save it in the specified file
if (GenerateFriend(fnt) != 0) {
Error("Process", "problems generating friend tree for %s (%s)",
fCurrent->GetName(), fnt.Data());
return kFALSE;
} else if (fAction == 2) {
TString fnt;
// Generate the TTree and save it in the specified file
if (GenerateTree(fCurrent->GetName(), fNEvents, fnt) != 0) {
Error("Process", "problems generating tree (%lld, %s, %lld)",
entry, fCurrent->GetName(), fNEvents);
return kFALSE;
} else {
// Unknown action
Warning("Process", "do not know how to process action %d - do nothing", fAction);
return kFALSE;
return kTRUE;
void ProofAux::SlaveTerminate()
// The SlaveTerminate() function is called after all entries or objects
// have been processed. When running with PROOF SlaveTerminate() is called
// on each slave server.
if (fMainList && fMainList->GetSize() > 0) fOutput->Add(fMainList);
if (fFriendList && fFriendList->GetSize() > 0) fOutput->Add(fFriendList);
void ProofAux::Terminate()
// The Terminate() function is the last function to be called during
// a query. It always runs on the client, it can be used to present
// the results graphically or save the results to file.
Int_t ProofAux::GenerateTree(const char *fnt, Long64_t ent, TString &fn)
// Generate the main tree for the 'friends' tutorial; the tree is called
// 'Tmain', has 'ent' entries and is saved to file 'fnt'.
// The full file path is returned in 'fn'.
// Return 0 on success, -1 on error.
Int_t rc = -1;
// Check the filename
fn = fnt;
if (fn.IsNull()) {
Error("GenerateTree", "file name undefined!");
return rc;
TUrl uu(fn, kTRUE);
if (!strcmp(uu.GetProtocol(), "file") && !fn.BeginsWith("/")) {
// Local file with relative path: create under the data directory
if (!gProofServ ||
!(gProofServ->GetDataDir()) || strlen(gProofServ->GetDataDir()) <= 0) {
Error("GenerateTree", "data directory undefined!");
return rc;
// Insert data directory
// Make sure the directory exists
if (gSystem->mkdir(dir, kTRUE) != 0) {
Error("GenerateTree", "problems creating directory %s to store the file", dir.Data());
return rc;
// Create the file
TDirectory *savedir = gDirectory;
TFile *f = new TFile(fn, "RECREATE");
if (!f || f->IsZombie()) {
Error("GenerateTree", "problems opening file %s", fn.Data());
return rc;
// Create sub-drirectory, if required
TDirectory *destdir = f;
if (!fDir.IsNull()) {
if (f->mkdir(fDir.Data())) {
Info("GenerateTree", "sub-directory '%s' successfully created", fDir.Data());
destdir = gDirectory;
} else {
Error("GenerateTree", "creating sub-directory '%s'", fDir.Data());
delete f;
return rc;
rc = 0;
// Create the tree
TTree *T = new TTree("Tmain","Main tree for tutorial friends");
Int_t Run = 1;
Long64_t Event = 0;
Float_t x = 0., y = 0., z = 0.;
for (Long64_t i = 0; i < ent; i++) {
if (i > 0 && i%1000 == 0) Run++;
Event = i;
x = r.Gaus(10,1);
y = r.Gaus(20,2);
z = r.Landau(2,1);
delete f;
delete T;
// Notify success
Info("GenerateTree", "file '%s' successfully created", fn.Data());
// Add to the list
TString fds(fn);
if (!strcmp(uu.GetProtocol(), "file")) {
if (gSystem->Getenv("LOCALDATASERVER")) {
if (strcmp(TUrl(gSystem->Getenv("LOCALDATASERVER"), kTRUE).GetProtocol(), "file"))
fds.Insert(0, TString::Format("%s/", gSystem->Getenv("LOCALDATASERVER")));
} else {
fds.Insert(0, TString::Format("root://%s/", gSystem->HostName()));
fMainList->Add(new TObjString(fds));
// Done
return rc;
Int_t ProofAux::GenerateFriend(const char *fnt, const char *fnf)
// Generate the friend tree for the main tree in the 'friends' tutorial fetched
// from 'fnt'.
// the tree is called 'Tfriend', has the same number of entries as the main
// tree and is saved to file 'fnf'. If 'fnf' is not defined the filename is
// derived from 'fnt' either replacing 'tree' with 'friend', or adding '_friend'
// before the '.root' extension.
// Return 0 on success, -1 on error.
Int_t rc = -1;
// Check the input filename
TString fin(fnt);
if (fin.IsNull()) {
Error("GenerateFriend", "file name for the main tree undefined!");
return rc;
// Make sure that the file can be read
Error("GenerateFriend", "input file does not exist or cannot be read: %s", fin.Data());
return rc;
// File handlers
Bool_t sameFile = kTRUE;
const char *openMain = "UPDATE";
// The output filename
TString fout(fnf);
if (!fout.IsNull()) {
sameFile = kFALSE;
openMain = "READ";
// Make sure the directory exists
TString dir = gSystem->GetDirName(fout);
if (gSystem->mkdir(dir, kTRUE) != 0) {
Error("GenerateFriend", "problems creating directory %s to store the file", dir.Data());
return rc;
} else {
// We set the same name
fout = fin;
// Get main tree
TFile *fi = TFile::Open(fin, openMain);
if (!fi || fi->IsZombie()) {
Error("GenerateFriend", "problems opening input file %s", fin.Data());
return rc;
TTree *Tin = (TTree *) fi->Get("Tmain");
if (!Tin) {
Error("GenerateFriend", "problems getting tree 'Tmain' from file %s", fin.Data());
delete fi;
return rc;
// Set branches
Float_t x, y, z;
Tin->SetBranchAddress("x", &x);
Tin->SetBranchAddress("y", &y);
Tin->SetBranchAddress("z", &z);
TBranch *b_x = Tin->GetBranch("x");
TBranch *b_y = Tin->GetBranch("y");
TBranch *b_z = Tin->GetBranch("z");
TDirectory* savedir = gDirectory;
// Create output file
TFile *fo = 0;
if (!sameFile) {
fo = new TFile(fout, "RECREATE");
if (!fo || fo->IsZombie()) {
Error("GenerateFriend", "problems opening file %s", fout.Data());
delete fi;
return rc;
} else {
// Same file
fo = fi;
rc = 0;
// Create the tree
TTree *Tfrnd = new TTree("Tfrnd", "Friend tree for tutorial 'friends'");
Float_t r = 0;
Long64_t ent = Tin->GetEntries();
for (Long64_t i = 0; i < ent; i++) {
r = TMath::Sqrt(x*x + y*y + z*z);
if (!sameFile) {
delete fi;
delete fo;
delete Tfrnd;
// Notify success
Info("GenerateFriend", "friend file '%s' successfully created", fout.Data());
// Add to the list
TUrl uu(fout);
if (!strcmp(uu.GetProtocol(), "file")) {
if (gSystem->Getenv("LOCALDATASERVER")) {
if (strcmp(TUrl(gSystem->Getenv("LOCALDATASERVER"), kTRUE).GetProtocol(), "file"))
fout.Insert(0, TString::Format("%s/", gSystem->Getenv("LOCALDATASERVER")));
} else {
fout.Insert(0, TString::Format("root://%s/", gSystem->HostName()));
fFriendList->Add(new TObjString(fout));
// Done
return rc;
ROOT::R::TRInterface & r
Definition: Object.C:4
Selector used for auxiliary actions in the PROOF tutorials.
#define f(i)
Definition: RSha256.hxx:104
const Ssiz_t kNPOS
Definition: RtypesCore.h:115
int Int_t
Definition: RtypesCore.h:45
int Ssiz_t
Definition: RtypesCore.h:67
const Bool_t kFALSE
Definition: RtypesCore.h:92
bool Bool_t
Definition: RtypesCore.h:63
long long Long64_t
Definition: RtypesCore.h:73
float Float_t
Definition: RtypesCore.h:57
const Bool_t kTRUE
Definition: RtypesCore.h:91
#define gDirectory
Definition: TDirectory.h:290
void Info(const char *location, const char *msgfmt,...)
Use this function for informational messages.
Definition: TError.cxx:220
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition: TError.cxx:187
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition: TError.cxx:231
R__EXTERN TProofServ * gProofServ
Definition: TProofServ.h:347
@ kReadPermission
Definition: TSystem.h:47
@ kWritePermission
Definition: TSystem.h:46
R__EXTERN TSystem * gSystem
Definition: TSystem.h:559
A TTree is a list of TBranches.
Definition: TBranch.h:89
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all leaves of entry and return total number of bytes read.
Definition: TBranch.cxx:1652
void SetName(const char *name)
Definition: TCollection.h:204
Manages an element of a TDSet.
Definition: TDSet.h:66
Bool_t cd(const char *path=nullptr) override
Change current directory to "this" directory.
TObject * Get(const char *namecycle) override
Return pointer to object identified by namecycle.
Describe directory structure in memory.
Definition: TDirectory.h:45
virtual Bool_t cd(const char *path=nullptr)
Change current directory to "this" directory.
Definition: TDirectory.cxx:504
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:54
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3997
void Close(Option_t *option="") override
Close a file.
Definition: TFile.cxx:879
A doubly linked list.
Definition: TList.h:44
virtual TObject * FindObject(const char *name) const
Find an object in this list using its name.
Definition: TList.cxx:578
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
Collectable string class.
Definition: TObjString.h:28
R__ALWAYS_INLINE Bool_t IsZombie() const
Definition: TObject.h:149
Class used by TMap to store (key,value) pairs.
Definition: TMap.h:102
TObject * Value() const
Definition: TMap.h:121
const char * GetOrdinal() const
Definition: TProofServ.h:253
const char * GetDataDir() const
Definition: TProofServ.h:250
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:27
Basic string class.
Definition: TString.h:136
TString & Insert(Ssiz_t pos, const char *s)
Definition: TString.h:649
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Return true if string ends with the specified string.
Definition: TString.cxx:2197
const char * Data() const
Definition: TString.h:369
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition: TString.h:692
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition: TString.h:615
Bool_t IsNull() const
Definition: TString.h:407
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition: TString.cxx:2331
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:624
virtual const char * Getenv(const char *env)
Get environment variable.
Definition: TSystem.cxx:1661
virtual int mkdir(const char *name, Bool_t recursive=kFALSE)
Make a file system directory.
Definition: TSystem.cxx:905
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition: TSystem.cxx:1294
virtual const char * BaseName(const char *pathname)
Base name of a file name. Base name of /user/root is root.
Definition: TSystem.cxx:933
virtual const char * HostName()
Return the system's host name.
Definition: TSystem.cxx:304
virtual TString GetDirName(const char *pathname)
Return the directory name in pathname.
Definition: TSystem.cxx:1030
A TTree represents a columnar dataset.
Definition: TTree.h:79
virtual Int_t Fill()
Fill all branches.
Definition: TTree.cxx:4567
virtual TBranch * GetBranch(const char *name)
Return pointer to the branch with the given name in this tree or its friends.
Definition: TTree.cxx:5252
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=0)
Change branch address, dealing with clone trees properly.
Definition: TTree.cxx:8332
virtual void SetDirectory(TDirectory *dir)
Change the tree's directory.
Definition: TTree.cxx:8908
virtual Long64_t GetEntries() const
Definition: TTree.h:459
virtual void Print(Option_t *option="") const
Print a summary of the tree contents.
Definition: TTree.cxx:7183
TBranch * Branch(const char *name, T *obj, Int_t bufsize=32000, Int_t splitlevel=99)
Add a new branch, and infer the data type from the type of obj being passed.
Definition: TTree.h:350
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TTree.cxx:9690
This class represents a WWW compatible URL.
Definition: TUrl.h:33
const char * GetProtocol() const
Definition: TUrl.h:64
Double_t y[n]
Definition: legend1.C:17
Double_t x[n]
Definition: legend1.C:17
double T(double x)
Definition: ChebyshevPol.h:34
void Begin(Int_t type)
Double_t Sqrt(Double_t x)
Definition: TMath.h:691
auto * a
Definition: textangle.C:12
Gerardo Ganis (gerar.nosp@m.do.g.nosp@m.anis@.nosp@m.cern.nosp@m..ch)

Definition in file ProofAux.C.