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