Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TSelector.cxx
Go to the documentation of this file.
1// @(#)root/tree:$Id$
2// Author: Rene Brun 05/02/97
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 TSelector
13\ingroup tree
14
15A TSelector object is used by the TTree::Draw, TTree::Scan,
16TTree::Process to navigate in a TTree and make selections.
17It contains the following main methods:
18
19- void TSelector::Init(TTree *t). Called every time a new TTree is
20 attached.
21
22- void TSelector::SlaveBegin(). Create e.g. histograms in this method.
23 This method is called before looping on the
24 entries in the Tree.
25
26- void TSelector::Begin(). Mostly for backward compatibility; use
27 SlaveBegin() instead. Both methods are called before looping on the
28 entries in the Tree.
29
30- bool TSelector::Notify(). This method is called at the first entry
31 of a new file in a chain.
32
33- bool TSelector::Process(Long64_t entry). This method is called
34 to process an entry. It is the user's responsibility to read
35 the corresponding entry in memory (may be just a partial read).
36 Once the entry is in memory one can apply a selection and if the
37 entry is selected histograms can be filled. Processing stops
38 when this function returns false. This function combines the
39 next two functions in one, avoiding to have to maintain state
40 in the class to communicate between these two functions.
41 See WARNING below about entry.
42
43- bool TSelector::ProcessCut(Long64_t entry). This method is called
44 before processing entry. It is the user's responsibility to read
45 the corresponding entry in memory (may be just a partial read).
46 The function returns true if the entry must be processed,
47 false otherwise. This method is obsolete, use Process().
48 See WARNING below about entry.
49
50- void TSelector::ProcessFill(Long64_t entry). This method is called
51 for all selected entries. User fills histograms in this function.
52 This method is obsolete, use Process().
53 See WARNING below about entry.
54
55- void TSelector::Terminate(). This method is called at the end of
56 the loop on all entries. Typically one performs the fits on the
57 produced histograms or write the histograms to file in this method.
58
59__WARNING when a selector is used with a TChain:__
60
61in the Process, ProcessCut, ProcessFill function, you must use
62the pointer to the current Tree to call `GetEntry(entry)`.
63entry is always the local entry number in the current tree.
64Assuming that fChain is the pointer to the TChain being processed,
65use `fChain->GetTree()->GetEntry(entry);`
66*/
67
68#include "TROOT.h"
69#include "TSystem.h"
70#include "TTree.h"
71#include "TError.h"
72#include "TSelector.h"
73#include "TClass.h"
74#include "TInterpreter.h"
75
77
78////////////////////////////////////////////////////////////////////////////////
79/// Default selector ctor.
80
82{
83 fStatus = 0;
85 fObject = nullptr;
86 fInput = nullptr;
89}
90
91////////////////////////////////////////////////////////////////////////////////
92/// Selector destructor.
93
95{
96 delete fOutput;
97}
98
99////////////////////////////////////////////////////////////////////////////////
100/// Abort processing. If what = kAbortProcess, the Process() loop will be
101/// aborted. If what = kAbortFile, the current file in a chain will be
102/// aborted and the processing will continue with the next file, if there
103/// is no next file then Process() will be aborted. Abort() can also be
104/// called from Begin(), SlaveBegin(), Init() and Notify(). After abort
105/// the Terminate() method is always called. The abort flag
106/// can be checked in these methods using GetAbort().
107
108void TSelector::Abort(const char *why, EAbort what)
109{
110 fAbort = what;
111 TString mess = "Abort";
112 if (fAbort == kAbortProcess)
113 mess = "AbortProcess";
114 else if (fAbort == kAbortFile)
115 mess = "AbortFile";
116
117 Info(mess, "%s", why);
118}
119
120////////////////////////////////////////////////////////////////////////////////
121/// The code in filename is loaded (interpreted or compiled, see below),
122/// filename must contain a valid class implementation derived from TSelector.
123///
124/// If filename is of the form file.C, the file will be interpreted.
125/// If filename is of the form file.C++, the file file.C will be compiled
126/// and dynamically loaded. The corresponding binary file and shared
127/// library will be deleted at the end of the function.
128/// If filename is of the form file.C+, the file file.C will be compiled
129/// and dynamically loaded. At next call, if file.C is older than file.o
130/// and file.so, the file.C is not compiled, only file.so is loaded.
131///
132/// The static function returns a pointer to a TSelector object
133
135{
136 // If the filename does not contain "." assume class is compiled in
138 bool fromFile = false;
139 if (strchr(filename, '.') != nullptr) {
140 //Interpret/compile filename via CINT
141 localname = ".L ";
143 gROOT->ProcessLine(localname);
144 fromFile = true;
145 }
146
147 //loop on all classes known to CINT to find the class on filename
148 //that derives from TSelector
149 const char *basename = gSystem->BaseName(filename);
150 if (!basename) {
151 ::Error("TSelector::GetSelector","unable to determine the classname for file %s", filename);
152 return nullptr;
153 }
154 TString aclicmode,args,io;
156 if (localname.Last('.') != kNPOS)
157 localname.Remove(localname.Last('.'));
158
159 // if a file was not specified, try to load the class via the interpreter;
160 // this returns 0 (== failure) in the case the class is already in memory
161 // but does not have a dictionary, so we just raise a flag for better
162 // diagnostic in the case the class is not found in the CINT ClassInfo table.
163 bool autoloaderr = false;
164 if (!fromFile && gCling->AutoLoad(localname) != 1)
165 autoloaderr = true;
166
168 if (selCl) {
169 // We have all we need.
170 auto offset = selCl->GetBaseClassOffset(TSelector::Class());
171 if (offset == -1) {
172 // TSelector is not a based class.
173 if (fromFile)
174 ::Error("TSelector::GetSelector",
175 "The class %s in file %s does not derive from TSelector.", localname.Data(), filename);
176 else if (autoloaderr)
177 ::Error("TSelector::GetSelector", "class %s could not be loaded", filename);
178 else
179 ::Error("TSelector::GetSelector",
180 "class %s does not exist or does not derive from TSelector", filename);
181 return nullptr;
182 }
183 char *result = (char*)selCl->New();
184 // By adding offset, we support the case where TSelector is not the
185 // "left-most" base class (i.e. offset != 0)
186 return (TSelector*)(result+offset);
187
188 } else {
190 bool ok = false;
191 bool nameFound = false;
192 if (cl && gCling->ClassInfo_IsValid(cl)) {
193 if (localname == gCling->ClassInfo_FullName(cl)) {
194 nameFound = true;
195 if (gCling->ClassInfo_IsBase(cl,"TSelector")) ok = true;
196 }
197 }
198 if (!ok) {
199 if (fromFile) {
200 if (nameFound) {
201 ::Error("TSelector::GetSelector",
202 "The class %s in file %s does not derive from TSelector.", localname.Data(), filename);
203 } else {
204 ::Error("TSelector::GetSelector",
205 "The file %s does not define a class named %s.", filename, localname.Data());
206 }
207 } else {
208 if (autoloaderr)
209 ::Error("TSelector::GetSelector", "class %s could not be loaded", filename);
210 else
211 ::Error("TSelector::GetSelector",
212 "class %s does not exist or does not derive from TSelector", filename);
213 }
215 return nullptr;
216 }
217
218 // we can now create an instance of the class
219 TSelector *selector = (TSelector*)gCling->ClassInfo_New(cl);
221 return selector;
222 }
223}
224
225////////////////////////////////////////////////////////////////////////////////
226/// Find out if this is a standard selection used for Draw actions
227/// (either TSelectorDraw or deriving from it).
228
230{
231 // Make sure we have a name
232 if (!selec) {
233 ::Info("TSelector::IsStandardDraw",
234 "selector name undefined - do nothing");
235 return false;
236 }
237
238 bool stdselec = false;
239 if (!strchr(selec, '.')) {
240 if (strstr(selec, "TSelectorDraw")) {
241 stdselec = true;
242 } else {
244 if (cl && (cl->InheritsFrom("TSelectorDraw")))
245 stdselec = true;
246 }
247 }
248
249 // We are done
250 return stdselec;
251}
252
253////////////////////////////////////////////////////////////////////////////////
254/// Imports the content of 'output' in the internal output list. Existing content
255/// in the output list is discarded (unless found also in 'output').
256/// In particular, if 'output' is nullptr or empty, reset the internal list.
257/// On return, the content of 'output' is cleared to avoid double deletion issues.
258/// (The caller is responsible of 'output' as container: its content is transferred
259/// under the selector ownership).
260
262
263 // Reset the list, if required
264 if (!output || output->GetSize() <= 0) {
265 fOutput->Delete();
266 return;
267 }
268
269 TObject *o;
270
271 // Remove from new list objects already existing locally
273 while ((o = nxexo())) {
274 if (output->FindObject(o)) output->Remove(o);
275 }
276
277 // Transfer remaining objects
279 while ((o = nxo())) {
280 fOutput->Add(o);
281 }
282
283 // Cleanup original list
284 output->SetOwner(false);
285 output->Clear("nodelete");
286
287 // Done
288 return;
289}
290
291////////////////////////////////////////////////////////////////////////////////
292/// This method is called before processing entry. It is the user's responsibility to read
293/// the corresponding entry in memory (may be just a partial read).
294/// The function returns true if the entry must be processed,
295/// false otherwise. This method is obsolete, use Process().
296///
297/// WARNING when a selector is used with a TChain:
298/// in the Process, ProcessCut, ProcessFill function, you must use
299/// the pointer to the current Tree to call GetEntry(entry).
300/// entry is always the local entry number in the current tree.
301/// Assuming that fChain is the pointer to the TChain being processed,
302/// use fChain->GetTree()->GetEntry(entry);
303
305{
306
307 return true;
308}
309
310////////////////////////////////////////////////////////////////////////////////
311/// This method is called for all selected entries. User fills histograms
312/// in this function. This method is obsolete, use Process().
313///
314/// WARNING when a selector is used with a TChain:
315/// in the Process, ProcessCut, ProcessFill function, you must use
316/// the pointer to the current Tree to call GetEntry(entry).
317/// entry is always the local entry number in the current tree.
318/// Assuming that fChain is the pointer to the TChain being processed,
319/// use fChain->GetTree()->GetEntry(entry);
320
322{
323
324}
325
326////////////////////////////////////////////////////////////////////////////////
327/// The Process() function is called for each entry in the tree to be processed.
328/// The entry argument specifies which entry in the currently loaded
329/// tree is to be processed.
330/// It can be passed to either t01::GetEntry() or TBranch::GetEntry()
331/// to read either all or the required parts of the data.
332///
333/// This function should contain the "body" of the analysis. It can contain
334/// simple or elaborate selection criteria, run algorithms on the data
335/// of the event and typically fill histograms.
336///
337/// The processing can be stopped by calling Abort().
338///
339/// Use fStatus to set the return value of TTree::Process().
340///
341/// The return value is currently not used.
342///
343/// WARNING when a selector is used with a TChain, you must use
344/// the pointer to the current TTree to call GetEntry(entry).
345/// The entry is always the local entry number in the current tree.
346/// Assuming that fChain is the pointer to the TChain being processed,
347/// use: `fChain->GetTree()->GetEntry(entry)`.
348
350
351 return false;
352}
constexpr Ssiz_t kNPOS
The equivalent of std::string::npos for the ROOT class TString.
Definition RtypesCore.h:131
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:83
#define ClassImp(name)
Definition Rtypes.h:376
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
R__EXTERN TInterpreter * gCling
#define gROOT
Definition TROOT.h:411
R__EXTERN TSystem * gSystem
Definition TSystem.h:572
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:84
Bool_t InheritsFrom(const char *cl) const override
Return kTRUE if this class inherits from a class with name "classname".
Definition TClass.cxx:4902
static TClass * GetClass(const char *name, Bool_t load=kTRUE, Bool_t silent=kFALSE)
Static method returning pointer to TClass of the specified class name.
Definition TClass.cxx:2974
virtual void SetOwner(Bool_t enable=kTRUE)
Set whether this collection is the owner (enable==true) of its content.
void Delete(Option_t *option="") override
Remove all objects from the list AND delete all heap based objects.
virtual void * ClassInfo_New(ClassInfo_t *) const
virtual Bool_t ClassInfo_IsValid(ClassInfo_t *) const
virtual void ClassInfo_Delete(ClassInfo_t *) const
virtual Bool_t ClassInfo_IsBase(ClassInfo_t *, const char *) const
virtual const char * ClassInfo_FullName(ClassInfo_t *) const
virtual Int_t AutoLoad(const char *classname, Bool_t knowDictNotLoaded=kFALSE)=0
virtual ClassInfo_t * ClassInfo_Factory(Bool_t=kTRUE) const =0
A doubly linked list.
Definition TList.h:38
void Add(TObject *obj) override
Definition TList.h:81
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:1072
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition TObject.cxx:1046
A TList derived class that makes sure that objects added to it are not linked to the currently open f...
A TSelector object is used by the TTree::Draw, TTree::Scan, TTree::Process to navigate in a TTree and...
Definition TSelector.h:31
static bool IsStandardDraw(const char *selec)
Find out if this is a standard selection used for Draw actions (either TSelectorDraw or deriving from...
TList * fInput
List of objects available during processing.
Definition TSelector.h:41
virtual void ProcessFill(Long64_t)
This method is called for all selected entries.
@ kAbortProcess
Definition TSelector.h:34
virtual bool ProcessCut(Long64_t)
This method is called before processing entry.
virtual bool Process(Long64_t)
The Process() function is called for each entry in the tree to be processed.
virtual void ImportOutput(TList *output)
Imports the content of 'output' in the internal output list.
TSelectorList * fOutput
! List of objects created during processing
Definition TSelector.h:42
Long64_t fStatus
Selector status.
Definition TSelector.h:37
static TClass * Class()
virtual void Abort(const char *why, EAbort what=kAbortProcess)
Abort processing.
TObject * fObject
! Current object if processing object (vs. TTree)
Definition TSelector.h:40
~TSelector() override
Selector destructor.
Definition TSelector.cxx:94
EAbort fAbort
Abort status.
Definition TSelector.h:38
TSelector()
Default selector ctor.
Definition TSelector.cxx:81
static TSelector * GetSelector(const char *filename)
The code in filename is loaded (interpreted or compiled, see below), filename must contain a valid cl...
Basic string class.
Definition TString.h:138
virtual TString SplitAclicMode(const char *filename, TString &mode, TString &args, TString &io) const
This method split a filename of the form:
Definition TSystem.cxx:4284
virtual const char * BaseName(const char *pathname)
Base name of a file name. Base name of /user/root is root.
Definition TSystem.cxx:946
static const char * what
Definition stlLoader.cc:5
static void output()