MINT2
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
HyperBinningMemRes Class Reference

#include <HyperBinningMemRes.h>

Inheritance diagram for HyperBinningMemRes:
HyperBinning BinningBase

Public Member Functions

 HyperBinningMemRes ()
 The only constructor. More...
 
virtual ~HyperBinningMemRes ()
 
virtual void reserveCapacity (int nElements)
 
virtual std::vector< int > getPrimaryVolumeNumbers () const
 
virtual void setDimension (int dim)
 
virtual void addPrimaryVolumeNumber (int volumeNumber)
 
virtual bool addHyperVolume (const HyperVolume &hyperVolume, std::vector< int > linkedVolumes=std::vector< int >(0, 0))
 
virtual int getNumHyperVolumes () const
 
virtual HyperVolume getHyperVolume (int volumeNumber) const
 
virtual std::vector< int > getLinkedHyperVolumes (int volumeNumber) const
 
virtual int getNumPrimaryVolumes () const
 
virtual int getPrimaryVolumeNumber (int i) const
 
virtual void load (TString filename, TString option="READ")
 
virtual BinningBaseclone () const
 
- Public Member Functions inherited from HyperBinning
 HyperBinning ()
 The only constructor. More...
 
bool isPrimaryVolume (int volumeNumber) const
 
int getHyperVolumeNumber (int binNumber) const
 
int getBinNum (int volumeNumber) const
 
virtual ~HyperBinning ()
 
virtual void save (TString filename) const
 
virtual void save () const
 
virtual void mergeBinnings (const BinningBase &other)
 
virtual int getNumBins () const
 
virtual int getBinNum (const HyperPoint &coords) const
 
virtual std::vector< int > getBinNum (const HyperPointSet &coords) const
 
std::vector< int > getBinNumAlt (const HyperPointSet &coords) const
 
virtual HyperVolume getBinHyperVolume (int binNumber) const
 
virtual HyperPoint getAverageBinWidth () const
 
virtual HyperCuboid getLimits () const
 
- Public Member Functions inherited from BinningBase
 BinningBase ()
 
void setNames (HyperName names)
 
HyperName getNames () const
 
const int & getDimension () const
 
double getMin (int dimension) const
 
double getMax (int dimension) const
 
TString getBinningType () const
 
bool isSameBinningType (const BinningBase &other) const
 
virtual bool isDiskResident () const
 
virtual TString filename () const
 
virtual ~BinningBase ()
 

Protected Member Functions

void setBranchAddresses (TTree *tree, int *binNumber, double *lowCorner, double *highCorner, std::vector< int > **linkedBins) const
 
void loadPrimaryVolumeNumbers (TFile *file)
 
- Protected Member Functions inherited from HyperBinning
int followBinLinks (const HyperPoint &coords, int binNumber) const
 
void updateCash () const
 
void updateBinNumbering () const
 
void updateAverageBinWidth () const
 
void updateMinMax () const
 
int getHyperBinningDimFromTree (TTree *tree)
 
void createBranches (TTree *tree, int *binNumber, double *lowCorner, double *highCorner, std::vector< int > **linkedBins) const
 
void saveHyperVolumeToTree (TTree *tree, double *lowCorner, double *highCorner, const HyperVolume &hyperVolume) const
 
void savePrimaryVolumeNumbers () const
 
- Protected Member Functions inherited from BinningBase
void setBinningType (TString binningType)
 

Protected Attributes

std::vector< HyperVolume_hyperVolumes
 
std::vector< int > _primaryVolumeNumbers
 
std::vector< std::vector< int > > _linkedHyperVolumes
 

Detailed Description

HyperPlot, Author: Sam Harnew, sam.h.nosp@m.arne.nosp@m.w@gma.nosp@m.il.c.nosp@m.om , Date: Dec 2015

HyperBinningMemRes is a binning scheme where each bin volume is defined by a HyperVolume.

