laura is hosted by Hepforge, IPPP Durham
Laura++  v1r0
A maximum likelihood fitting package for performing Dalitz-plot analysis.
LauGenNtuple.cc
Go to the documentation of this file.
1 
2 // Copyright University of Warwick 2006 - 2013.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 // Authors:
7 // Thomas Latham
8 // John Back
9 // Paul Harrison
10 
15 #include <iostream>
16 using std::cout;
17 using std::cerr;
18 using std::endl;
19 
20 #include "TFile.h"
21 #include "TTree.h"
22 
23 #include "LauGenNtuple.hh"
24 
25 ClassImp(LauGenNtuple)
26 
27 
28 LauGenNtuple::LauGenNtuple(const TString& rootFileName, const TString& rootTreeName) :
29  rootFileName_(rootFileName),
30  rootTreeName_(rootTreeName),
31  rootFile_(0),
32  rootTree_(0),
33  definedBranches_(kFALSE)
34 {
35  this->createFileAndTree();
36 }
37 
39 {
40  // seems that closing the file deletes the tree
41  // so only delete if the file is still open for some reason
42  if (rootFile_ && rootFile_->IsOpen()) {
43  delete rootTree_; rootTree_ = 0;
44  }
45  delete rootFile_; rootFile_ = 0;
46 }
47 
49 {
50  // first check whether we've already opened up the file or not
51  if (!rootFile_) {
52  // if not, first check the filename and if all ok create the file
53  if (rootFileName_ == "") {
54  cerr<<"ERROR in LauGenNtuple::createFileAndTree : Bad filename supplied, not creating file or tree."<<endl;
55  return;
56  }
57  rootFile_ = TFile::Open(rootFileName_, "recreate");
58  if (!rootFile_ || rootFile_->IsZombie() || !rootFile_->IsWritable()) {
59  cerr<<"ERROR in LauGenNtuple::createFileAndTree : Problem opening file \""<<rootFileName_<<"\" for writing, not creating tree."<<endl;
60  return;
61  }
62  }
63  // check whether we've already created the tree
64  if (!rootTree_) {
65  // if not change to the file's directory and create the tree
66  rootFile_->cd();
68  rootTree_->SetDirectory(rootFile_);
69  this->definedBranches(kFALSE);
70  }
71 }
72 
74 {
75  if (this->definedBranches()) {
76  cerr<<"ERROR in LauGenNtuple::addIntegerBranch : Already defined branches, can't add further ones."<<endl;
77  return;
78  }
79  this->setIntegerBranchValue(name, 0);
80 }
81 
82 void LauGenNtuple::addDoubleBranch(const TString& name)
83 {
84  if (this->definedBranches()) {
85  cerr<<"ERROR in LauGenNtuple::addDoubleBranch : Already defined branches, can't add further ones."<<endl;
86  return;
87  }
88  this->setDoubleBranchValue(name, 0.0);
89 }
90 
91 void LauGenNtuple::setIntegerBranchValue(const TString& name, Int_t value)
92 {
93  intVars_[name] = value;
94 }
95 
96 void LauGenNtuple::setDoubleBranchValue(const TString& name, Double_t value)
97 {
99 }
100 
101 Int_t LauGenNtuple::getIntegerBranchValue(const TString& name) const
102 {
103  IntVarMap::const_iterator iter = intVars_.find( name );
104  if ( iter == intVars_.end() ) {
105  cerr<<"ERROR in LauGenNtuple::getIntegerBranchValue : no such branch \""<<name<<"\"."<<endl;
106  return -99;
107  } else {
108  return iter->second;
109  }
110 }
111 
112 Double_t LauGenNtuple::getDoubleBranchValue(const TString& name) const
113 {
114  DoubleVarMap::const_iterator iter = doubleVars_.find( name );
115  if ( iter == doubleVars_.end() ) {
116  cerr<<"ERROR in LauGenNtuple::getDoubleBranchValue : no such branch \""<<name<<"\"."<<endl;
117  return -99.0;
118  } else {
119  return iter->second;
120  }
121 }
122 
124 {
125  if (this->definedBranches()) {
126  cerr<<"ERROR in LauGenNtuple::defineBranches : Already defined branches, not doing it again."<<endl;
127  return;
128  }
129  for (IntVarMap::iterator iter = intVars_.begin(); iter != intVars_.end(); ++iter) {
130  TString name = iter->first;
131  Int_t * pointer = &(iter->second);
132  TString thirdPart(name); thirdPart += "/I";
133  rootTree_->Branch(name, pointer, thirdPart);
134  }
135  for (DoubleVarMap::iterator iter = doubleVars_.begin(); iter != doubleVars_.end(); ++iter) {
136  TString name = iter->first;
137  Double_t * pointer = &(iter->second);
138  TString thirdPart(name); thirdPart += "/D";
139  rootTree_->Branch(name, pointer, thirdPart);
140  }
141  this->definedBranches(kTRUE);
142 }
143 
145 {
146  if (!rootTree_) {
147  cerr<<"ERROR in LauGenNtuple::fillBranches : Tree not created, cannot fill branches."<<endl;
148  return;
149  } else if (!this->definedBranches()) {
150  this->defineBranches();
151  }
152  rootTree_->Fill();
153 }
154 
156 {
157  if (rootTree_) {
158  delete rootTree_;
159  rootTree_ = 0;
160  }
161  this->createFileAndTree();
162 }
163 
164 Int_t LauGenNtuple::buildIndex(const TString& majorName, const TString& minorName)
165 {
166  if (!rootTree_) {
167  cerr<<"ERROR in LauGenNtuple::buildIndex : Tree not created, cannot build index."<<endl;
168  return -1;
169  }
170  return rootTree_->BuildIndex(majorName, minorName);
171 }
172 
174 {
175  // Write out the generated ntuple
176 
177  // Check that the file is open
178  if ( rootFile_ == 0 ) {
179  cerr<<"ERROR in LauGenNtuple::writeOutGenResults : File not opened, can't write anything."<<endl;
180  return;
181  }
182 
183  // Check that the tree exists and if so then make sure we have the
184  // up to date pointer to the file since if it splits via the
185  // TTree::ChangeFile mechanism we're left with a dangling pointer
186  if ( rootTree_ != 0 ) {
187  rootFile_ = rootTree_->GetCurrentFile();
188  }
189  rootFile_->cd();
190  rootFile_->Write();
191  rootFile_->Close();
192  delete rootFile_; rootFile_ = 0;
193 }
194 
195 void LauGenNtuple::addFriendTree(const TString& rootFileName, const TString& rootTreeName)
196 {
197  if (!rootTree_) {
198  cerr<<"ERROR in LauGenNtuple::addFriendTree : Tree not created, cannot add friend."<<endl;
199  return;
200  }
201  rootTree_->AddFriend(rootTreeName,rootFileName);
202 }
203 
const TString & name() const
The parameter name.
Int_t buildIndex(const TString &majorName, const TString &minorName="0")
Create an index table using leaves of the tree.
Int_t getIntegerBranchValue(const TString &name) const
Get value of an integer branch.
void deleteAndRecreateTree()
Delete and recreate tree.
void addFriendTree(const TString &rootFileName, const TString &rootTreeName)
Add a friend tree.
Double_t getDoubleBranchValue(const TString &name) const
Get value of a double branch.
void fillBranches()
Fill branches in the ntuple.
Bool_t definedBranches() const
Flags whether branches have been defined.
DoubleVarMap doubleVars_
Double variables.
void createFileAndTree()
Create ntuple file and the tree.
Definition: LauGenNtuple.cc:48
void addIntegerBranch(const TString &name)
Add integer branch to tree.
Definition: LauGenNtuple.cc:73
virtual ~LauGenNtuple()
Destructor.
Definition: LauGenNtuple.cc:38
void defineBranches()
Define branches of the tree.
Class to store the results from the toy MC generation into an ntuple.
Definition: LauGenNtuple.hh:32
TFile * rootFile_
Root file.
void setIntegerBranchValue(const TString &name, Int_t value)
Set value of an integer branch.
Definition: LauGenNtuple.cc:91
TTree * rootTree_
Root tree.
void writeOutGenResults()
Write out the results from the generation.
TString rootTreeName_
Name of root tree.
Double_t value() const
The value of the parameter.
IntVarMap intVars_
Integer variables.
void addDoubleBranch(const TString &name)
Add double branch to tree.
Definition: LauGenNtuple.cc:82
TString rootFileName_
Name of root file.
File containing declaration of LauGenNtuple class.
void setDoubleBranchValue(const TString &name, Double_t value)
Set value of a double branch.
Definition: LauGenNtuple.cc:96