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
76
77////////////////////////////////////////////////////////////////////////////////
78/// Default selector ctor.
79
81{
82 fStatus = 0;
84 fObject = nullptr;
85 fInput = nullptr;
88}
89
90////////////////////////////////////////////////////////////////////////////////
91/// Selector destructor.
92
94{
95 delete fOutput;
96}
97
98////////////////////////////////////////////////////////////////////////////////
99/// Abort processing. If what = kAbortProcess, the Process() loop will be
100/// aborted. If what = kAbortFile, the current file in a chain will be
101/// aborted and the processing will continue with the next file, if there
102/// is no next file then Process() will be aborted. Abort() can also be
103/// called from Begin(), SlaveBegin(), Init() and Notify(). After abort
104/// the Terminate() method is always called. The abort flag
105/// can be checked in these methods using GetAbort().
106
107void TSelector::Abort(const char *why, EAbort what)
108{
109 fAbort = what;
110 TString mess = "Abort";
111 if (fAbort == kAbortProcess)
112 mess = "AbortProcess";
113 else if (fAbort == kAbortFile)
114 mess = "AbortFile";
115
116 Info(mess, "%s", why);
117}
118
119////////////////////////////////////////////////////////////////////////////////
120/// The code in filename is loaded (interpreted or compiled, see below),
121/// filename must contain a valid class implementation derived from TSelector.
122///
123/// If filename is of the form file.C, the file will be interpreted.
124/// If filename is of the form file.C++, the file file.C will be compiled
125/// and dynamically loaded. The corresponding binary file and shared
126/// library will be deleted at the end of the function.
127/// If filename is of the form file.C+, the file file.C will be compiled
128/// and dynamically loaded. At next call, if file.C is older than file.o
129/// and file.so, the file.C is not compiled, only file.so is loaded.
130///
131/// The static function returns a pointer to a TSelector object
132
134{
135 // If the filename does not contain "." assume class is compiled in
137 bool fromFile = false;
138 if (strchr(filename, '.') != nullptr) {
139 //Interpret/compile filename via CINT
140 localname = ".L ";
142 gROOT->ProcessLine(localname);
143 fromFile = true;
144 }
145
146 //loop on all classes known to CINT to find the class on filename
147 //that derives from TSelector
148 const char *basename = gSystem->BaseName(filename);
149 if (!basename) {
150 ::Error("TSelector::GetSelector","unable to determine the classname for file %s", filename);
151 return nullptr;
152 }
153 TString aclicmode,args,io;
155 if (localname.Last('.') != kNPOS)
156 localname.Remove(localname.Last('.'));
157
158 // if a file was not specified, try to load the class via the interpreter;
159 // this returns 0 (== failure) in the case the class is already in memory
160 // but does not have a dictionary, so we just raise a flag for better
161 // diagnostic in the case the class is not found in the CINT ClassInfo table.
162 bool autoloaderr = false;
163 if (!fromFile && gCling->AutoLoad(localname) != 1)
164 autoloaderr = true;
165
167 if (selCl) {
168 // We have all we need.
169 auto offset = selCl->GetBaseClassOffset(TSelector::Class());
170 if (offset == -1) {
171 // TSelector is not a based class.
172 if (fromFile)
173 ::Error("TSelector::GetSelector",
174 "The class %s in file %s does not derive from TSelector.", localname.Data(), filename);
175 else if (autoloaderr)
176 ::Error("TSelector::GetSelector", "class %s could not be loaded", filename);
177 else
178 ::Error("TSelector::GetSelector",
179 "class %s does not exist or does not derive from TSelector", filename);
180 return nullptr;
181 }
182 char *result = (char*)selCl->New();
183 // By adding offset, we support the case where TSelector is not the
184 // "left-most" base class (i.e. offset != 0)
185 return (TSelector*)(result+offset);
186
187 } else {
189 bool ok = false;
190 bool nameFound = false;
191 if (cl && gCling->ClassInfo_IsValid(cl)) {
192 if (localname == gCling->ClassInfo_FullName(cl)) {
193 nameFound = true;
194 if (gCling->ClassInfo_IsBase(cl,"TSelector")) ok = true;
195 }
196 }
197 if (!ok) {
198 if (fromFile) {
199 if (nameFound) {
200 ::Error("TSelector::GetSelector",
201 "The class %s in file %s does not derive from TSelector.", localname.Data(), filename);
202 } else {
203 ::Error("TSelector::GetSelector",
204 "The file %s does not define a class named %s.", filename, localname.Data());
205 }
206 } else {
207 if (autoloaderr)
208 ::Error("TSelector::GetSelector", "class %s could not be loaded", filename);
209 else
210 ::Error("TSelector::GetSelector",
211 "class %s does not exist or does not derive from TSelector", filename);
212 }
214 return nullptr;
215 }
216
217 // we can now create an instance of the class
218 TSelector *selector = (TSelector*)gCling->ClassInfo_New(cl);
220 return selector;
221 }
222}
223
224////////////////////////////////////////////////////////////////////////////////
225/// Find out if this is a standard selection used for Draw actions
226/// (either TSelectorDraw or deriving from it).
227
229{
230 // Make sure we have a name
231 if (!selec) {
232 ::Info("TSelector::IsStandardDraw",
233 "selector name undefined - do nothing");
234 return false;
235 }
236
237 bool stdselec = false;
238 if (!strchr(selec, '.')) {
239 if (strstr(selec, "TSelectorDraw")) {
240 stdselec = true;
241 } else {
243 if (cl && (cl->InheritsFrom("TSelectorDraw")))
244 stdselec = true;
245 }
246 }
247
248 // We are done
249 return stdselec;
250}
251
252////////////////////////////////////////////////////////////////////////////////
253/// Imports the content of 'output' in the internal output list. Existing content
254/// in the output list is discarded (unless found also in 'output').
255/// In particular, if 'output' is nullptr or empty, reset the internal list.
256/// On return, the content of 'output' is cleared to avoid double deletion issues.
257/// (The caller is responsible of 'output' as container: its content is transferred
258/// under the selector ownership).
259
261
262 // Reset the list, if required
263 if (!output || output->GetSize() <= 0) {
264 fOutput->Delete();
265 return;
266 }
267
268 TObject *o;
269
270 // Remove from new list objects already existing locally
272 while ((o = nxexo())) {
273 if (output->FindObject(o)) output->Remove(o);
274 }
275
276 // Transfer remaining objects
278 while ((o = nxo())) {
279 fOutput->Add(o);
280 }
281
282 // Cleanup original list
283 output->SetOwner(false);
284 output->Clear("nodelete");
285
286 // Done
287 return;
288}
289
290////////////////////////////////////////////////////////////////////////////////
291/// This method is called before processing entry. It is the user's responsibility to read
292/// the corresponding entry in memory (may be just a partial read).
293/// The function returns true if the entry must be processed,
294/// false otherwise. This method is obsolete, use Process().
295///
296/// WARNING when a selector is used with a TChain:
297/// in the Process, ProcessCut, ProcessFill function, you must use
298/// the pointer to the current Tree to call GetEntry(entry).
299/// entry is always the local entry number in the current tree.
300/// Assuming that fChain is the pointer to the TChain being processed,
301/// use fChain->GetTree()->GetEntry(entry);
302
304{
305
306 return true;
307}
308
309////////////////////////////////////////////////////////////////////////////////
310/// This method is called for all selected entries. User fills histograms
311/// in this function. This method is obsolete, use Process().
312///
313/// WARNING when a selector is used with a TChain:
314/// in the Process, ProcessCut, ProcessFill function, you must use
315/// the pointer to the current Tree to call GetEntry(entry).
316/// entry is always the local entry number in the current tree.
317/// Assuming that fChain is the pointer to the TChain being processed,
318/// use fChain->GetTree()->GetEntry(entry);
319
321{
322
323}
324
325////////////////////////////////////////////////////////////////////////////////
326/// The Process() function is called for each entry in the tree to be processed.
327/// The entry argument specifies which entry in the currently loaded
328/// tree is to be processed.
329/// It can be passed to either t01::GetEntry() or TBranch::GetEntry()
330/// to read either all or the required parts of the data.
331///
332/// This function should contain the "body" of the analysis. It can contain
333/// simple or elaborate selection criteria, run algorithms on the data
334/// of the event and typically fill histograms.
335///
336/// The processing can be stopped by calling Abort().
337///
338/// Use fStatus to set the return value of TTree::Process().
339///
340/// The return value is currently not used.
341///
342/// WARNING when a selector is used with a TChain, you must use
343/// the pointer to the current TTree to call GetEntry(entry).
344/// The entry is always the local entry number in the current tree.
345/// Assuming that fChain is the pointer to the TChain being processed,
346/// use: `fChain->GetTree()->GetEntry(entry)`.
347
349
350 return false;
351}
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
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:4901
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:2973
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:1071
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
Definition TObject.cxx:1045
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:93
EAbort fAbort
Abort status.
Definition TSelector.h:38
TSelector()
Default selector ctor.
Definition TSelector.cxx:80
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:4282
virtual const char * BaseName(const char *pathname)
Base name of a file name. Base name of /user/root is root.
Definition TSystem.cxx:944
static const char * what
Definition stlLoader.cc:5
static void output()