Finding the correct bin number is quite a compuationally slow process if one has to loop over every bin and check if a HyperPoint falls within that bin volume. It's not unusual to have millions of HyperPoints that need to be sorted into tens of thousands of bins. This would require billions of calulations. To speed this process up there is the option to build a hierarchy of bins. A schematic below shows a 1D example.

HyperVolume Numbers
|-------------0-------------|
|------1------|------2------|
|--3---|---4--|---5---|--6--|
|-7-|-8|
Bin Numbers
| 0 | 1| 2 | 3 | 4 |

Imagine we have a HyperPoint that falls into Bin 0. One would first check if it falls into HyperVolume 0 (note the distiction here between Bin/HyperVolume Numbers as indicated by the figure). First we check if it falls into HyperVolume 0, then HyperVolume 1 or 2, then HyperVolume 3 or 4, then HyperVolume 7 or 8.

In this simple example, it took 7 operations, whereas checking each Bin would have taken

  1. Clearly as the number of bins increases, it becomes computationally much less expensive to follow this hierarchy approach.

The

Definition at line 80 of file HyperBinningMemRes.h.

Constructor & Destructor Documentation

◆ HyperBinningMemRes()

HyperBinningMemRes::HyperBinningMemRes ( )

The only constructor.

Definition at line 5 of file HyperBinningMemRes.cpp.

6 {
7  WELCOME_LOG << "Hello from the HyperBinningMemRes() Constructor";
8 }
#define WELCOME_LOG

◆ ~HyperBinningMemRes()

HyperBinningMemRes::~HyperBinningMemRes ( )
virtual

Destructor

Definition at line 254 of file HyperBinningMemRes.cpp.

254  {
255  GOODBYE_LOG << "Goodbye from the HyperBinningMemRes() Constructor";
256 }
#define GOODBYE_LOG

Member Function Documentation

◆ addHyperVolume()

bool HyperBinningMemRes::addHyperVolume ( const HyperVolume hyperVolume,
std::vector< int >  linkedVolumes = std::vector<int>(0, 0) 
)
virtual

Add a HyperVolume to the HyperBinningMemRes and add a set of empty HyperVolume links.

Implements HyperBinning.

Definition at line 65 of file HyperBinningMemRes.cpp.

65  {
66 
67  //If this is the first volume that has been added, use it to set the dimension
68  if (_hyperVolumes.size() == 0){
69  setDimension( hyperVolume.getDimension() );
70  }
71 
72  if (hyperVolume.getDimension() == getDimension()) {
73  _hyperVolumes.push_back(hyperVolume);
74  _linkedHyperVolumes.push_back(linkedVolumes);
75  updateCash();
76  return true;
77  }
78 
79  ERROR_LOG << "This HyperVolume has the wrong dimensionality for this HyperBinningMemRes";
80  return false;
81 
82 }
#define ERROR_LOG
std::vector< HyperVolume > _hyperVolumes
void updateCash() const
const int & getDimension() const
Definition: HyperVolume.h:44
std::vector< std::vector< int > > _linkedHyperVolumes
const int & getDimension() const
Definition: BinningBase.cpp:30
virtual void setDimension(int dim)

◆ addPrimaryVolumeNumber()

void HyperBinningMemRes::addPrimaryVolumeNumber ( int  volumeNumber)
virtual

Add a primary volume number

Implements HyperBinning.

Definition at line 41 of file HyperBinningMemRes.cpp.

41  {
42  _primaryVolumeNumbers.push_back(volumeNumber);
43 }
std::vector< int > _primaryVolumeNumbers

◆ clone()

BinningBase * HyperBinningMemRes::clone ( ) const
virtual

Implements HyperBinning.

Definition at line 31 of file HyperBinningMemRes.cpp.

31  {
32 
33  return dynamic_cast<BinningBase*>(new HyperBinningMemRes(*this));
34 
35 }
HyperBinningMemRes()
The only constructor.

◆ getHyperVolume()

HyperVolume HyperBinningMemRes::getHyperVolume ( int  volumeNumber) const
virtual

get one of the HyperVolumes

Implements HyperBinning.

