EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MyTClonesArray.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file MyTClonesArray.cc
1 // This class puts rows of MySimpleTree classes into a TClonesArray
2 
3 #include "MyTClonesArray.h"
4 #include "MySimpleTree.h"
5 
6 #include <phool/phool.h>
7 
8 #include <TClonesArray.h>
9 #include <iostream>
10 
12 
13 using namespace std;
14 
15 // DEFAULTSIZE gives the initial number of entries
16 // the TClonesArray can hold and also the step size
17 // by which it is extended when needed
18 // this should be a meaningful number (like what you
19 // expect in most of the cases) to limit the number of
20 // times the TClonesArray has to be extended (and shrunk again
21 // at the end of the event) which eats a bit of cpu time
22 
23 static int DEFAULTSIZE = 10;
24 
26  myeventint(0),
27  myeventfloat(NAN)
28 {
29  MyTCArray = new TClonesArray("MySimpleTree",DEFAULTSIZE );
30 }
31 
32 
34 {
35  if (MyTCArray)
36  {
37  MyTCArray->Clear();
38  delete MyTCArray;
39  }
40  return ;
41 }
42 
43 
44 void
46 {
47  myeventint = 0;
48  myeventfloat = NAN;
49  MyTCArray->Clear();
50  if (MyTCArray->GetSize() > DEFAULTSIZE)
51  {
52  MyTCArray->Expand(DEFAULTSIZE);
53  }
54  return ;
55 }
56 
57 // this method returns a pointer to the newly generated item
58 // in the TClonesArray.
59 // There is a bit TCLonesArray magic involved here but if you
60 // replace MySimpleTree by your class everywhere you should be fine.
61 // I once figured out a way to do this even more generic, but I couldn't
62 // locate this code on short notice (it's somewhere in our phuniverse I guess)
65 {
66  // in a TClonesArray GetLast returns the last valid index, so add 1 to get
67  // the one we need to create
68  TClonesArray &cl = *MyTCArray;
69  int nextindex = MyTCArray->GetLast()+1;
70  // check if we are at the current capacity of our TClonesArray and
71  // if so extend the TClonesArray
72  if (nextindex == MyTCArray->GetSize())
73  {
74  MyTCArray->Expand(MyTCArray->GetSize() + DEFAULTSIZE);
75  }
76  new(cl[nextindex]) MySimpleTree();
77  return (static_cast<MySimpleTree *> (cl[nextindex]));
78 }
79 
81 MyTClonesArray::GetItem(const unsigned int i) const
82 {
83 
84  if (i > (unsigned int) MyTCArray->GetLast())
85  {
86  cout << PHWHERE << " Index " << i
87  << " out of range, max = " << MyTCArray->GetLast() << endl;
88  return 0;
89  }
90 
91  return (static_cast<MySimpleTree *> (MyTCArray->UncheckedAt(i)));
92 }
93 
94 int
96 {
97  return MyTCArray->GetEntriesFast();
98 }