Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
13The class is derived from the ROOT class TSelector. For more
14information on the TSelector framework see
15$ROOTSYS/README/README.SELECTOR or the ROOT User Manual.
16
17The following methods are defined in this file:
18
19 - Begin(): called every time 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
30To 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
47 fOwnInput(kFALSE), fChain(tree), fSelect(0), fSelectedRows(0), fSelectMultiple(kFALSE)
48{
49 if (selection && selection[0]) {
51 }
52}
53
54////////////////////////////////////////////////////////////////////////////////
55/// Constructor.
56
57TSelectorEntries::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
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) ) {
167 }
168 } else {
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)) {
183 } else {
184 for (Int_t i=1;i<ndata;i++) {
185 if (fSelect->EvalInstance(i)) {
187 break;
188 }
189 }
190 }
191 }
192 return kTRUE;
193}
194
195////////////////////////////////////////////////////////////////////////////////
196/// Set the selection expression.
197
198void TSelectorEntries::SetSelection(const char *selection)
199{
200 if (!fInput) {
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}
const Bool_t kFALSE
Definition RtypesCore.h:101
long long Long64_t
Definition RtypesCore.h:80
const Bool_t kTRUE
Definition RtypesCore.h:100
virtual Int_t GetNdim() const
Definition TFormula.h:237
TObject * FindObject(const char *name) const
Find object using its name.
A doubly linked list.
Definition TList.h:38
virtual void Add(TObject *obj)
Definition TList.h:81
virtual TObject * FindObject(const char *name) const
Find an object in this list using its name.
Definition TList.cxx:578
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition TList.cxx:470
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition TNamed.cxx:164
Mother of all ROOT objects.
Definition TObject.h:41
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:963
virtual const char * GetTitle() const
Returns title of object.
Definition TObject.cxx:473
void ResetBit(UInt_t f)
Definition TObject.h:200
const AParamType & GetVal() const
Definition TParameter.h:67
Bool_t fSelectMultiple
True if selection has a variable index.
TTree * fChain
! Pointer to the analyzed TTree or TChain
virtual Int_t GetEntry(Long64_t entry, Int_t getall=0)
Read entry.
Bool_t fOwnInput
True if we created the input list.
TTreeFormula * fSelect
Pointer to selection formula.
virtual void Init(TTree *tree)
The Init() function is called when the selector needs to initialize a new tree or chain.
virtual Bool_t Notify()
This function is called at the first entry of a new tree in a chain.
virtual void SetSelection(const char *selection)
Set the selection expression.
virtual ~TSelectorEntries()
Destructor.
virtual void Begin(TTree *tree)
The Begin() function is called at the start of the query.
virtual void Terminate()
The Terminate() function is the last function to be called during a query.
TSelectorEntries(TTree *tree=0, const char *selection=0)
Default, constructor.
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 ...
Long64_t fSelectedRows
Number of selected entries.
virtual void SlaveBegin(TTree *tree)
The SlaveBegin() function is called after the Begin() function.
virtual void SlaveTerminate()
The SlaveTerminate() function is called after all entries or objects have been processed.
Named scalar type, based on Long64_t, streamable, storable and mergeable.
virtual void SetStatus(Long64_t status)
Definition TSelector.h:67
TList * fInput
List of objects available during processing.
Definition TSelector.h:41
TSelectorList * fOutput
! List of objects created during processing
Definition TSelector.h:42
virtual const char * GetOption() const
Definition TSelector.h:57
Basic string class.
Definition TString.h:136
Used to pass a selection expression to the Tree drawing routine.
T EvalInstance(Int_t i=0, const char *stringStack[]=0)
Evaluate this treeformula.
virtual Int_t GetMultiplicity() const
virtual void UpdateFormulaLeaves()
This function is called TTreePlayer::UpdateFormulaLeaves, itself called by TChain::LoadTree when a ne...
virtual Int_t GetNdata()
Return number of available instances in the formula.
void SetQuickLoad(Bool_t quick)
A TTree represents a columnar dataset.
Definition TTree.h:79
virtual Int_t GetEntry(Long64_t entry, Int_t getall=0)
Read all branches of entry and return total number of bytes read.
Definition TTree.cxx:5622
virtual TTree * GetTree() const
Definition TTree.h:514
@ kForceRead
Definition TTree.h:248
Definition tree.py:1