laura is hosted by Hepforge, IPPP Durham
Laura++  v3r5
A maximum likelihood fitting package for performing Dalitz-plot analysis.
LauGenNtuple.cc
Go to the documentation of this file.
1 
2 /*
3 Copyright 2006 University of Warwick
4 
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8 
9  http://www.apache.org/licenses/LICENSE-2.0
10 
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 */
17 
18 /*
19 Laura++ package authors:
20 John Back
21 Paul Harrison
22 Thomas Latham
23 */
24 
29 #include <iostream>
30 using std::cout;
31 using std::cerr;
32 using std::endl;
33 
34 #include "TFile.h"
35 #include "TTree.h"
36 
37 #include "LauGenNtuple.hh"
38 
40 
41 
42 LauGenNtuple::LauGenNtuple(const TString& rootFileName, const TString& rootTreeName) :
43  rootFileName_(rootFileName),
44  rootTreeName_(rootTreeName),
45  rootFile_(0),
46  rootTree_(0),
47  definedBranches_(kFALSE)
48 {
49  this->createFileAndTree();
50 }
51 
53 {
54  // seems that closing the file deletes the tree
55  // so only delete if the file is still open for some reason
56  if (rootFile_ && rootFile_->IsOpen()) {
57  delete rootTree_; rootTree_ = 0;
58  }
59  delete rootFile_; rootFile_ = 0;
60 }
61 
63 {
64  // first check whether we've already opened up the file or not
65  if (!rootFile_) {
66  // if not, first check the filename and if all ok create the file
67  if (rootFileName_ == "") {
68  cerr<<"ERROR in LauGenNtuple::createFileAndTree : Bad filename supplied, not creating file or tree."<<endl;
69  return;
70  }
71  rootFile_ = TFile::Open(rootFileName_, "recreate");
72  if (!rootFile_ || rootFile_->IsZombie() || !rootFile_->IsWritable()) {
73  cerr<<"ERROR in LauGenNtuple::createFileAndTree : Problem opening file \""<<rootFileName_<<"\" for writing, not creating tree."<<endl;
74  return;
75  }
76  }
77  // check whether we've already created the tree
78  if (!rootTree_) {
79  // if not change to the file's directory and create the tree
80  rootFile_->cd();
82  rootTree_->SetDirectory(rootFile_);
83  this->definedBranches(kFALSE);
84  }
85 }
86 
88 {
89  if (this->definedBranches()) {
90  cerr<<"ERROR in LauGenNtuple::addIntegerBranch : Already defined branches, can't add further ones."<<endl;
91  return;
92  }
93  this->setIntegerBranchValue(name, 0);
94 }
95 
96 void LauGenNtuple::addDoubleBranch(const TString& name)
97 {
98  if (this->definedBranches()) {
99  cerr<<"ERROR in LauGenNtuple::addDoubleBranch : Already defined branches, can't add further ones."<<endl;
100  return;
101  }
102  this->setDoubleBranchValue(name, 0.0);
103 }
104 
105 void LauGenNtuple::setIntegerBranchValue(const TString& name, Int_t value)
106 {
107  intVars_[name] = value;
108 }
109 
110 void LauGenNtuple::setDoubleBranchValue(const TString& name, Double_t value)
111 {
113 }
114 
115 Int_t LauGenNtuple::getIntegerBranchValue(const TString& name) const
116 {
117  IntVarMap::const_iterator iter = intVars_.find( name );
118  if ( iter == intVars_.end() ) {
119  cerr<<"ERROR in LauGenNtuple::getIntegerBranchValue : no such branch \""<<name<<"\"."<<endl;
120  return -99;
121  } else {
122  return iter->second;
123  }
124 }
125 
126 Double_t LauGenNtuple::getDoubleBranchValue(const TString& name) const
127 {
128  DoubleVarMap::const_iterator iter = doubleVars_.find( name );
129  if ( iter == doubleVars_.end() ) {
130  cerr<<"ERROR in LauGenNtuple::getDoubleBranchValue : no such branch \""<<name<<"\"."<<endl;
131  return -99.0;
132  } else {
133  return iter->second;
134  }
135 }
136 
138 {
139  if (this->definedBranches()) {
140  cerr<<"ERROR in LauGenNtuple::defineBranches : Already defined branches, not doing it again."<<endl;
141  return;
142  }
143  for (IntVarMap::iterator iter = intVars_.begin(); iter != intVars_.end(); ++iter) {
144  TString name = iter->first;
145  Int_t * pointer = &(iter->second);
146  TString thirdPart(name); thirdPart += "/I";
147  rootTree_->Branch(name, pointer, thirdPart);
148  }
149  for (DoubleVarMap::iterator iter = doubleVars_.begin(); iter != doubleVars_.end(); ++iter) {
150  TString name = iter->first;
151  Double_t * pointer = &(iter->second);
152  TString thirdPart(name); thirdPart += "/D";
153  rootTree_->Branch(name, pointer, thirdPart);
154  }
155  this->definedBranches(kTRUE);
156 }
157 
159 {
160  if (!rootTree_) {
161  cerr<<"ERROR in LauGenNtuple::fillBranches : Tree not created, cannot fill branches."<<endl;
162  return;
163  } else if (!this->definedBranches()) {
164  this->defineBranches();
165  }
166  rootTree_->Fill();
167 }
168 
170 {
171  if (rootTree_) {
172  delete rootTree_;
173  rootTree_ = 0;
174  }
175  this->createFileAndTree();
176 }
177 
178 Int_t LauGenNtuple::buildIndex(const TString& majorName, const TString& minorName)
179 {
180  if (!rootTree_) {
181  cerr<<"ERROR in LauGenNtuple::buildIndex : Tree not created, cannot build index."<<endl;
182  return -1;
183  }
184  return rootTree_->BuildIndex(majorName, minorName);
185 }
186 
188 {
189  // Write out the generated ntuple
190 
191  // Check that the file is open
192  if ( rootFile_ == 0 ) {
193  cerr<<"ERROR in LauGenNtuple::writeOutGenResults : File not opened, can't write anything."<<endl;
194  return;
195  }
196 
197  // Check that the tree exists and if so then make sure we have the
198  // up to date pointer to the file since if it splits via the
199  // TTree::ChangeFile mechanism we're left with a dangling pointer
200  if ( rootTree_ != 0 ) {
201  rootFile_ = rootTree_->GetCurrentFile();
202  }
203  rootFile_->cd();
204  rootTree_->Write("",TObject::kOverwrite);
205  rootFile_->Close();
206  delete rootFile_; rootFile_ = 0;
207 }
208 
209 void LauGenNtuple::addFriendTree(const TString& rootFileName, const TString& rootTreeName)
210 {
211  if (!rootTree_) {
212  cerr<<"ERROR in LauGenNtuple::addFriendTree : Tree not created, cannot add friend."<<endl;
213  return;
214  }
215  rootTree_->AddFriend(rootTreeName,rootFileName);
216 }
217 
ClassImp(LauAbsCoeffSet)
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:62
void addIntegerBranch(const TString &name)
Add integer branch to tree.
Definition: LauGenNtuple.cc:87
virtual ~LauGenNtuple()
Destructor.
Definition: LauGenNtuple.cc:52
void defineBranches()
Define branches of the tree.
Class to store the results from the toy MC generation into an ntuple.
Definition: LauGenNtuple.hh:46
TFile * rootFile_
Root file.
void setIntegerBranchValue(const TString &name, Int_t value)
Set value of an integer branch.
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:96
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.