11 _binning(binning.clone())
13 WELCOME_LOG <<
"Good day from the HyperHistogram() Constructor";
97 HistogramBase::operator=(other);
98 HyperFunction::operator=(other);
113 WELCOME_LOG <<
"Good day from the HyperHistogram() Constructor";
115 if (option.Contains(
"Empty")){
119 load(filename, option);
135 WELCOME_LOG <<
"Good day from the HyperHistogram() Constructor";
137 int nFiles = filename.size();
139 ERROR_LOG <<
"The list of filenames you provided to HyperHistogram is empty" << std::endl;
142 INFO_LOG <<
"Loading HyperHistogram at: " << filename.at(0) << std::endl;
143 load(filename.at(0),
"MEMRES");
145 for (
int i = 1; i < nFiles; i++){
146 INFO_LOG <<
"Loading and merging HyperHistogram at: " << filename.at(i) << std::endl;
147 merge(filename.at(i));
165 WELCOME_LOG <<
"Good day from the HyperHistogram() Constructor";
167 int nFiles = filename.size();
169 ERROR_LOG <<
"The list of filenames you provided to HyperHistogram is empty" << std::endl;
175 INFO_LOG <<
"Creating HyperHistogram at: " << targetFilename <<
" with binning type " << binningType <<std::endl;
176 loadEmpty(targetFilename,
"DISKRES", binningType );
179 INFO_LOG <<
"I estimate there will be " << nResBins <<
" in total - resizing the Histogram accordingly" << std::endl;
181 for (
int i = 0; i < nFiles; i++){
182 INFO_LOG <<
"Loading and merging HyperHistogram at: " << filename.at(i) << std::endl;
183 merge(filename.at(i));
197 for (
unsigned i = 0; i < filename.size(); i++){
198 TFile* file =
new TFile(filename.at(i),
"READ");
200 ERROR_LOG <<
"HyperHistogram::estimateCapacity - " << filename.at(i) <<
" does not exist" << std::endl;
203 TTree* hist = dynamic_cast<TTree*>( file->Get(binningType) );
204 TTree* base = dynamic_cast<TTree*>( file->Get(
"HistogramBase") );
206 ERROR_LOG <<
"HyperHistogram::estimateCapacity - " << filename.at(i) <<
" does not contain tree " << binningType << std::endl;
210 ERROR_LOG <<
"HyperHistogram::estimateCapacity - " << filename.at(i) <<
" does not contain tree HistogramBase" << std::endl;
213 nbins += base->GetEntries();
214 nvols += hist->GetEntries();
229 _binning( other._binning->clone() )
242 WELCOME_LOG <<
"Good day from the HyperHistogram() Constructor";
295 int nPoints = points.
size();
299 std::vector<double> conts(nPoints);
301 for (
int i = 0; i < nPoints; i++){
317 for(
unsigned i = 0; i < points.
size(); i++){
339 const HyperHistogram* histOther = dynamic_cast<const HyperHistogram*>(&other);
342 ERROR_LOG <<
"The object passed to HyperHistogram::merge is not of type ";
343 ERROR_LOG <<
"HyperHistogram, so cannot merge";
388 for (
int i = 0; i < nbins; i++){
390 double funcVal = func.
getVal(binCenter);
407 ERROR_LOG <<
"It is only possible to merge bins when using HyperBinning. Doing nothing." << std::endl;
415 std::map<int, bool> volumeKept;
417 volumeKept[i] =
true;
429 if (linkedVols.size() == 0)
continue;
431 bool linksLeadToBins =
true;
433 for (
unsigned j = 0; j < linkedVols.size(); j++){
434 int volNum = linkedVols.at(j);
436 linksLeadToBins =
false;
441 if (linksLeadToBins ==
false)
continue;
443 double binCont0 = -99999.9;
445 bool binsHaveSameContent =
true;
447 for (
unsigned j = 0; j < linkedVols.size(); j++){
448 int volNum = linkedVols.at(j);
449 int binNum = hyperBinning.
getBinNum(volNum);
451 if (j == 0) binCont0 = binContent;
453 if (binContent != binCont0){
454 binsHaveSameContent =
false;
460 if (binsHaveSameContent ==
false)
continue;
462 for (
unsigned j = 0; j < linkedVols.size(); j++){
463 int volNum = linkedVols.at(j);
464 volumeKept[volNum] =
false;
473 std::map<int, int> oldToNewVolumeNum;
477 bool exists = volumeKept[i];
480 oldToNewVolumeNum[i] = newVolNum;
484 oldToNewVolumeNum[i] = -1;
497 binningNew->
load( hyperBinning.
filename().ReplaceAll(
".root",
"_temp.root"),
"RECREATE" );
503 INFO_LOG <<
"Created a new HyperBinning for the reduced binning" << std::endl;
510 int newVolNum = oldToNewVolumeNum[i];
512 if (newVolNum == -1)
continue;
514 if (newVolNum != count){
515 ERROR_LOG <<
"Something has gone wrong in mergeBinsWithSameContent()" << std::endl;
520 std::vector<int> newLinkedVols;
523 for (
unsigned j = 0; j < linkedVols.size(); j++){
524 int linkVolNum = linkedVols.at(j);
525 int newLinkVolNum = oldToNewVolumeNum[linkVolNum];
527 if (newLinkVolNum != -1){
528 newLinkedVols.push_back(newLinkVolNum);
539 INFO_LOG <<
"This should never be one" << std::endl;
548 for (
unsigned i = 0; i < primVolNums.size(); i++){
549 int oldVolNum = primVolNums.at(i);
550 int newVolNum = oldToNewVolumeNum[oldVolNum];
554 oldToNewVolumeNum.clear();
556 INFO_LOG <<
"Filled the new binning with reduced bins" << std::endl;
561 INFO_LOG <<
"Made a new hitogram which will clone the binning" << std::endl;
565 INFO_LOG <<
"You have managed to remove " << hyperBinning.
getNumBins() - binningNew->
getNumBins() <<
" bins with the same content" << std::endl;
567 bool moreBinsToMerge =
false;
571 TString filenm = hyperBinning.
filename();
574 newHist.
save( filenm );
576 load(filenm,
"DISK");
598 painter.
draw(path, options);
603 painter.
draw(path, options);
607 painter.
draw(path, options);
621 painter.
draw(path, options);
626 painter.
draw(path, options);
631 painter.
draw(path, options);
660 double totWidth = hyperHighEdge - hyperLowEdge;
661 int lowBin = histogram->GetXaxis()->FindFixBin(hyperLowEdge);
662 int highBin = histogram->GetXaxis()->FindFixBin(hyperHighEdge);
664 if (lowBin==highBin) histogram->Fill(hyperLowEdge, content);
669 double widthInLowBin = histogram->GetXaxis()->GetBinUpEdge(lowBin) - hyperLowEdge;
670 double widthInHighBin = hyperHighEdge - histogram->GetXaxis()->GetBinLowEdge(highBin);
671 double eventsInLowBin = (widthInLowBin /totWidth)*content;
672 double eventsInHighBin = (widthInHighBin/totWidth)*content;
673 histogram->Fill(hyperLowEdge , eventsInLowBin);
674 histogram->Fill(hyperHighEdge, eventsInHighBin);
678 for(
int bin = (lowBin + 1); bin <= (highBin - 1); bin++){
679 double lowEdge = histogram->GetXaxis()->GetBinLowEdge(bin);
680 double highEdge = histogram->GetXaxis()->GetBinUpEdge (bin);
681 double events = ((highEdge - lowEdge)/totWidth)*content;
682 histogram->Fill( histogram->GetXaxis()->GetBinCenter(bin) , events);
694 double volume = hyperVolume.
volume();
695 for(
int i = 0; i < hyperVolume.
size(); i++){
697 double cuboidVolume = cuboid.
volume();
698 double cuboidContent = (content*cuboidVolume)/volume;
699 project(histogram, cuboid, cuboidContent, dimension);
708 ERROR_LOG <<
"HyperHistogram::getDimension - cannot get dimension, binning not set." << std::endl;
726 std::vector<double> binContents;
727 std::vector<double> binErrors ;
733 for (
int i = 0; i <
getNBins(); i++){
739 if (slicedVol.
size() == 0)
continue;
751 for (
unsigned i = 0; i < binContents.size(); i++){
770 int nSlices = slicePoints.
size();
773 std::vector< std::vector<double> > binContents (nSlices, std::vector<double>(0, 0.0) );
774 std::vector< std::vector<double> > binErrors (nSlices, std::vector<double>(0, 0.0) );
780 for (
int i = 0; i <
getNBins(); i++){
786 for (
int sli = 0; sli < nSlices; sli++){
789 if (slicedVol.
size() == 0)
continue;
791 slicedBinnings.
at(sli).addHyperVolume( slicedVol );
792 binContents .at(sli).push_back ( content );
793 binErrors .at(sli).push_back ( error );
801 std::vector< HyperHistogram > slicedHist;
802 slicedHist.reserve(nSlices);
804 for (
int sli = 0; sli < nSlices; sli++){
808 int nBins = binContents.at(sli).size();
809 for (
int bin = 0; bin < nBins; bin++){
810 slicedHist.at(sli).setBinContent(bin, binContents.at(sli).at(bin) );
811 slicedHist.at(sli).setBinError (bin, binErrors .at(sli).at(bin) );
815 slicedHist.at(sli).setMin (
getMin() );
816 slicedHist.at(sli).setMax (
getMax() );
827 std::vector<int > sliceDims;
830 if (i == sliceDimX)
continue;
831 if (i == sliceDimY)
continue;
832 sliceDims.push_back( i );
836 sliceHist.
draw(path, options);
845 ERROR_LOG <<
"Why would you take a 2D slice of something with less than 3 dim." << std::endl;
849 int slicedimx = random->Integer(dim);
850 int slicedimy = random->Integer(dim);
851 while( slicedimx == slicedimy ){
852 slicedimy = random->Integer(dim);
857 draw2DSlice(path, slicedimx, slicedimy, slicepoint, options);
863 std::vector<int > _sliceDims;
866 if (i == sliceDimX)
continue;
867 if (i == sliceDimY)
continue;
868 _sliceDims.push_back( i );
878 double width = (max - min)/
double(nSlices);
880 std::vector<TString> paths;
882 for (
int i = 0; i < nSlices; i++){
883 double val = min + width*(i + 0.5);
884 slicePointCp.
at(sliceSetDim) = val;
888 TString uniquePath = path;
889 uniquePath +=
"_sliceNum";
892 paths.push_back(uniquePath);
896 std::vector<HyperHistogram> hists =
slice(_sliceDims, slicePoints);
898 for (
unsigned i = 0; i < hists.size(); i++){
899 hists.at(i).draw(paths.at(i), options);
907 std::vector<int > sliceDims;
910 if (i == sliceDimX)
continue;
911 if (i == sliceDimY)
continue;
912 sliceDims.push_back( i );
917 std::vector<TString> paths;
920 for (
unsigned j = 0; j < sliceDims.size(); j++){
924 int sliceSetDim = sliceDims.at(j);
928 double width = (max - min)/
double(nSlices);
930 TString pathj = path;
932 pathj += sliceSetDim;
934 for (
int i = 0; i < nSlices; i++){
935 double val = min + width*(i + 0.5);
936 slicePointCp.
at(sliceSetDim) = val;
940 TString uniquePath = pathj;
941 uniquePath +=
"_sliceNum";
944 paths.push_back(uniquePath);
952 std::vector<HyperHistogram> hists =
slice(sliceDims, slicePoints);
954 for (
unsigned i = 0; i < hists.size(); i++){
955 hists.at(i).draw(paths.at(i), options);
970 if (i >= j)
continue;
972 TString thsPath = path;
994 TH1D projection(name, name, bins, lowEdge, highEdge);
1001 for (
int i = 1; i <= projection.GetNbinsX(); i++){
1002 projection.SetBinError(i, 0.0);
1014 TH1D projection =
project(dim, bins);
1027 TString thisPath = path +
"_"; thisPath += i;
1037 TH1D projection =
project(dim, bins);
1038 TH1D projectionOther = other.
project(dim, bins,
"projection2");
1040 plotter.
add(&projectionOther);
1050 TString thisPath = path +
"_"; thisPath += i;
1060 TFile* file =
new TFile(filename,
"RECREATE");
1063 ERROR_LOG <<
"Could not open TFile in HyperHistogram::save(" << filename <<
")";
1083 ERROR_LOG <<
"It is only possible to saveToTxtFile when using HyperBinning. Doing nothing." << std::endl;
1091 std::ofstream myfile;
1092 myfile.open (filename);
1094 int widthForVolNum = ceil(log10(nVolumes)) + 2;
1096 for (
int i = 0; i < nVolumes; i++ ){
1099 int binNumber = hyperBinning.
getBinNum(i);
1100 double content = -1.0;
1101 double error = -1.0;
1108 if (binNumber != -1){
1114 myfile << std::setw(widthForVolNum) << std::left << i;
1116 TString binType =
"";
1117 if (isPrimary ) myfile <<
"P";
1119 if (isBin ) myfile <<
"B";
1120 if (!isBin ) myfile <<
"V";
1122 myfile << std::setw(4) << std::left << binType;
1127 myfile << std::setw(width) << std::left << cube.
getLowCorner() << std::setw(width) << std::left << cube.
getHighCorner();
1130 myfile << std::setw(10) << std::left << content;
1131 if (incError) myfile << std::setw(10) << std::left << error;
1135 for (
unsigned j = 0; j < linkedBins.size(); j++){
1136 myfile << std::setw(10) << std::left << linkedBins.
at(j);
1140 myfile << std::endl;
1151 ERROR_LOG <<
"It is only possible to saveToTxtFile when using HyperBinning. Doing nothing." << std::endl;
1159 std::ofstream myfile;
1160 myfile.open (filename);
1162 for (
int i = 0; i < nBins; i++ ){
1170 myfile << std::setw(width) << std::left << cube.
getLowCorner() <<
" " << std::setw(width) << std::left << cube.
getHighCorner();
1172 myfile << std::setw(10) << std::left << content;
1173 if (incError) myfile << std::setw(10) << std::left << error;
1175 myfile << std::endl;
1189 TFile* file =
new TFile(filename,
"READ");
1192 ERROR_LOG <<
"Could not open TFile in HyperBinning::load(" << filename <<
")";
1196 TTree* tree = (TTree*)file->Get(
"HyperBinning");
1200 return "HyperBinning";
1218 if (binningType.Contains(
"HyperBinning")){
1232 if (binningType ==
""){
1233 ERROR_LOG <<
"HyperHistogram::load - I could not find any binning scheme in this file" << std::endl;
1243 if (binningType.Contains(
"HyperBinning")){
1251 if (binningType ==
""){
1252 ERROR_LOG <<
"HyperHistogram::loadEmpty - I could not find any binning scheme in this file" << std::endl;
1271 bool diskResidentBinning = 0;
1272 TString filename =
"";
1281 if (diskResidentBinning){
1283 TFile *file =
new TFile(filename,
"update");
1284 std::string object_to_remove=
"HistogramBase";
1285 gDirectory->Delete(object_to_remove.c_str());
1292 GOODBYE_LOG <<
"Goodbye from the HyperHistogram() Constructor";
void loadBase(TString filename)
virtual bool isDiskResident() const
double getMax(int dimension) const
virtual TString filename() const
bool isPrimaryVolume(int volumeNumber) const
virtual void draw(TString path="", TString option="")
double getBinError(int bin) const
void setNames(HyperName names)
virtual void load(TString filename, TString option="READ")=0
HyperName getNames() const
virtual void mergeBinnings(const BinningBase &other)=0
HyperPoint getRandomPoint(TRandom *random=gRandom) const
void loadEmpty(TString filename, TString option="MEMRES READ", TString binningType="HyperBinning")
void draw(TString path, TString options="")
void drawDensity(TString path, TString options="")
virtual bool addHyperVolume(const HyperVolume &hyperVolume, std::vector< int > linkedVolumes=std::vector< int >(0, 0))=0
double getMin(int dimension) const
virtual BinningBase * clone() const =0
const HyperCuboid & at(int i) const
virtual void merge(const HistogramBase &other)
std::vector< double > _binContents
bool exists(const string &fname)
virtual void merge(const HistogramBase &other)
void push_back(const HyperCuboid &hyperCuboid)
void addAlgOption(AlgOption option)
Add an AlgOption which is passed to the binning algortihm.
virtual HyperVolume getHyperVolume(int volumeNumber) const =0
virtual void load(TString filename, TString option="READ")=0
void setBinError(int bin, double val)
std::vector< double > _sumW2
virtual std::vector< int > getLinkedHyperVolumes(int volumeNumber) const =0
double getBinContent(int bin) const
virtual int getNumHyperVolumes() const =0
TString getBinningType() const
void mergeBinsWithSameContent()
void setBinContent(int bin, double val)
virtual void draw(TString path="", TString option="")
void draw2DSliceSet(TString path, int sliceDimX, int sliceDimY, int sliceSetDim, int nSlices, const HyperPoint &slicePoint, TString options="") const
virtual void addPrimaryVolumeNumber(int volumeNumber)=0
void fillBase(int binNum, double weight)
virtual std::vector< int > getPrimaryVolumeNumbers() const
virtual int getNumBins() const =0
void plotWithRatio(TString plotDirectory, TString plotOptions="", TPad *pad=0)
void drawRandom2DSlice(TString path, TRandom *random=gRandom, TString options="") const
const int & getDimension() const
HyperHistogram * getHyperBinningHistogram() const
void drawProjection(TString path, int dim=0, int bins=100) const
HyperBinningMaker * getHyperBinningMaker(HyperCuboid binningRange, HyperPointSet points)
virtual HyperVolume getBinHyperVolume(int binNumber) const
const HyperPoint & at(int i) const
virtual int getBinNum(const HyperPoint &coords) const =0
void useDensity(bool val)
void draw2DSlice(TString path, int sliceDimX, int sliceDimY, const HyperPoint &slicePoint, TString options="") const
virtual void save(TString filename) const =0
const int & getDimension() const
void resetBinContents(int nBins)
const HyperPoint & getHighCorner() const
virtual void draw(TString path="", TString option="")
HyperHistogram & operator=(const HyperHistogram &other)
void drawAllProjections(TString path, int bins) const
void setNames(HyperName names)
virtual void plot(TString plotDirectory, TString plotOptions="", TPad *pad=0, double scaleFactor=1.0)
virtual void makeBinning()=0
const TString & at(int dim) const
int fill(const HyperPoint &coords, double weight)
void reserveCapacity(int nElements)
const int & getDimension() const
void print(std::ostream &os=std::cout, int endline=1) const
HyperCuboid getLimits() const
virtual double getVal(const HyperPoint &point) const =0
const double & at(int i) const
void setContentsFromFunc(const HyperFunction &func)
void project(TH1D *histogram, const HyperCuboid &cuboid, double content, int dimension) const
virtual double getVal(const HyperPoint &point) const
virtual void reserveCapacity(int nElements)
const HyperPoint & getLowCorner() const
int estimateCapacity(std::vector< TString > filename, TString binningType)
unsigned int size() const
void add(TObject *histogram)
virtual int getNumBins() const
virtual ~HyperHistogram()
const BinningBase & getBinning() const
double getWeight(int i=0) const
void setFuncLimits(const HyperCuboid &limits)
const HyperCuboid & getFuncLimits() const
HyperVolume slice(const HyperPoint &coords, std::vector< int > dims) const
void save(TString filename)
int getBinNum(int volumeNumber) const
void compareProjection(TString path, int dim, const HyperHistogram &other, int bins=100) const
void load(TString filename, TString option="MEMRES READ")
virtual double getBinVolume(int bin) const
HyperName getNames() const
const HyperCuboid & getHyperCuboid(int i) const
void saveToTxtFileNoLinks(TString filename, bool incError=true) const
HyperPoint getAverageCenter() const
virtual HyperCuboid getLimits() const =0
void compareAllProjections(TString path, const HyperHistogram &other, int bins=100) const
TString getBinningType(TString filename)
virtual bool addHyperVolume(const HyperVolume &hyperVolume, std::vector< int > linkedVolumes=std::vector< int >(0, 0))
void push_back(const HyperPoint &point)
virtual HyperVolume getBinHyperVolume(int binNumber) const =0
HyperHistogram slice(std::vector< int > sliceDims, const HyperPoint &slicePoint) const
void saveToTxtFile(TString filename, bool incError=true) const