Definition at line 127 of file HyperBinningMemRes.cpp.

127  {
128  return _hyperVolumes.at(volumeNumber);
129 }
std::vector< HyperVolume > _hyperVolumes

◆ getLinkedHyperVolumes()

std::vector< int > HyperBinningMemRes::getLinkedHyperVolumes ( int  volumeNumber) const
virtual

Implements HyperBinning.

Definition at line 24 of file HyperBinningMemRes.cpp.

24  {
25 
26  return _linkedHyperVolumes.at(volumeNumber);
27 
28 }
std::vector< std::vector< int > > _linkedHyperVolumes

◆ getNumHyperVolumes()

int HyperBinningMemRes::getNumHyperVolumes ( ) const
virtual

get the number of HyperVolumes

Implements HyperBinning.

Definition at line 49 of file HyperBinningMemRes.cpp.

49  {
50  return _hyperVolumes.size();
51 }
std::vector< HyperVolume > _hyperVolumes

◆ getNumPrimaryVolumes()

int HyperBinningMemRes::getNumPrimaryVolumes ( ) const
virtual

Implements HyperBinning.

Definition at line 55 of file HyperBinningMemRes.cpp.

55  {
56  return _primaryVolumeNumbers.size();
57 }
std::vector< int > _primaryVolumeNumbers

◆ getPrimaryVolumeNumber()

int HyperBinningMemRes::getPrimaryVolumeNumber ( int  i) const
virtual

Implements HyperBinning.

Definition at line 59 of file HyperBinningMemRes.cpp.

59  {
60  return _primaryVolumeNumbers.at(i);
61 }
std::vector< int > _primaryVolumeNumbers

◆ getPrimaryVolumeNumbers()

std::vector< int > HyperBinningMemRes::getPrimaryVolumeNumbers ( ) const
virtual

Look at the tree that contains the HyperBinningMemRes and find the dimensionality

Reimplemented from HyperBinning.

Definition at line 134 of file HyperBinningMemRes.cpp.

134  {
135  return _primaryVolumeNumbers;
136 }
std::vector< int > _primaryVolumeNumbers

◆ load()

void HyperBinningMemRes::load ( TString  filename,
TString  option = "READ" 
)
virtual

Load HyperBinningMemRes from a file

Implements HyperBinning.

Definition at line 141 of file HyperBinningMemRes.cpp.

