Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TEventList.cxx
Go to the documentation of this file.
1// @(#)root/tree:$Id$
2// Author: Rene Brun 11/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 TEventList
13\ingroup tree
14
15\legacy{TEventList}
16
17A TEventList object is a list of selected events (entries) in a TTree.
18
19A TEventList is automatically generated by TTree::Draw: example
20~~~ {.cpp}
21 tree->Draw(">>elist1","x<0 && y> 0");
22~~~
23In this example, a TEventList object named "elist1" will be
24generated. (Previous contents are overwritten).
25~~~ {.cpp}
26 tree->Draw(">>+elist1","x<0 && y> 0");
27~~~
28In this example, selected entries are added to the list.
29
30The TEventList object is added to the list of objects in the current
31directory.
32
33Use TTree:SetEventList(TEventList *list) to inform TTree that you
34want to use the list as input. The following code gets a pointer to
35the TEventList object created in the above commands:
36~~~ {.cpp}
37 TEventList *list = (TEventList*)gDirectory->Get("elist1");
38~~~
39- Use function Enter to enter an element in the list
40- The function Add may be used to merge two lists.
41- The function Subtract may be used to subtract two lists.
42- The function Reset may be used to reset a list.
43- Use TEventList::Print(option) to print the contents.
44 (option "all" prints all the list entries).
45- Operators + and - correspond to functions Add and Subtract.
46- A TEventList object can be saved on a file via the Write function.
47*/
48
49#include "strlcpy.h"
50#include "TEventList.h"
51#include "TBuffer.h"
52#include "TCut.h"
53#include "TDirectory.h"
54#include "TCollection.h"
55#include "TMathBase.h"
56#include "TROOT.h"
57
58#include <algorithm>
59
60////////////////////////////////////////////////////////////////////////////////
61/// Create a EventList.
62///
63/// This Eventlist is added to the list of objects in current directory.
64
65TEventList::TEventList(const char *name, const char *title, Int_t initsize, Int_t delta)
66 : TNamed(name, title), fSize(std::max(100, initsize)), fDelta(std::max(100, delta))
67{
70 if (fDirectory)
71 fDirectory->Append(this);
72 }
73}
74
75////////////////////////////////////////////////////////////////////////////////
76/// Copy constructor.
77
79 : TNamed(list),
80 fN(list.fN),
81 fSize(list.fSize),
82 fDelta(list.fDelta),
83 fReapply(list.fReapply),
84 fList(new Long64_t[fSize])
85{
86 std::copy(list.fList, list.fList + list.fN, fList);
87}
88
89////////////////////////////////////////////////////////////////////////////////
90/// Default destructor for a EventList.
91
93{
94 delete[] fList;
95 if (fDirectory)
96 fDirectory->Remove(this);
97}
98
99////////////////////////////////////////////////////////////////////////////////
100/// Merge contents of alist with this list.
101///
102/// Both alist and this list are assumed to be sorted prior to this call
103
104void TEventList::Add(const TEventList *alist)
105{
106 Int_t i;
107 Int_t an = alist->GetN();
108 if (!an) return;
109 Long64_t *alst = alist->GetList();
110 if (!fList) {
111 fList = new Long64_t[an];
112 for (i=0;i<an;i++) fList[i] = alst[i];
113 fN = an;
114 fSize = an;
115 return;
116 }
117 Int_t newsize = fN + an;
120 newpos = alpos = 0;
121 for (i=0;i<fN;i++) {
122 while (alpos < an && fList[i] > alst[alpos]) {
124 newpos++;
125 alpos++;
126 }
127 if (alpos < an && fList[i] == alst[alpos]) alpos++;
128 newlist[newpos] = fList[i];
129 newpos++;
130 }
131 while (alpos < an) {
133 newpos++;
134 alpos++;
135 }
136 delete [] fList;
137 fN = newpos;
138 fSize = newsize;
139 fList = newlist;
140
141 TCut orig = GetTitle();
142 TCut added = alist->GetTitle();
143 TCut updated = orig || added;
144 SetTitle(updated.GetTitle());
145}
146
147////////////////////////////////////////////////////////////////////////////////
148/// Return TRUE if list contains entry.
149
151{
152 if (GetIndex(entry) < 0) return false;
153 return true;
154}
155
156////////////////////////////////////////////////////////////////////////////////
157/// Return TRUE if list contains entries from entrymin to entrymax included.
158
160{
162 //printf("ContainsRange: entrymin=%lld, entrymax=%lld,imax=%lld, fList[imax]=%lld\n",entrymin,entrymax,imax,fList[imax]);
163
164 if (fList[imax] < entrymin) return false;
165 return true;
166}
167
168////////////////////////////////////////////////////////////////////////////////
169/// Called by TKey and others to automatically add us to a directory when we are read from a file.
170
175
176////////////////////////////////////////////////////////////////////////////////
177/// Enter element entry into the list.
178
180{
181 if (!fList) {
182 fList = new Long64_t[fSize];
183 fList[0] = entry;
184 fN = 1;
185 return;
186 }
187 if (fN>0 && entry==fList[fN-1]) return;
188 if (fN >= fSize) {
191 }
192 if(fN==0 || entry>fList[fN-1]) {
193 fList[fN] = entry;
194 ++fN;
195 } else {
197 if(pos>=0 && entry==fList[pos])
198 return;
199 ++pos;
200 memmove( &(fList[pos+1]), &(fList[pos]), 8*(fN-pos));
201 fList[pos] = entry;
202 ++fN;
203 }
204}
205
206////////////////////////////////////////////////////////////////////////////////
207/// Return value of entry at index in the list.
208/// Return -1 if index is not in the list range.
209
211{
212 if (!fList) return -1;
213 if (index < 0 || index >= fN) return -1;
214 return fList[index];
215}
216
217////////////////////////////////////////////////////////////////////////////////
218/// Return index in the list of element with value entry
219/// array is supposed to be sorted prior to this call.
220/// If match is found, function returns position of element.
221/// If no match found, function returns -1.
222
224{
226 nabove = fN+1;
227 nbelow = 0;
228 while(nabove-nbelow > 1) {
229 middle = (nabove+nbelow)/2;
230 if (entry == fList[middle-1]) return middle-1;
231 if (entry < fList[middle-1]) nabove = middle;
232 else nbelow = middle;
233 }
234 return -1;
235}
236
237////////////////////////////////////////////////////////////////////////////////
238/// Remove elements from this list that are NOT present in alist.
239
241{
242 if (!alist) return;
243 if (!fList) return;
244
245 Long64_t *newlist = new Long64_t[fN];
246 Int_t newpos = 0;
247 Int_t i;
248 for (i=0;i<fN;i++) {
249 if (alist->GetIndex(fList[i]) >= 0) {
250 newlist[newpos] = fList[i];
251 newpos++;
252 }
253 }
254 delete [] fList;
255 fN = newpos;
256 fList = newlist;
257
258 TCut orig = GetTitle();
259 TCut removed = alist->GetTitle();
261 SetTitle(updated.GetTitle());
262}
263
264////////////////////////////////////////////////////////////////////////////////
265/// Merge entries in all the TEventList in the collection in this event list.
266
268{
269 if (!list) return -1;
270 TIter next(list);
271
272 //first loop to count the number of entries
273 TEventList *el;
274 Int_t nevents = 0;
275 while ((el = (TEventList*)next())) {
276 if (!el->InheritsFrom(TEventList::Class())) {
277 Error("Add","Attempt to add object of class: %s to a %s",el->ClassName(),this->ClassName());
278 return -1;
279 }
280 Add(el);
281 nevents += el->GetN();
282 }
283
284 return nevents;
285}
286
287////////////////////////////////////////////////////////////////////////////////
288/// Print contents of this list.
289
291{
292 printf("EventList:%s/%s, number of entries =%d, size=%d\n",GetName(),GetTitle(),fN,fSize);
293 if (!strstr(option,"all")) return;
294 Int_t i;
295 Int_t nbuf = 0;
296 char element[10];
297 char *line = new char[100];
298 snprintf(line,100,"%5d : ",0);
299 for (i=0;i<fN;i++) {
300 nbuf++;
301 if (nbuf > 10) {
302 printf("%s\n",line);
303 snprintf(line,100,"%5d : ",i);
304 nbuf = 1;
305 }
306 snprintf(element,10,"%7lld ",fList[i]);
307 strlcat(line,element,100);
308 }
309 if (nbuf) printf("%s\n",line);
310 delete [] line;
311}
312
313////////////////////////////////////////////////////////////////////////////////
314/// Reset number of entries in event list.
315
317{
318 fN = 0;
319}
320
321////////////////////////////////////////////////////////////////////////////////
322/// Resize list by delta entries.
323
325{
326 if (!delta) delta = fDelta;
327 fSize += delta;
329 for (Int_t i=0;i<fN;i++) newlist[i] = fList[i];
330 delete [] fList;
331 fList = newlist;
332}
333
334////////////////////////////////////////////////////////////////////////////////
335/// Remove reference to this EventList from current directory and add
336/// reference to new directory dir. dir can be 0 in which case the list
337/// does not belong to any directory.
338
340{
341 if (fDirectory == dir) return;
342 if (fDirectory) fDirectory->Remove(this);
343 fDirectory = dir;
344 if (fDirectory) fDirectory->Append(this);
345}
346
347////////////////////////////////////////////////////////////////////////////////
348/// Change the name of this TEventList.
349
350void TEventList::SetName(const char *name)
351{
352 // TEventLists are named objects in a THashList.
353 // We must update the hashlist if we change the name
354 if (fDirectory) fDirectory->Remove(this);
355 fName = name;
356 if (fDirectory) fDirectory->Append(this);
357}
358
359////////////////////////////////////////////////////////////////////////////////
360/// Sort list entries in increasing order
361
363{
364 Int_t *index = new Int_t[fN];
366 Int_t i,ind;
367 TMath::Sort(fN,fList,index); //sort in decreasing order
368 for (i=0;i<fN;i++) {
369 ind = index[fN-i-1];
370 newlist[i] = fList[ind];
371 }
372 for (i=fN;i<fSize;i++) {
373 newlist[i] = 0;
374 }
375 delete [] index;
376 delete [] fList;
377 fList = newlist;
378}
379
380////////////////////////////////////////////////////////////////////////////////
381/// Stream an object of class TEventList.
382
384{
385 if (b.IsReading()) {
387 Version_t R__v = b.ReadVersion(&R__s, &R__c);
388 fDirectory = nullptr;
389 if (R__v > 1) {
390 b.ReadClassBuffer(TEventList::Class(), this, R__v, R__s, R__c);
392 return;
393 }
394 //====process old versions before automatic schema evolution
396 b >> fN;
397 b >> fSize;
398 b >> fDelta;
399 if (fN) {
400 Int_t *tlist = new Int_t[fSize];
401 b.ReadFastArray(tlist,fN);
402 fList = new Long64_t[fSize];
403 for (Int_t i=0;i<fN;i++) fList[i] = tlist[i];
404 delete [] tlist;
405 }
407 b.CheckByteCount(R__s, R__c, TEventList::IsA());
408 //====end of old versions
409
410 } else {
411 b.WriteClassBuffer(TEventList::Class(), this);
412 }
413}
414
415////////////////////////////////////////////////////////////////////////////////
416/// Remove elements from this list that are present in alist.
417
419{
420 if (!alist) return;
421 if (!fList) return;
422
423 Long64_t *newlist = new Long64_t[fN];
424 Int_t newpos = 0;
425 Int_t i;
426 for (i=0;i<fN;i++) {
427 if (alist->GetIndex(fList[i]) < 0) {
428 newlist[newpos] = fList[i];
429 newpos++;
430 }
431 }
432 delete [] fList;
433 fN = newpos;
434 fList = newlist;
435
436 TCut orig = GetTitle();
437 TCut removed = alist->GetTitle();
438 TCut updated = orig && !removed;
439 SetTitle(updated.GetTitle());
440}
441
442////////////////////////////////////////////////////////////////////////////////
443/// Assingment.
444
446{
447 if (this != &list) {
448 TNamed::operator=(list);
449 if (fSize < list.fSize) {
450 delete [] fList;
451 fList = new Long64_t[list.fSize];
452 }
453 fN = list.fN;
454 fSize = list.fSize;
455 fDelta = list.fDelta;
456 for (Int_t i=0; i<fN; i++)
457 fList[i] = list.fList[i];
458 }
459 return *this;
460}
461
462////////////////////////////////////////////////////////////////////////////////
463/// Addition.
464
466{
467 TEventList newlist = list1;
468 newlist.Add(&list2);
469 return newlist;
470}
471
472////////////////////////////////////////////////////////////////////////////////
473/// Subtraction
474
476{
477 TEventList newlist = list1;
478 newlist.Subtract(&list2);
479 return newlist;
480}
481
482////////////////////////////////////////////////////////////////////////////////
483/// Intersection.
484
486{
487 TEventList newlist = list1;
488 newlist.Intersect(&list2);
489 return newlist;
490}
491
dim_t fSize
#define b(i)
Definition RSha256.hxx:100
short Version_t
Class version identifier (short)
Definition RtypesCore.h:80
long long Long64_t
Portable signed long integer 8 bytes.
Definition RtypesCore.h:84
const char Option_t
Option string (const char)
Definition RtypesCore.h:81
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define gDirectory
Definition TDirectory.h:385
TEventList operator-(const TEventList &list1, const TEventList &list2)
Subtraction.
TEventList operator+(const TEventList &list1, const TEventList &list2)
Addition.
TEventList operator*(const TEventList &list1, const TEventList &list2)
Intersection.
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
char name[80]
Definition TGX11.cxx:148
Buffer base class used for serializing objects.
Definition TBuffer.h:43
Collection abstract base class.
Definition TCollection.h:65
A specialized string object used for TTree selections.
Definition TCut.h:25
Describe directory structure in memory.
Definition TDirectory.h:45
virtual void Append(TObject *obj, Bool_t replace=kFALSE)
Append object to this directory.
virtual TObject * Remove(TObject *)
Remove an object from the in-memory list.
<div class="legacybox"><h2>Legacy Code</h2> TEventList is a legacy interface: there will be no bug fi...
Definition TEventList.h:31
Long64_t * fList
[fN]Array of elements
Definition TEventList.h:38
static TClass * Class()
~TEventList() override
Default destructor for a EventList.
void Streamer(TBuffer &) override
Stream an object of class TEventList.
TEventList()=default
virtual void Reset(Option_t *option="")
Reset number of entries in event list.
virtual bool ContainsRange(Long64_t entrymin, Long64_t entrymax)
Return TRUE if list contains entries from entrymin to entrymax included.
TDirectory * fDirectory
! Pointer to directory holding this tree
Definition TEventList.h:39
virtual Long64_t GetEntry(Int_t index) const
Return value of entry at index in the list.
virtual Int_t GetIndex(Long64_t entry) const
Return index in the list of element with value entry array is supposed to be sorted prior to this cal...
virtual Int_t GetN() const
Definition TEventList.h:56
virtual Long64_t * GetList() const
Definition TEventList.h:55
TClass * IsA() const override
Definition TEventList.h:77
virtual Int_t Merge(TCollection *list)
Merge entries in all the TEventList in the collection in this event list.
virtual void DirectoryAutoAdd(TDirectory *)
Called by TKey and others to automatically add us to a directory when we are read from a file.
virtual void Subtract(const TEventList *list)
Remove elements from this list that are present in alist.
void Print(Option_t *option="") const override
Print contents of this list.
virtual void SetDirectory(TDirectory *dir)
Remove reference to this EventList from current directory and add reference to new directory dir.
virtual void Add(const TEventList *list)
Merge contents of alist with this list.
Int_t fN
Number of elements in the list.
Definition TEventList.h:34
virtual void Resize(Int_t delta=0)
Resize list by delta entries.
virtual void Enter(Long64_t entry)
Enter element entry into the list.
Int_t fSize
Size of array.
Definition TEventList.h:35
virtual void Sort()
Sort list entries in increasing order.
virtual bool Contains(Long64_t entry)
Return TRUE if list contains entry.
TEventList & operator=(const TEventList &list)
Assingment.
virtual void Intersect(const TEventList *list)
Remove elements from this list that are NOT present in alist.
Int_t fDelta
Increment size.
Definition TEventList.h:36
void SetName(const char *name) override
Change the name of this TEventList.
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:173
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
void Streamer(TBuffer &) override
Stream an object of class TObject.
const char * GetTitle() const override
Returns title of object.
Definition TNamed.h:50
TString fName
Definition TNamed.h:32
TNamed & operator=(const TNamed &rhs)
TNamed assignment operator.
Definition TNamed.cxx:50
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1096
void ResetBit(UInt_t f)
Definition TObject.h:203
@ kMustCleanup
if object destructor must call RecursiveRemove()
Definition TObject.h:73
TLine * line
bool ObjectAutoRegistrationEnabled()
Test whether objects in this thread auto-register themselves, e.g.
Definition TROOT.cxx:776
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Definition TMathBase.h:249
void Sort(Index n, const Element *a, Index *index, Bool_t down=kTRUE)
Sort the n elements of the array a of generic templated type Element.
Definition TMathBase.h:413
Long64_t BinarySearch(Long64_t n, const T *array, T value)
Binary search in an array of n values to locate value.
Definition TMathBase.h:329