Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:21:37

0001 // This class puts rows of MySimpleTree classes into a TClonesArray
0002 
0003 #include "MyTClonesArray.h"
0004 #include "MySimpleTree.h"
0005 
0006 #include <phool/phool.h>
0007 
0008 #include <TClonesArray.h>
0009 
0010 #include <iostream>
0011 
0012 // DEFAULTSIZE gives the initial number of entries
0013 // the TClonesArray can hold and also the step size
0014 // by which it is extended when needed
0015 // this should be a meaningful number (like what you
0016 // expect in most of the cases) to limit the number of
0017 // times the TClonesArray has to be extended (and shrunk again
0018 // at the end of the event) which eats a bit of cpu time
0019 
0020 static int DEFAULTSIZE = 10;
0021 
0022 MyTClonesArray::MyTClonesArray()
0023 {
0024   MyTCArray = new TClonesArray("MySimpleTree",DEFAULTSIZE );
0025 }
0026 
0027 
0028 MyTClonesArray::~MyTClonesArray()
0029 {
0030   if (MyTCArray)
0031     {
0032       MyTCArray->Clear();
0033       delete MyTCArray;
0034     }
0035   return ;
0036 }
0037 
0038 
0039 void 
0040 MyTClonesArray::Reset()
0041 {
0042   myeventint = 0;
0043   myeventfloat = NAN;
0044   MyTCArray->Clear();
0045   if (MyTCArray->GetSize() > DEFAULTSIZE)
0046     {
0047       MyTCArray->Expand(DEFAULTSIZE);
0048     }
0049   return ;
0050 }
0051 
0052 // this method returns a pointer to the newly generated item
0053 // in the TClonesArray.
0054 // There is a bit TCLonesArray magic involved here but if you
0055 // replace MySimpleTree by your class everywhere you should be fine.
0056 // I once figured out a way to do this even more generic, but I couldn't
0057 // locate this code on short notice (it's somewhere in our phuniverse I guess)
0058 MySimpleTree *
0059 MyTClonesArray::GetNewItem()
0060 {
0061   // in a TClonesArray GetLast returns the last valid index, so add 1 to get
0062   // the one we need to create
0063   TClonesArray &cl = *MyTCArray;
0064   int nextindex = MyTCArray->GetLast()+1;
0065   // check if we are at the current capacity of our TClonesArray and 
0066   // if so extend the TClonesArray
0067   if (nextindex == MyTCArray->GetSize())
0068     {
0069       MyTCArray->Expand(MyTCArray->GetSize() + DEFAULTSIZE);
0070     }
0071   new(cl[nextindex]) MySimpleTree();
0072   return (static_cast<MySimpleTree *> (cl[nextindex]));
0073 }
0074 
0075 MySimpleTree *
0076 MyTClonesArray::GetItem(const unsigned int i) const
0077 {
0078 
0079   if (i > (unsigned int) MyTCArray->GetLast())
0080     {
0081       std::cout << PHWHERE << " Index " << i
0082         << " out of range, max = " << MyTCArray->GetLast() << std::endl;
0083       return 0;
0084     }
0085 
0086   return (static_cast<MySimpleTree *> (MyTCArray->UncheckedAt(i)));
0087 }
0088 
0089 int
0090 MyTClonesArray::Entries()
0091 {
0092   return MyTCArray->GetEntriesFast();
0093 }