ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  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  fChain(0), fSelect(0), fSelectedRows(0), fSelectMultiple(kFALSE)
59 {
61 }
62 
63 ////////////////////////////////////////////////////////////////////////////////
64 /// Destructor.
65 
67 {
68  delete fSelect; fSelect = 0;
69 }
70 
71 ////////////////////////////////////////////////////////////////////////////////
72 /// The Begin() function is called at the start of the query.
73 /// When running with PROOF Begin() is only called on the client.
74 /// The tree argument is deprecated (on PROOF 0 is passed).
75 
77 {
78  TString option = GetOption();
79  fChain = tree;
80 }
81 
82 ////////////////////////////////////////////////////////////////////////////////
83 /// The SlaveBegin() function is called after the Begin() function.
84 /// When running with PROOF SlaveBegin() is called on each slave server.
85 /// The tree argument is deprecated (on PROOF 0 is passed).
86 
88 {
89  fChain = tree;
90  TString option = GetOption();
91 
92  SetStatus(0);
93  fSelectedRows = 0;
94  TObject *selectObj = fInput->FindObject("selection");
95  const char *selection = selectObj ? selectObj->GetTitle() : "";
96 
97  if (strlen(selection)) {
98  fSelect = new TTreeFormula("Selection",selection,fChain);
100  if (!fSelect->GetNdim()) {delete fSelect; fSelect = 0; return; }
101  }
103 
105 }
106 
107 ////////////////////////////////////////////////////////////////////////////////
108 /// Read entry.
109 
111 {
112  return fChain ? fChain->GetTree()->GetEntry(entry, getall) : 0;
113 }
114 
115 ////////////////////////////////////////////////////////////////////////////////
116 /// The Init() function is called when the selector needs to initialize
117 /// a new tree or chain. Typically here the branch addresses and branch
118 /// pointers of the tree will be set.
119 /// It is normaly not necessary to make changes to the generated
120 /// code, but the routine can be extended by the user if needed.
121 /// Init() will be called many times when running on PROOF
122 /// (once per file to be processed).
123 
124 void TSelectorEntries::Init(TTree * /* tree */)
125 {
126 }
127 
128 ////////////////////////////////////////////////////////////////////////////////
129 /// This function is called at the first entry of a new tree in a chain.
130 
132 {
134  return kTRUE;
135 }
136 
137 ////////////////////////////////////////////////////////////////////////////////
138 /// The Process() function is called for each entry in the tree (or possibly
139 /// keyed object in the case of PROOF) to be processed. The entry argument
140 /// specifies which entry in the currently loaded tree is to be processed.
141 /// It can be passed to either TSelectorEntries::GetEntry() or TBranch::GetEntry()
142 /// to read either all or the required parts of the data. When processing
143 /// keyed objects with PROOF, the object is already loaded and is available
144 /// via the fObject pointer.
145 ///
146 /// This function should contain the "body" of the analysis. It can contain
147 /// simple or elaborate selection criteria, run algorithms on the data
148 /// of the event and typically fill histograms.
149 ///
150 /// The processing can be stopped by calling Abort().
151 ///
152 /// Use fStatus to set the return value of TTree::Process().
153 ///
154 /// The return value is currently not used.
155 
157 {
158  if (!fSelectMultiple) {
159  if (fSelect) {
160  if ( fSelect->EvalInstance(0) ) {
161  ++fSelectedRows;
162  }
163  } else {
164  ++fSelectedRows;
165  }
166  } else if (fSelect) {
167  // Grab the array size of the formulas for this entry
169 
170  // No data at all, let's move on to the next entry.
171  if (!ndata) return kTRUE;
172 
173  // Calculate the first values
174  // Always call EvalInstance(0) to insure the loading
175  // of the branches.
176  if (fSelect->EvalInstance(0)) {
177  ++fSelectedRows;
178  } else {
179  for (Int_t i=1;i<ndata;i++) {
180  if (fSelect->EvalInstance(i)) {
181  ++fSelectedRows;
182  break;
183  }
184  }
185  }
186  }
187  return kTRUE;
188 }
189 
190 ////////////////////////////////////////////////////////////////////////////////
191 /// Set the selection expression.
192 
193 void TSelectorEntries::SetSelection(const char *selection)
194 {
195  if (!fInput) {
196  fInput = new TList;
197  }
198  TNamed *cselection = (TNamed*)fInput->FindObject("selection");
199  if (!cselection) {
200  cselection = new TNamed("selection","");
201  fInput->Add(cselection);
202  }
203  cselection->SetTitle(selection);
204 }
205 
206 ////////////////////////////////////////////////////////////////////////////////
207 /// The SlaveTerminate() function is called after all entries or objects
208 /// have been processed. When running with PROOF SlaveTerminate() is called
209 /// on each slave server.
210 
212 {
213  fOutput->Add(new TSelectorScalar("fSelectedRows",fSelectedRows));
214 }
215 
216 ////////////////////////////////////////////////////////////////////////////////
217 /// The Terminate() function is the last function to be called during
218 /// a query. It always runs on the client, it can be used to present
219 /// the results graphically or save the results to file.
220 
222 {
223  TSelectorScalar* rows = (TSelectorScalar*)fOutput->FindObject("fSelectedRows");
224  if (rows)
225  {
226  fSelectedRows = rows->GetVal();
227  } else {
228  Error("Terminate","fSelectedRows is missing in fOutput");
229  }
230 }
const int ndata
virtual const char * GetOption() const
Definition: TSelector.h:65
TSelectorList * fOutput
Definition: TSelector.h:50
long long Long64_t
Definition: RtypesCore.h:69
TObject * FindObject(const char *name) const
Find object using its name.
Definition: THashList.cxx:213
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:5144
Basic string class.
Definition: TString.h:137
virtual Int_t GetNdim() const
Definition: TFormula.h:243
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
virtual TObject * FindObject(const char *name) const
Find an object in this list using its name.
Definition: TList.cxx:497
virtual ~TSelectorEntries()
Destructor.
Named scalar type, based on Long64_t, streamable, storable and mergable.
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 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:33
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
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:64
A doubly linked list.
Definition: TList.h:47
TTreeFormula * fSelect
pointer to the analyzed TTree or TChain
virtual void SetStatus(Long64_t status)
Definition: TSelector.h:74
virtual void SetSelection(const char *selection)
Set the selection expression.
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...
Long64_t entry
virtual Int_t GetNdata()
Return number of available instances in the formula.
tuple tree
Definition: tree.py:24
virtual TTree * GetTree() const
Definition: TTree.h:436
T EvalInstance(Int_t i=0, const char *stringStack[]=0)
Evaluate this treeformula.
Mother of all ROOT objects.
Definition: TObject.h:58
TList * fInput
Current object if processing object (vs. TTree)
Definition: TSelector.h:49
virtual void Add(TObject *obj)
Definition: TList.h:81
virtual void Terminate()
The Terminate() function is the last function to be called during a query.
A TTree object has a header with a name and a title.
Definition: TTree.h:98
void SetQuickLoad(Bool_t quick)
Definition: TTreeFormula.h:202
void ResetBit(UInt_t f)
Definition: TObject.h:172
const AParamType & GetVal() const
Definition: TParameter.h:77
const Bool_t kTRUE
Definition: Rtypes.h:91
virtual Int_t GetMultiplicity() const
Definition: TTreeFormula.h:186
virtual const char * GetTitle() const
Returns title of object.
Definition: TObject.cxx:459
virtual void SetTitle(const char *title="")
Change (i.e. set) the title of the TNamed.
Definition: TNamed.cxx:152