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
57#include <algorithm>
58
59////////////////////////////////////////////////////////////////////////////////
60/// Create a EventList.
61///
62/// This Eventlist is added to the list of objects in current directory.
63
64TEventList::TEventList(const char *name, const char *title, Int_t initsize, Int_t delta)
65 : TNamed(name, title), fSize(std::max(100, initsize)), fDelta(std::max(100, delta))
66{
68 if (fDirectory)
69 fDirectory->Append(this);
70}
71
72////////////////////////////////////////////////////////////////////////////////
73/// Copy constructor.
74
76 : TNamed(list),
77 fN(list.fN),
78 fSize(list.fSize),
79 fDelta(list.fDelta),
80 fReapply(list.fReapply),
81 fList(new Long64_t[fSize])
82{
83 std::copy(list.fList, list.fList + list.fN, fList);
84}
85
86////////////////////////////////////////////////////////////////////////////////
87/// Default destructor for a EventList.
88
90{
91 delete[] fList;
92 if (fDirectory)
93 fDirectory->Remove(this);
94}
95
96////////////////////////////////////////////////////////////////////////////////
97/// Merge contents of alist with this list.
98///
99/// Both alist and this list are assumed to be sorted prior to this call
100
101void TEventList::Add(const TEventList *alist)
102{
103 Int_t i;
104 Int_t an = alist->GetN();
105 if (!an) return;
106 Long64_t *alst = alist->GetList();
107 if (!fList) {
108 fList = new Long64_t[an];
109 for (i=0;i<an;i++) fList[i] = alst[i];
110 fN = an;
111 fSize = an;
112 return;
113 }
114 Int_t newsize = fN + an;
117 newpos = alpos = 0;
118 for (i=0;i<fN;i++) {
119 while (alpos < an && fList[i] > alst[alpos]) {
121 newpos++;
122 alpos++;
123 }
124 if (alpos < an && fList[i] == alst[alpos]) alpos++;
125 newlist[newpos] = fList[i];
126 newpos++;
127 }
128 while (alpos < an) {
130 newpos++;
131 alpos++;
132 }
133 delete [] fList;
134 fN = newpos;
135 fSize = newsize;
136 fList = newlist;
137
138 TCut orig = GetTitle();
139 TCut added = alist->GetTitle();
140 TCut updated = orig || added;
141 SetTitle(updated.GetTitle());
142}
143
144////////////////////////////////////////////////////////////////////////////////
145/// Return TRUE if list contains entry.
146
148{
149 if (GetIndex(entry) < 0) return false;
150 return true;
151}
152
153////////////////////////////////////////////////////////////////////////////////
154/// Return TRUE if list contains entries from entrymin to entrymax included.
155
157{
159 //printf("ContainsRange: entrymin=%lld, entrymax=%lld,imax=%lld, fList[imax]=%lld\n",entrymin,entrymax,imax,fList[imax]);
160
161 if (fList[imax] < entrymin) return false;
162 return true;
163}
164
165////////////////////////////////////////////////////////////////////////////////
166/// Called by TKey and others to automatically add us to a directory when we are read from a file.
167
172
173////////////////////////////////////////////////////////////////////////////////
174/// Enter element entry into the list.
175
177{
178 if (!fList) {
179 fList = new Long64_t[fSize];
180 fList[0] = entry;
181 fN = 1;
182 return;
183 }
184 if (fN>0 && entry==fList[fN-1]) return;
185 if (fN >= fSize) {
188 }
189 if(fN==0 || entry>fList[fN-1]) {
190 fList[fN] = entry;
191 ++fN;
192 } else {
194 if(pos>=0 && entry==fList[pos])
195 return;
196 ++pos;
197 memmove( &(fList[pos+1]), &(fList[pos]), 8*(fN-pos));
198 fList[pos] = entry;
199 ++fN;
200 }
201}
202
203////////////////////////////////////////////////////////////////////////////////
204/// Return value of entry at index in the list.
205/// Return -1 if index is not in the list range.
206
208{
209 if (!fList) return -1;
210 if (index < 0 || index >= fN) return -1;
211 return fList[index];
212}
213
214////////////////////////////////////////////////////////////////////////////////
215/// Return index in the list of element with value entry
216/// array is supposed to be sorted prior to this call.
217/// If match is found, function returns position of element.
218/// If no match found, function returns -1.
219
221{
223 nabove = fN+1;
224 nbelow = 0;
225 while(nabove-nbelow > 1) {
226 middle = (nabove+nbelow)/2;
227 if (entry == fList[middle-1]) return middle-1;
228 if (entry < fList[middle-1]) nabove = middle;
229 else nbelow = middle;
230 }
231 return -1;
232}
233
234////////////////////////////////////////////////////////////////////////////////
235/// Remove elements from this list that are NOT present in alist.
236
238{
239 if (!alist) return;
240 if (!fList) return;
241
242 Long64_t *newlist = new Long64_t[fN];
243 Int_t newpos = 0;
244 Int_t i;
245 for (i=0;i<fN;i++) {
246 if (alist->GetIndex(fList[i]) >= 0) {
247 newlist[newpos] = fList[i];
248 newpos++;
249 }
250 }
251 delete [] fList;
252 fN = newpos;
253 fList = newlist;
254
255 TCut orig = GetTitle();
256 TCut removed = alist->GetTitle();
258 SetTitle(updated.GetTitle());
259}
260
261////////////////////////////////////////////////////////////////////////////////
262/// Merge entries in all the TEventList in the collection in this event list.
263
265{
266 if (!list) return -1;
267 TIter next(list);
268
269 //first loop to count the number of entries
270 TEventList *el;
271 Int_t nevents = 0;
272 while ((el = (TEventList*)next())) {
273 if (!el->InheritsFrom(TEventList::Class())) {
274 Error("Add","Attempt to add object of class: %s to a %s",el->ClassName(),this->ClassName());
275 return -1;
276 }
277 Add(el);
278 nevents += el->GetN();
279 }
280
281 return nevents;
282}
283
284////////////////////////////////////////////////////////////////////////////////
285/// Print contents of this list.
286
288{
289 printf("EventList:%s/%s, number of entries =%d, size=%d\n",GetName(),GetTitle(),fN,fSize);
290 if (!strstr(option,"all")) return;
291 Int_t i;
292 Int_t nbuf = 0;
293 char element[10];
294 char *line = new char[100];
295 snprintf(line,100,"%5d : ",0);
296 for (i=0;i<fN;i++) {
297 nbuf++;
298 if (nbuf > 10) {
299 printf("%s\n",line);
300 snprintf(line,100,"%5d : ",i);
301 nbuf = 1;
302 }
303 snprintf(element,10,"%7lld ",fList[i]);
304 strlcat(line,element,100);
305 }
306 if (nbuf) printf("%s\n",line);
307 delete [] line;
308}
309
310////////////////////////////////////////////////////////////////////////////////
311/// Reset number of entries in event list.
312
314{
315 fN = 0;
316}
317
318////////////////////////////////////////////////////////////////////////////////
319/// Resize list by delta entries.
320
322{
323 if (!delta) delta = fDelta;
324 fSize += delta;
326 for (Int_t i=0;i<fN;i++) newlist[i] = fList[i];
327 delete [] fList;
328 fList = newlist;
329}
330
331////////////////////////////////////////////////////////////////////////////////
332/// Remove reference to this EventList from current directory and add
333/// reference to new directory dir. dir can be 0 in which case the list
334/// does not belong to any directory.
335
337{
338 if (fDirectory == dir) return;
339 if (fDirectory) fDirectory->Remove(this);
340 fDirectory = dir;
341 if (fDirectory) fDirectory->Append(this);
342}
343
344////////////////////////////////////////////////////////////////////////////////
345/// Change the name of this TEventList.
346
347void TEventList::SetName(const char *name)
348{
349 // TEventLists are named objects in a THashList.
350 // We must update the hashlist if we change the name
351 if (fDirectory) fDirectory->Remove(this);
352 fName = name;
353 if (fDirectory) fDirectory->Append(this);
354}
355
356////////////////////////////////////////////////////////////////////////////////
357/// Sort list entries in increasing order
358
360{
361 Int_t *index = new Int_t[fN];
363 Int_t i,ind;
364 TMath::Sort(fN,fList,index); //sort in decreasing order
365 for (i=0;i<fN;i++) {
366 ind = index[fN-i-1];
367 newlist[i] = fList[ind];
368 }
369 for (i=fN;i<fSize;i++) {
370 newlist[i] = 0;
371 }
372 delete [] index;
373 delete [] fList;
374 fList = newlist;
375}
376
377////////////////////////////////////////////////////////////////////////////////
378/// Stream an object of class TEventList.
379
381{
382 if (b.IsReading()) {
384 Version_t R__v = b.ReadVersion(&R__s, &R__c);
385 fDirectory = nullptr;
386 if (R__v > 1) {
387 b.ReadClassBuffer(TEventList::Class(), this, R__v, R__s, R__c);
389 return;
390 }
391 //====process old versions before automatic schema evolution
393 b >> fN;
394 b >> fSize;
395 b >> fDelta;
396 if (fN) {
397 Int_t *tlist = new Int_t[fSize];
398 b.ReadFastArray(tlist,fN);
399 fList = new Long64_t[fSize];
400 for (Int_t i=0;i<fN;i++) fList[i] = tlist[i];
401 delete [] tlist;
402 }
404 b.CheckByteCount(R__s, R__c, TEventList::IsA());
405 //====end of old versions
406
407 } else {
408 b.WriteClassBuffer(TEventList::Class(), this);
409 }
410}
411
412////////////////////////////////////////////////////////////////////////////////
413/// Remove elements from this list that are present in alist.
414
416{
417 if (!alist) return;
418 if (!fList) return;
419
420 Long64_t *newlist = new Long64_t[fN];
421 Int_t newpos = 0;
422 Int_t i;
423 for (i=0;i<fN;i++) {
424 if (alist->GetIndex(fList[i]) < 0) {
425 newlist[newpos] = fList[i];
426 newpos++;
427 }
428 }
429 delete [] fList;
430 fN = newpos;
431 fList = newlist;
432
433 TCut orig = GetTitle();
434 TCut removed = alist->GetTitle();
435 TCut updated = orig && !removed;
436 SetTitle(updated.GetTitle());
437}
438
439////////////////////////////////////////////////////////////////////////////////
440/// Assingment.
441
443{
444 if (this != &list) {
445 TNamed::operator=(list);
446 if (fSize < list.fSize) {
447 delete [] fList;
448 fList = new Long64_t[list.fSize];
449 }
450 fN = list.fN;
451 fSize = list.fSize;
452 fDelta = list.fDelta;
453 for (Int_t i=0; i<fN; i++)
454 fList[i] = list.fList[i];
455 }
456 return *this;
457}
458
459////////////////////////////////////////////////////////////////////////////////
460/// Addition.
461
463{
464 TEventList newlist = list1;
465 newlist.Add(&list2);
466 return newlist;
467}
468
469////////////////////////////////////////////////////////////////////////////////
470/// Subtraction
471
473{
474 TEventList newlist = list1;
475 newlist.Subtract(&list2);
476 return newlist;
477}
478
479////////////////////////////////////////////////////////////////////////////////
480/// Intersection.
481
483{
484 TEventList newlist = list1;
485 newlist.Intersect(&list2);
486 return newlist;
487}
488
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
#define snprintf
Definition civetweb.c:1579
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
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