MINT2
HyperBinningDiskRes.cpp
Go to the documentation of this file.
2 
3 
7  _file(0),
8  _writeable(false),
9  _tree(0),
10  _cuboid(0),
11  _linkedBins(new std::vector<int>()),
12  _volumeNumber(-1),
13  _currentEntry(-1),
14  _treePrimVol(0),
15  _primVolNum(-1)
16 {
17  WELCOME_LOG << "Hello from the HyperBinningDiskRes() Constructor";
18 }
19 
23  _file(0),
24  _writeable(false),
25  _tree(0),
26  _cuboid(0),
27  _linkedBins(new std::vector<int>()),
28  _volumeNumber(-1),
29  _currentEntry(-1),
30  _treePrimVol(0),
31  _primVolNum(-1)
32 {
33  if (other._writeable == true){
34  other._file->Write();
35  }
36  load(other._file->GetName(), "READ");
37 }
38 
39 
45 
46  if (getDimension() == 0){
48  _cuboid = HyperCuboid(dim);
49  }
50 
51 }
52 
55 void HyperBinningDiskRes::getEntry(int volumeNumber) const{
56  if (_tree == 0){
57  ERROR_LOG << "HyperBinningDiskRes::getEntry - tree doesn't exist" << std::endl;
58  return;
59  }
60  if ( _currentEntry != volumeNumber ){
61  _currentEntry = volumeNumber;
62  _tree->GetEntry(volumeNumber);
63  }
64 }
65 
68  getEntry(volumeNumber);
69  return HyperVolume(_cuboid);
70 }
71 
74 std::vector<int> HyperBinningDiskRes::getLinkedHyperVolumes( int volumeNumber ) const{
75  getEntry(volumeNumber);
76  return *_linkedBins;
77 }
78 
81 
82  BinningBase* ret = dynamic_cast<BinningBase*>(new HyperBinningDiskRes());
83 
84  if (_file != 0) {
85 
86  TString filenm = filename();
87  _file->cd();
88 
89  if (_writeable == true){
90  _file->Write();
91  }
92 
93  ret->load(filenm, "READ");
94 
95  }
96 
97  return ret;
98 
99 }
100 
101 
105 
106  if (_writeable == false){
107  ERROR_LOG << "HyperBinningDiskRes::addPrimaryVolumeNumber - Cannot write, you must have opened in READ mode" << std::endl;
108  return;
109  }
110  if (_treePrimVol == 0){
111  ERROR_LOG << "HyperBinningDiskRes::addPrimaryVolumeNumber - Cannot find tree" << std::endl;
112  return;
113  }
114 
115  _primVolNum = volumeNumber;
116  _treePrimVol->Fill();
117 }
118 
119 
120 
124  if (_tree == 0){
125  return 0;
126  }
127  return _tree->GetEntries();
128 }
129 
130 
133 bool HyperBinningDiskRes::addHyperVolume(const HyperVolume& hyperVolume, std::vector<int> linkedVolumes){
134 
135  if (_writeable == false){
136  ERROR_LOG << "HyperBinningDiskRes::addHyperVolume - Cannot write, you must have opened in READ mode" << std::endl;
137  return false;
138  }
139 
140  //If dimension hasn't been set, and it's a writeable, we can now
141  //set the dimesnion and create the tree
142 
143  if (getDimension() == 0 && _writeable == true){
144  setDimension(hyperVolume.getDimension());
146  }
147 
148  if (_tree == 0){
149  ERROR_LOG << "HyperBinningDiskRes::addHyperVolume - Cannot find tree" << std::endl;
150  return false;
151  }
152 
153  int nVolumes = getNumHyperVolumes();
154  for (int i = 0; i < hyperVolume.size(); i++){
155  *_linkedBins = linkedVolumes;
156  _cuboid = hyperVolume.at(i);
157  _volumeNumber = nVolumes;
158  _tree->Fill();
159  }
160 
161  updateCash();
162 
163  return true;
164 }
165 
166 
170  if (_treePrimVol == 0) return 0;
171  return _treePrimVol->GetEntries();
172 }
173 
177  if (_treePrimVol == 0) {
178  ERROR_LOG << "HyperBinningDiskRes::getPrimaryVolumeNumber - No tree found" << std::endl;
179  }
180  _treePrimVol->GetEntry(i);
181  return _primVolNum;
182 }
183 
184 
186 
187  _tree = dynamic_cast<TTree*>(_file->Get("HyperBinning"));
188 
189  if (_tree == 0){
190  ERROR_LOG << "Could not open TTree in HyperBinningDiskRes::load()";
191  return;
192  }
193 
194  //Figure out how many dimensions there are from the tree
196 
197  _tree->SetBranchAddress("binNumber" , &_volumeNumber);
198  _tree->SetBranchAddress("linkedBins", &_linkedBins );
199 
200  for (int i = 0; i < getDimension(); i++){
201  TString lowCornerName = "lowCorner_" ; lowCornerName += i;
202  TString highCornerName = "highCorner_"; highCornerName += i;
203  _tree->SetBranchAddress(lowCornerName , &_cuboid.getLowCorner ().at(i) );
204  _tree->SetBranchAddress(highCornerName, &_cuboid.getHighCorner().at(i) );
205  }
206 
207  //_tree->SetMaxVirtualSize(1000);
208  _currentEntry = -1;
209  updateCash();
210 
211 }
212 
213 
214 
216 
217  _treePrimVol = dynamic_cast<TTree*>( _file->Get("PrimaryVolumeNumbers") );
218 
219  if (_treePrimVol == 0){
220  ERROR_LOG << "HyperBinningDiskRes::loadPrimaryVolumeNumbers - could not open TTree";
221  return;
222  }
223 
224  _treePrimVol->SetBranchAddress("volumeNumber", &_primVolNum);
225 
226 }
227 
229 
230  _file->cd();
231  _tree = new TTree("HyperBinning", "HyperBinning");
232 
233  if (_treePrimVol == 0){
234  ERROR_LOG << "HyperBinningDiskRes::createHyperBinningTree - could not open TTree";
235  return;
236  }
237 
238  //Figure out how many dimensions there are from the tree
239  int dim = getDimension();
240  if (dim == 0){
241  ERROR_LOG << "The dimesion has not yet been set, so I cannot createHyperBinningTree!!" << std::endl;
242  return;
243  }
244 
245  _tree->Branch("binNumber" , &_volumeNumber );
246  _tree->Branch("linkedBins", &_linkedBins );
247 
248  for (int i = 0; i < dim; i++){
249  TString lowCornerName = "lowCorner_" ; lowCornerName += i;
250  TString highCornerName = "highCorner_"; highCornerName += i;
251  _tree->Branch(lowCornerName , &_cuboid.getLowCorner ().at(i) );
252  _tree->Branch(highCornerName, &_cuboid.getHighCorner().at(i) );
253  }
254 
255  //random access is really slow. Thought this might help
256  //_tree->SetMaxVirtualSize(1000);
257 
258  _currentEntry = -1;
259  updateCash();
260 
261 }
262 
264 
265  _file->cd();
266  _treePrimVol = new TTree("PrimaryVolumeNumbers", "PrimaryVolumeNumbers");
267 
268  if (_treePrimVol == 0){
269  ERROR_LOG << "HyperBinningDiskRes::createPrimaryVolumeTree - could not open TTree";
270  return;
271  }
272 
273  _treePrimVol->Branch("volumeNumber", &_primVolNum);
274 
275 }
276 
279 void HyperBinningDiskRes::load(TString filename, TString option){
280 
281  _writeable = false;
282  if (option == "UPDATE" || option == "RECREATE") {
283  _writeable = true;
284  }
285 
286  if (_file != 0) _file->Close();
287 
288  _file = new TFile(filename, option);
289 
290  if (_file == 0){
291  ERROR_LOG << "Could not open TFile in HyperBinningDiskRes::load(" << filename << ")";
292  return;
293  }
294 
295  if (option == "UPDATE" || option == "READ"){
298  }
299  else if (option == "RECREATE"){
300  //if we're opening a new file, we don't know the dimension yet.
301  //wait until the dimension is set explicitly with setDimension().
302  //createHyperBinningTree ();
304  }
305  else{
306  ERROR_LOG << "There are only three load options (READ, RECREATE, UPDATE) - You have selected " << option << std::endl;
307  return;
308  }
309 
310  INFO_LOG << "Sucessfully attached Disk Resident HyperBinning to file " << filename << std::endl;
311 
312 }
313 
315  return true;
316 }
318  if (_file == 0){
319  ERROR_LOG << "HyperBinningDiskRes::filename - there is no file associated to this object yet" << std::endl;
320  return "";
321  }
322  return _file->GetName();
323 }
324 
326  HyperBinning::reserveCapacity(nElements);
327 
328 }
329 
330 
331 
335  GOODBYE_LOG << "Goodbye from the HyperBinningDiskRes() Constructor";
336 
337  if (_file != 0) {
338  _file->cd();
339  if (_writeable == true){
340  _tree ->Write();
341  _treePrimVol->Write();
342  }
343  _file->Close();
344  _file = 0;
345  }
346 
347  delete _linkedBins;
348 
349 }
350 
351 
352 
virtual int getNumPrimaryVolumes() const
int getHyperBinningDimFromTree(TTree *tree)
#define INFO_LOG
virtual void setDimension(int dim)
const HyperCuboid & at(int i) const
Definition: HyperVolume.h:55
int size() const
Definition: HyperVolume.h:60
virtual void reserveCapacity(int nElements)
void getEntry(int volumeNumber) const
virtual void addPrimaryVolumeNumber(int volumeNumber)
virtual void load(TString filename, TString option="READ")=0
#define ERROR_LOG
virtual int getPrimaryVolumeNumber(int i) const
void updateCash() const
virtual std::vector< int > getLinkedHyperVolumes(int volumeNumber) const
virtual bool addHyperVolume(const HyperVolume &hyperVolume, std::vector< int > linkedVolumes=std::vector< int >(0, 0))
const int & getDimension() const
Definition: HyperVolume.h:44
const HyperPoint & getHighCorner() const
Definition: HyperCuboid.h:89
#define GOODBYE_LOG
virtual void load(TString filename, TString option="READ")
const int & getDimension() const
Definition: BinningBase.cpp:30
const double & at(int i) const
Definition: HyperPoint.cpp:433
virtual TString filename() const
virtual void reserveCapacity(int nElements)
const HyperPoint & getLowCorner() const
Definition: HyperCuboid.h:87
#define WELCOME_LOG
virtual bool isDiskResident() const
virtual int getNumHyperVolumes() const
virtual void setDimension(int dim)
virtual HyperVolume getHyperVolume(int volumeNumber) const
Get a HyperVolume from its volume number.
std::vector< int > * _linkedBins
virtual BinningBase * clone() const
Create a clone of the object and return a pointer to it.