141  {
142 
143  if (option != "READ"){
144  INFO_LOG << "For a memory resident HyperBinning you should always use the READ option. Setting to READ" << std::endl;
145  option = "READ";
146  }
147 
148  TFile* file = new TFile(filename, "READ");
149 
150  if (file == 0){
151  ERROR_LOG << "Could not open TFile in HyperBinningMemRes::load(" << filename << ")";
152  return;
153  }
154 
156 
157  TTree* tree = (TTree*)file->Get("HyperBinning");
158 
159  if (tree == 0){
160  ERROR_LOG << "Could not open TTree in HyperBinningMemRes::load()";
161  return;
162  }
163 
164  //Figure out how many dimensions there are from the tree
166 
167  //Create branch addresses and link them to TTree
168  int binNumber = -1;
169  double* lowCorner = new double [getDimension()];
170  double* highCorner = new double [getDimension()];
171  std::vector<int>* linkedBins = new std::vector<int>();
172 
173  setBranchAddresses(tree, &binNumber, lowCorner, highCorner, &linkedBins);
174 
175 
176  //Loop over the TTree and fill the HyperBinningMemRes
177  int nEntries = tree->GetEntries();
178 
179  int currentBinNumber = -1;
180  HyperVolume* currentHyperVolume = new HyperVolume(getDimension());
181  std::vector<int> currentLinkedVolumes;
182 
183  //_verbose = true;
184 
185  for(int ent = 0; ent < nEntries; ent++){
186  tree->GetEntry(ent);
187 
188  //If the bin number hasn't changed, need to
189  //add HyperCube to previous HyperVolume
190  if(ent == 0 || currentBinNumber == binNumber){
191  currentBinNumber = binNumber;
192  currentLinkedVolumes = *linkedBins;
193  HyperPoint lowCornerVect (getDimension());
194  HyperPoint highCornerVect(getDimension());
195  for (int i = 0; i < getDimension(); i++){
196  lowCornerVect .at(i) = lowCorner [i];
197  highCornerVect.at(i) = highCorner[i];
198  }
199  VERBOSE_LOG << "Adding cuboid to volume";
200  currentHyperVolume->addHyperCuboid(lowCornerVect, highCornerVect);
201  }
202 
203  //if the bin number has changed, need to add
204  //previous HyperVolume to HyperBinningMemRes
205  //and start a new HyperVolume
206  else {
207  VERBOSE_LOG << "Adding volume to binning";
208  this->addHyperVolume(*currentHyperVolume, currentLinkedVolumes);
209  delete currentHyperVolume;
210  currentHyperVolume = new HyperVolume(getDimension());
211  currentLinkedVolumes = *linkedBins;
212  currentBinNumber = binNumber;
213  HyperPoint lowCornerVect (getDimension());
214  HyperPoint highCornerVect(getDimension());
215  for (int i = 0; i < getDimension(); i++){
216  lowCornerVect .at(i) = lowCorner [i];
217  highCornerVect.at(i) = highCorner[i];
218  }
219  VERBOSE_LOG << "Adding cuboid to volume";
220  currentHyperVolume->addHyperCuboid(lowCornerVect, highCornerVect);
221  VERBOSE_LOG << "Adding linked volumes";
222 
223  }
224 
225  //if it's the final iteration, need to add the final HyperVolume
226  if (ent == nEntries - 1) {
227  this->addHyperVolume(*currentHyperVolume);
228  }
229  }
230 
231  VERBOSE_LOG << "Binning loaded";
232 
233  //_verbose = false;
234 
235  delete currentHyperVolume;
236  delete[] lowCorner;
237  delete[] highCorner;
238  delete linkedBins;
239 
240  updateCash();
241 
242  file->Close();
243 
244 }
virtual TString filename() const
Definition: BinningBase.cpp:57
int getHyperBinningDimFromTree(TTree *tree)
#define INFO_LOG
#define ERROR_LOG
void updateCash() const
#define VERBOSE_LOG
void loadPrimaryVolumeNumbers(TFile *file)
const int & getDimension() const
Definition: BinningBase.cpp:30
void addHyperCuboid(const HyperPoint &lowCorner, const HyperPoint &highCorner)
Definition: HyperVolume.cpp:91
void setBranchAddresses(TTree *tree, int *binNumber, double *lowCorner, double *highCorner, std::vector< int > **linkedBins) const
virtual void setDimension(int dim)
virtual bool addHyperVolume(const HyperVolume &hyperVolume, std::vector< int > linkedVolumes=std::vector< int >(0, 0))

◆ loadPrimaryVolumeNumbers()

void HyperBinningMemRes::loadPrimaryVolumeNumbers ( TFile *  file)
protected

Save the list of Primary Volume Numbers to the open (and in scope) TFile.

Definition at line 89 of file HyperBinningMemRes.cpp.

89  {
90 
91  TTree* tree = dynamic_cast<TTree*>( file->Get("PrimaryVolumeNumbers") );
92 
93  if (tree == 0){
94  ERROR_LOG << "Could not open TTree in HyperBinningMemRes::loadPrimaryVolumeNumbers()";
95  return;
96  }
97 
98  //Define branch addresses
99  int volumeNumber = -1;
100 
101  tree->SetBranchAddress("volumeNumber", &volumeNumber);
102 
103  //Loop over each Primary Volume
104  for(int i = 0; i < tree->GetEntries(); i++ ){
105  tree->GetEntry(i);
106  _primaryVolumeNumbers.push_back(volumeNumber);
107  }
108 
109 }
#define ERROR_LOG
std::vector< int > _primaryVolumeNumbers

◆ reserveCapacity()

