Logo ROOT   master
Reference Guide
TSelectorEntries.cxx
Go to the documentation of this file.
1 // @(#)root/treeplayer:$Id$
2 // Author: Philippe Canal 09/06/2006
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 /** \class TSelectorEntries
13 The class is derived from the ROOT class TSelector. For more
14 information on the TSelector framework see
15 $ROOTSYS/README/README.SELECTOR or the ROOT User Manual.
16 
17 The following methods are defined in this file:
18 
19  - Begin(): called everytime a loop on the tree starts,
20  a convenient place to create your histograms.
21  - SlaveBegin(): called after Begin(), when on PROOF called only on the
22  slave servers.
23  - Process(): called for each event, in this function you decide what
24  to read and fill your histograms.
25  - SlaveTerminate: called at the end of the loop on the tree, when on PROOF
26  called only on the slave servers.
27  - Terminate(): called at the end of the loop on the tree,
28  a convenient place to draw/fit your histograms.
29 
30 To use this file, try the following session on your Tree T:
31 ~~~{.cpp}
32  Root > T->Process("TSelectorEntries.C")
33  Root > T->Process("TSelectorEntries.C","some options")
34  Root > T->Process("TSelectorEntries.C+")
35 ~~~
36 */
37 
38 #include "TSelectorEntries.h"
39 #include "TTree.h"
40 #include "TTreeFormula.h"
41 #include "TSelectorScalar.h"
42 
43 ////////////////////////////////////////////////////////////////////////////////
44 /// Default, constructor.
45 
46 TSelectorEntries::TSelectorEntries(TTree *tree, const char *selection) :
47  fOwnInput(kFALSE), fChain(tree), fSelect(0), fSelectedRows(0), fSelectMultiple(kFALSE)
48 {
49  if (selection && selection[0]) {
51  }
52 }
53 
54 ////////////////////////////////////////////////////////////////////////////////
55 /// Constructor.
56 
57 TSelectorEntries::TSelectorEntries(const char *selection) :
58  fOwnInput(kFALSE), fChain(0), fSelect(0), fSelectedRows(0), fSelectMultiple(kFALSE)
59 {
61 }
62 
63 ////////////////////////////////////////////////////////////////////////////////
64 /// Destructor.
65 
67 {
68  delete fSelect; fSelect = nullptr;
69  if (fOwnInput) {
70  fInput->Delete();
71  delete fInput;
72  fInput = nullptr;
73  }
74 }
75 
76 ////////////////////////////////////////////////////////////////////////////////
77 /// The Begin() function is called at the start of the query.
78 /// When running with PROOF Begin() is only called on the client.
79 /// The tree argument is deprecated (on PROOF 0 is passed).
80 
82 {
83  TString option = GetOption();
84  fChain = tree;
85 }
86 
87 ////////////////////////////////////////////////////////////////////////////////
88 /// The SlaveBegin() function is called after the Begin() function.
89 /// When running with PROOF SlaveBegin() is called on each slave server.
90 /// The tree argument is deprecated (on PROOF 0 is passed).
91 
93 {
94  fChain = tree;
95  TString option = GetOption();
96 
97  SetStatus(0);
98  fSelectedRows = 0;
99  TObject *selectObj = fInput->FindObject("selection");
100  const char *selection = selectObj ? selectObj->GetTitle() : "";
101 
102  if (strlen(selection)) {
103  fSelect = new TTreeFormula("Selection",selection,fChain);
105  if (!fSelect->GetNdim()) {delete fSelect; fSelect = 0; return; }
106  }
108 
110 }
111 
112 ////////////////////////////////////////////////////////////////////////////////
113 /// Read entry.
114 
116 {
117  return fChain ? fChain->GetTree()->GetEntry(entry, getall) : 0;
118 }
119 
120 ////////////////////////////////////////////////////////////////////////////////
121 /// The Init() function is called when the selector needs to initialize
122 /// a new tree or chain. Typically here the branch addresses and branch
123 /// pointers of the tree will be set.
124 /// It is normally not necessary to make changes to the generated
125 /// code, but the routine can be extended by the user if needed.
126 /// Init() will be called many times when running on PROOF
127 /// (once per file to be processed).
128 
129 void TSelectorEntries::Init(TTree * /* tree */)
130 {
131 }
132 
133 ////////////////////////////////////////////////////////////////////////////////
134 /// This function is called at the first entry of a new tree in a chain.
135 
137 {
139  return kTRUE;
140 }
141 
142 ////////////////////////////////////////////////////////////////////////////////
143 /// The Process() function is called for each entry in the tree (or possibly
144 /// keyed object in the case of PROOF) to be processed. The entry argument
145 /// specifies which entry in the currently loaded tree is to be processed.
146 /// It can be passed to either TSelectorEntries::GetEntry() or TBranch::GetEntry()
147 /// to read either all or the required parts of the data. When processing
148 /// keyed objects with PROOF, the object is already loaded and is available
149 /// via the fObject pointer.
150 ///
151 /// This function should contain the "body" of the analysis. It can contain
152 /// simple or elaborate selection criteria, run algorithms on the data
153 /// of the event and typically fill histograms.
154 ///
155 /// The processing can be stopped by calling Abort().
156 ///
157 /// Use fStatus to set the return value of TTree::Process().
158 ///
159 /// The return value is currently not used.
160 
162 {
163  if (!fSelectMultiple) {
164  if (fSelect) {
165  if ( fSelect->EvalInstance(0) ) {
166  ++fSelectedRows;
167  }
168  } else {
169  ++fSelectedRows;
170  }
171  } else if (fSelect) {
172  // Grab the array size of the formulas for this entry
173  Int_t ndata = fSelect->GetNdata();
174 
175  // No data at all, let's move on to the next entry.
176  if (!ndata) return kTRUE;
177 
178  // Calculate the first values
179  // Always call EvalInstance(0) to insure the loading
180  // of the branches.
181  if (fSelect->EvalInstance(0)) {
182  ++fSelectedRows;
183  } else {
184  for (Int_t i=1;i<ndata;i++) {
185  if (fSelect->EvalInstance(i)) {
186  ++fSelectedRows;
187  break;
188  }
189  }
190  }
191  }
192  return kTRUE;
193 }
194 
195 ////////////////////////////////////////////////////////////////////////////////
196 /// Set the selection expression.
197 
198 void TSelectorEntries::SetSelection(const char *selection)
199 {
200  if (!fInput) {
201  fOwnInput = kTRUE;
202  fInput = new TList;
203  }
204  TNamed *cselection = (TNamed*)fInput->FindObject("selection");
205  if (!cselection) {
206  cselection = new TNamed("selection","");
207  fInput->Add(cselection);
208  }
209  cselection->SetTitle(selection);
210 }
211 
212 ////////////////////////////////////////////////////////////////////////////////
213 /// The SlaveTerminate() function is called after all entries or objects
214 /// have been processed. When running with PROOF SlaveTerminate() is called
215 /// on each slave server.
216 
218 {
219  fOutput->Add(new TSelectorScalar("fSelectedRows",fSelectedRows));
220 }
221 
222 ////////////////////////////////////////////////////////////////////////////////
223 /// The Terminate() function is the last function to be called during
224 /// a query. It always runs on the client, it can be used to present
225 /// the results graphically or save the results to file.
226 
228 {
229  TSelectorScalar* rows = (TSelectorScalar*)fOutput->FindObject("fSelectedRows");
230  if (rows)
231  {
232  fSelectedRows = rows->GetVal();
233  } else {
234  Error("Terminate","fSelectedRows is missing in fOutput");
235  }
236 }
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition: TList.cxx:469
TSelectorList * fOutput
! List of objects created during processing
Definition: TSelector.h:44
long long Long64_t
Definition: RtypesCore.h:71
TSelectorEntries(TTree *tree=0, const char *selection=0)
Default, constructor.
virtual Int_t GetEntry(Long64_t entry=0, Int_t getall=0)
Read all branches of entry and return total number of bytes read.
Definition: TTree.cxx:5509
Basic string class.
Definition: TString.h:131
virtual ~TSelectorEntries()
Destructor.
Named scalar type, based on Long64_t, streamable, storable and mergable.
TObject * FindObject(const char *name) const
Find object using its name.
Definition: THashList.cxx:262
virtual Bool_t Process(Long64_t entry)
The Process() function is called for each entry in the tree (or possibly keyed object in the case of ...
virtual void Init(TTree *tree)
The Init() function is called when the selector needs to initialize a new tree or chain...
virtual void Begin(TTree *tree)
The Begin() function is called at the start of the query.
virtual Int_t GetNdim() const
Definition: TFormula.h:237
virtual TObject * FindObject(const char *name) const
Find an object in this list using its name.
Definition: TList.cxx:577
virtual void SlaveTerminate()
The SlaveTerminate() function is called after all entries or objects have been processed.
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
virtual const char * GetOption() const
Definition: TSelector.h:59
virtual Int_t GetEntry(Long64_t entry, Int_t getall=0)
Read entry.
virtual void SlaveBegin(TTree *tree)
The SlaveBegin() function is called after the Begin() function.
Used to pass a selection expression to the Tree drawing routine.
Definition: TTreeFormula.h:58
virtual TTree * GetTree() const
Definition: TTree.h:508
virtual Int_t GetMultiplicity() const
Definition: TTreeFormula.h:191
A doubly linked list.
Definition: TList.h:44
TTreeFormula * fSelect
pointer to the analyzed TTree or TChain
virtual void SetStatus(Long64_t status)
Definition: TSelector.h:69
virtual void SetSelection(const char *selection)
Set the selection expression.
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:888
virtual Bool_t Notify()
This function is called at the first entry of a new tree in a chain.
virtual void UpdateFormulaLeaves()
This function is called TTreePlayer::UpdateFormulaLeaves, itself called by TChain::LoadTree when a ne...
const Bool_t kFALSE
Definition: RtypesCore.h:90
virtual Int_t GetNdata()
Return number of available instances in the formula.
T EvalInstance(Int_t i=0, const char *stringStack[]=0)
Evaluate this treeformula.
Mother of all ROOT objects.
Definition: TObject.h:37
virtual const char * GetTitle() const
Returns title of object.
Definition: TObject.cxx:401
TList * fInput
List of objects available during processing.
Definition: TSelector.h:43
virtual void Add(TObject *obj)
Definition: TList.h:87
virtual void Terminate()
The Terminate() function is the last function to be called during a query.
Definition: tree.py:1
A TTree represents a columnar dataset.
Definition: TTree.h:78
const AParamType & GetVal() const
Definition: TParameter.h:69
void SetQuickLoad(Bool_t quick)
Definition: TTreeFormula.h:207
void ResetBit(UInt_t f)
Definition: TObject.h:171
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:164
const Bool_t kTRUE
Definition: RtypesCore.h:89