Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files 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  fOwnInput(kFALSE), fChain(tree), fSelect(0), fSelectedRows(0), fSelectMultiple(kFALSE)
48 {
49  if (selection && selection[0]) {
50  TSelectorEntries::SetSelection(selection);
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 {
60  TSelectorEntries::SetSelection(selection);
61 }
62 
63 ////////////////////////////////////////////////////////////////////////////////
64 /// Destructor.
65 
66 TSelectorEntries::~TSelectorEntries()
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 
81 void TSelectorEntries::Begin(TTree *tree)
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 
92 void TSelectorEntries::SlaveBegin(TTree *tree)
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);
104  fSelect->SetQuickLoad(kTRUE);
105  if (!fSelect->GetNdim()) {delete fSelect; fSelect = 0; return; }
106  }
107  if (fSelect && fSelect->GetMultiplicity()) fSelectMultiple = kTRUE;
108 
109  fChain->ResetBit(TTree::kForceRead);
110 }
111 
112 ////////////////////////////////////////////////////////////////////////////////
113 /// Read entry.
114 
115 Int_t TSelectorEntries::GetEntry(Long64_t entry, Int_t getall)
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 
136 Bool_t TSelectorEntries::Notify()
137 {
138  if (fSelect) fSelect->UpdateFormulaLeaves();
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 
161 Bool_t TSelectorEntries::Process(Long64_t /* entry */)
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 
217 void TSelectorEntries::SlaveTerminate()
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 
227 void TSelectorEntries::Terminate()
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 }