HighLAND
HLClonesArray.cxx
1 #include <HLClonesArray.hxx>
2 #include <stdio.h>
3 #include <iostream>
4 
5 
6 //********************************************************************
7 HLClonesArray::HLClonesArray(const std::string& tree_name, const std::string& array_name, const std::string& class_name, const UInt_t size){
8 //********************************************************************
9 
10  // Create a TChain to save the steps into the config tree
11  // _chain = new TChain(tree_name.c_str());
12 
13  _fileName = "";
14 
15  _treeName = tree_name;
16 
17  _NObjects =0;
18  // The ClonesArray to save
19  _objects = new TClonesArray(class_name.c_str(), size);
20 
21  // Name of the TClonesArray in the tree
22  _tcArrayName = array_name;
23 
24  _chain = NULL;
25 }
26 
27 //********************************************************************
28 HLClonesArray::~HLClonesArray(){
29 //********************************************************************
30 
31 // delete _chain;
32 }
33 
34 //********************************************************************
35 void HLClonesArray::ReadClonesArray(const std::string& file){
36 //********************************************************************
37 
38  // Nothing to do if we read the same file
39  if (file == _fileName) return;
40 
41  // the current file
42  _fileName = file;
43 
44  // Reset the vectors
45  _objects->Clear();
46 
47  // Reset to 0, just in case the clones array is not found
48  _NObjects = 0;
49 
50  // Add the file to the chain
51  if (_chain) delete _chain;
52  _chain = new TChain(_treeName.c_str());
53  _chain->AddFile(file.c_str());
54 
55  const std::string& name = GetClonesArrayName();
56 
57  // Set the branch addresses to read the tree
58  _chain->SetBranchAddress(("N"+name).c_str(), &_NObjects);
59  _chain->SetBranchAddress(name.c_str(), &_objects);
60 
61  // Read one entry
62  Long64_t centry = _chain->LoadTree(0);
63  if (centry<0){
64  std::cout << "HLClonesArray::ReadClonesArray(). failed in reading '" << name << "' clones array !!!" << std::endl;
65  }
66 
67  Int_t nb = _chain->GetEntry(0);
68  if (nb==0){
69  std::cout << "HLClonesArray::ReadClonesArray(). failed in reading '" << name << "' clones array !!!" << std::endl;
70  }
71 
72  delete _chain; _chain=NULL;
73 }
74 
75 
76 //********************************************************************
77 void HLClonesArray::WriteClonesArray(TTree& tree){
78 //********************************************************************
79 
80  const std::string& name = GetClonesArrayName();
81 
82  tree.Branch(("N"+name).c_str(), &(GetNObjects()), ("N"+name+"/I").c_str(), 32000);
83  tree.Branch(name.c_str(),"TClonesArray", GetClonesArray(), 32000, 99);
84 }
std::string _fileName
Name of the last file read.
std::string _treeName
Name of the Tree.
TChain * _chain
TChain used to read the "config" tree from the output file.
Int_t & GetNObjects()
Return the number of steps that have been added.
TClonesArray * GetClonesArray()
Int_t _NObjects
The number of steps that were added.
TClonesArray * _objects
std::string _tcArrayName
Name of the TClonesArray in the tree.
const std::string & GetClonesArrayName() const
Return the name of the TClonesArray.