void HyperBinningMemRes::reserveCapacity ( int  nElements)
virtual

Reimplemented from HyperBinning.

Definition at line 246 of file HyperBinningMemRes.cpp.

246  {
248  _hyperVolumes .reserve(nElements);
249  _linkedHyperVolumes.reserve(nElements);
250 }
virtual void reserveCapacity(int nElements)
std::vector< HyperVolume > _hyperVolumes
std::vector< std::vector< int > > _linkedHyperVolumes

◆ setBranchAddresses()

void HyperBinningMemRes::setBranchAddresses ( TTree *  tree,
int *  binNumber,
double *  lowCorner,
double *  highCorner,
std::vector< int > **  linkedBins 
) const
protected

Set branch addresses for loading HyperBinningMemRes from a file.

Definition at line 113 of file HyperBinningMemRes.cpp.

113  {
114 
115  tree->SetBranchAddress("binNumber", binNumber);
116  tree->SetBranchAddress("linkedBins", linkedBins);
117  for (int i = 0; i < getDimension(); i++) {
118  TString lowCornerName = "lowCorner_"; lowCornerName += i;
119  TString highCornerName = "highCorner_"; highCornerName += i;
120  tree->SetBranchAddress(lowCornerName, lowCorner + i);
121  tree->SetBranchAddress(highCornerName, highCorner + i);
122  }
123 
124 }
const int & getDimension() const
Definition: BinningBase.cpp:30

◆ setDimension()

void HyperBinningMemRes::setDimension ( int  dim)
virtual

Set the dimension of the HyperBinningMemRes. This can only be called once, when it is known what dimesnion it is.

Reimplemented from HyperBinning.

Definition at line 15 of file HyperBinningMemRes.cpp.

15  {
16 
17  if (getDimension() == 0){
19  }
20 
21 }
virtual void setDimension(int dimension)
Definition: BinningBase.cpp:34
const int & getDimension() const
Definition: BinningBase.cpp:30

Member Data Documentation

◆ _hyperVolumes

std::vector< HyperVolume > HyperBinningMemRes::_hyperVolumes
protected

std::vector containing all of the HyperVolumes. Usually, not all of these are bins, but part of the bin hierarchy that is discussed in the class description.

Definition at line 84 of file HyperBinningMemRes.h.

◆ _linkedHyperVolumes

std::vector< std::vector<int> > HyperBinningMemRes::_linkedHyperVolumes
protected

Every HyperVolume has a _linkedHyperVolumes - if this is empty, this means the HyperVolume is a true Bin. If not it is part of the binning hierarchy. In the below, HyperVolume 0 would be linked to HyperVolume [1,2], although HyperVolume 4 would be linked to nothing.

HyperVolume Numbers
|-------------0-------------|
|------1------|------2------|
|--3---|---4--|---5---|--6--|
|-7-|-8|
Bin Numbers
| 0 | 1| 2 | 3 | 4 |

Definition at line 116 of file HyperBinningMemRes.h.

◆ _primaryVolumeNumbers

std::vector< int > HyperBinningMemRes::_primaryVolumeNumbers
protected

Usually all bins will be accessed through one primary volume i.e. volume 0 in the below example. If one wants to merge two binning schemes together e.g. Binning1 and Binning2 this involes appending the list of HyperVolumes from Binning2 to Binning1. This brings a problem when trying to sort a HyperPoint into a specific bin i.e. getBinNumber(HyperPoint) - if it belongs to Binning2, it will first have to check that it doesn't fall into any of the volumes in Binning1.

To remedy this problem we introduce the list of primary volumes. If this list exists i.e. its size is greater than 0, all the primary volumes will be checked first. The existance of this list also implies that ALL volumes are linked to the primary volumes.

   HyperVolume Numbers 

|----------—0----------—|

|---—1---—|---—2---—|

|–3—|—4–|—5—|–6–|

|-7-|-8|

Definition at line 90 of file HyperBinningMemRes.h.


The documentation for this class was generated from the following files: