From 4d0b94e1456b44d3eb71dd4cd8aa4882be3faaee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Gy=C3=B6ngy=C3=B6si?= Date: Mon, 6 Nov 2017 22:58:01 +0100 Subject: [PATCH] Added simulate functions(Crusher.cc, Draw2D.cc) created by K.Berta and Gy.Balazs Minor fixes and optimisation on other modules --- include/BM1DProcess.hh | 16 +++---- include/Crusher.hh | 37 ++++++++++++++++ include/Draw2D.hh | 43 ++++++++++++++++++ include/Lattice.hh | 20 ++++++--- include/Plotter.hh | 2 +- src/Analyse.cc | 12 ++++-- src/BM1DProcess.cc | 20 ++++++--- src/Crusher.cc | 92 +++++++++++++++++++++++++++++++++++++++ src/Draw2D.cc | 98 ++++++++++++++++++++++++++++++++++++++++++ src/Lattice.cc | 33 +++++++++++--- src/Plotter.cc | 5 ++- 11 files changed, 346 insertions(+), 32 deletions(-) create mode 100644 include/Crusher.hh create mode 100644 include/Draw2D.hh create mode 100644 src/Crusher.cc create mode 100644 src/Draw2D.cc diff --git a/include/BM1DProcess.hh b/include/BM1DProcess.hh index ee6c71c..24e6107 100644 --- a/include/BM1DProcess.hh +++ b/include/BM1DProcess.hh @@ -15,17 +15,17 @@ public: BM1DProcess(); ~BM1DProcess(); - void Run(int nSteps, int nRuns, double p0, double p1); - void Run(int nSteps, int nRuns, double p0, double mu, double sigma); - void Run(int nSteps, int nRuns, double p0, double x1, double x2, double mu1, double sigma1, double mu2, double sigma2); - void Run(int nRuns, int nSteps, double p0, double x1, double x2, double mu1, double sigma1, double mu2, double sigma2, double j_mu1, double j_sigma1, double rat); + void Run(const int & nRuns, const int & nSteps, const double & p0, const double & p1); + void Run(const int & nRuns, const int & nSteps, const double & p0, const double & mu, const double & sigma); + void Run(const int & nRuns, const int & nSteps, const double & p0, const double & x1, const double & x2, const double & mu1, const double & sigma1, const double &mu2, const double & sigma2); + void Run(const int & nRuns, const int & nSteps, const double & p0, const double & x1, const double & x2, const double & mu1, const double & sigma1, const double &mu2, const double & sigma2, const double & j_mu1, const double & j_sigma1, const double & rat); - std::vector GetT(){return t;} - std::vector GetX(){return x;} + const std::vector & GetT(){return t;} + const std::vector & GetX(){return x;} private: - TRandom* randomGenerator; - TRandom* randomGeneratorGauss; + TRandom3* randomGenerator; + TRandom3* randomGeneratorGauss; //TF1* jump; Double_t rand1; std::vector t; diff --git a/include/Crusher.hh b/include/Crusher.hh new file mode 100644 index 0000000..a153fcb --- /dev/null +++ b/include/Crusher.hh @@ -0,0 +1,37 @@ +#ifndef Crusher_h +#define Crusher_h 1 + +#include + + +#include "BM1DProcess.hh" +#include "Analyse.hh" +#include "Lattice.hh" + +class Crusher +{ + public: + Crusher(std::vector tp, std::vector xp, double p0p); + Crusher(std::vector tp, std::vector xp); + ~Crusher(); + + bool RunMachine(const int & nRunsm, const MuSigma & parameters); + + const std::vector & GetMulT() {return multipleRunVectorT;} + const std::vector & GetMulX() {return multipleRunVectorX;} + + private: + int nSteps; + double p0; + std::vector t; + std::vector x; + + BM1DProcess *myBM1DProcess; + Analyse *myAnalyse; + + bool Bigger(double percent); + + std::vector multipleRunVectorT; + std::vector multipleRunVectorX; +}; +#endif \ No newline at end of file diff --git a/include/Draw2D.hh b/include/Draw2D.hh new file mode 100644 index 0000000..b6d237b --- /dev/null +++ b/include/Draw2D.hh @@ -0,0 +1,43 @@ +#pragma once + +#include +#include +#include +#include +#include + + +#include "Analyse.hh" +#include "Lattice.hh" +#include "Crusher.hh" +#include "Plotter.hh" + +#include "TH2.h" +#include "TCanvas.h" +#include "TGraph.h" + +class Draw2D +{ + public: + + Draw2D(int nop, double percent, int nRunsp, std::vector tp, std::vector xp); + ~Draw2D(); + + void Histo2D(); + + private: + std::vector t; + std::vector x; + int nRuns; + + Analyse *myAnalyse; + Crusher *myCrusher; + Lattice *myLattice; + TCanvas* canvasB; + TCanvas* canvasB2; + TH2D *BM1D_histo; + TH2D *BM1D_histo2; + TGraph *gr; + + +}; \ No newline at end of file diff --git a/include/Lattice.hh b/include/Lattice.hh index 98e1d08..bba9938 100644 --- a/include/Lattice.hh +++ b/include/Lattice.hh @@ -1,5 +1,6 @@ -#ifndef Lattice_h -#define LAttice_h 1 +//#ifndef Lattice_h +//#define LAttice_h 1 +#pragma once #include #include @@ -29,15 +30,20 @@ public: ~Lattice(); void SetNop(Int_t num); //set the width of the lattice - void SetPercent(Int_t num); //set the interval of mu, sigma + void SetPercent(Double_t num); //set the interval of mu, sigma void SetLattice(Double_t mu_est, Double_t sigma_est); //set the lattice around mu_est, sigma_est - MuSigma GetMuSigma(Int_t index); + const MuSigma & GetMuSigma(Int_t index); Int_t GetLatticeSize(); //number of points in the lattice - Int_t GetWidth(); //number of mu values - Int_t GetHeight(); //number of sigma values + const Int_t & GetWidth(); //number of mu values + const Int_t & GetHeight(); //number of sigma values + + const double & GetMuMin(); + const double & GetMuMax(); + const double & GetSigmaMin(); + const double & GetSigmaMax(); private: @@ -66,4 +72,4 @@ private: ************************************************************************/ -#endif +//#endif diff --git a/include/Plotter.hh b/include/Plotter.hh index 2541f5a..eafaa00 100644 --- a/include/Plotter.hh +++ b/include/Plotter.hh @@ -24,7 +24,7 @@ class Plotter public: Plotter(bool draw); ~Plotter(); - void Plot(Int_t numRuns, Int_t nSteps, std::vector t, std::vector x); + void Plot(Int_t numRuns, Int_t nSteps, const std::vector & t, const std::vector & x); TMultiGraph * GetTmultiGraph(); static unsigned int IDCounter; diff --git a/src/Analyse.cc b/src/Analyse.cc index 158341c..2de4f4b 100644 --- a/src/Analyse.cc +++ b/src/Analyse.cc @@ -2,10 +2,16 @@ Analyse::Analyse() : _p0(0), _mu(0), _sigma(0) { canvA = new TCanvas("cancA","display",800,400); - dxhisto = new TH1D("dxhisto","dxhisto", 50, -5.0,5.0); + //dxhisto = new TH1D("dxhisto","dxhisto", 50, -5.0,5.0); } -Analyse::~Analyse() {;} +Analyse::~Analyse() + { + canvA->Clear(); + canvA->Closed(); + delete canvA; + delete dxhisto; + } Double_t Analyse::GetP0() { @@ -96,5 +102,5 @@ void Analyse::AnalyseGaus(std::vector t, std::vector x){ std::cout << "mu = " << _mu << std::endl; std::cout << "sigma = " << _sigma << std::endl; - + std::cout << "Analyse done" << std::endl; } diff --git a/src/BM1DProcess.cc b/src/BM1DProcess.cc index 6f00a7e..77334a4 100644 --- a/src/BM1DProcess.cc +++ b/src/BM1DProcess.cc @@ -2,8 +2,11 @@ BM1DProcess::BM1DProcess() { - randomGenerator = new TRandom(); - randomGeneratorGauss = new TRandom(); + randomGenerator = new TRandom3(); + randomGenerator -> SetSeed(); + randomGeneratorGauss = new TRandom3(); + randomGeneratorGauss -> SetSeed(); + std::cout << "seed:" << randomGeneratorGauss -> GetSeed() << std::endl; //jump = new TF1("jump","gaus(0)+gaus(3)",-30,30); } @@ -16,7 +19,7 @@ BM1DProcess::~BM1DProcess() } -void BM1DProcess::Run(int nRuns, int nSteps, double p0, double p1) +void BM1DProcess::Run(const int & nRuns, const int & nSteps, const double & p0, const double & p1) { t.clear(); x.clear(); @@ -54,8 +57,11 @@ void BM1DProcess::Run(int nRuns, int nSteps, double p0, double p1) } } -void BM1DProcess::Run(int nRuns, int nSteps, double p0, double mu, double sigma) + + +void BM1DProcess::Run(const int & nRuns, const int & nSteps, const double & p0, const double & mu, const double & sigma) { + t.clear(); x.clear(); @@ -82,9 +88,11 @@ void BM1DProcess::Run(int nRuns, int nSteps, double p0, double mu, double sigma) } } } + + } -void BM1DProcess::Run(int nRuns, int nSteps, double p0, double x1, double x2, double mu1, double sigma1, double mu2, double sigma2) +void BM1DProcess::Run(const int & nRuns, const int & nSteps, const double & p0, const double & x1, const double & x2, const double & mu1, const double & sigma1, const double &mu2, const double & sigma2) { t.clear(); x.clear(); @@ -125,7 +133,7 @@ void BM1DProcess::Run(int nRuns, int nSteps, double p0, double x1, double x2, do } -void BM1DProcess::Run(int nRuns, int nSteps, double p0, double x1, double x2, double mu1, double sigma1, double mu2, double sigma2, double j_mu1, double j_sigma1, double rat) +void BM1DProcess::Run(const int & nRuns, const int & nSteps, const double & p0, const double & x1, const double & x2, const double & mu1, const double & sigma1, const double &mu2, const double & sigma2, const double & j_mu1, const double & j_sigma1, const double & rat) { t.clear(); x.clear(); diff --git a/src/Crusher.cc b/src/Crusher.cc new file mode 100644 index 0000000..d6ae3cd --- /dev/null +++ b/src/Crusher.cc @@ -0,0 +1,92 @@ +#include "Crusher.hh" + +Crusher::Crusher(std::vector tp, std::vector xp, double p0p) +{ + nSteps = tp.size(); + t = tp; + x = xp; + p0 = p0p; + + myBM1DProcess = new BM1DProcess(); + +} + +Crusher::Crusher(std::vector tp, std::vector xp) +{ + nSteps = tp.size(); + t = tp; + x = xp; + p0 = 0.5; + + myBM1DProcess = new BM1DProcess(); +} + +Crusher::~Crusher() +{ + delete myBM1DProcess; +} + +bool Crusher::Bigger(double percent) +{ + int counterBig = 0; + double limit = nSteps * percent; + + for(int i = 0; i < nSteps; i++) + { + + if(myBM1DProcess -> GetX()[i] < x[i]) // reference < origin + { + counterBig ++; + } + } + + + if(counterBig >= limit) + { + return true; + } + + else return false; +} + +bool Crusher::RunMachine(const int & nRuns, const MuSigma & parameters) +{ + int counter = 0; + + multipleRunVectorT.clear(); + multipleRunVectorX.clear(); + + for(int i = 0; i < nRuns; i++ ) + { + + + myBM1DProcess->Run(1, nSteps, p0, parameters.mu, parameters.sigma); + + //std::cout << "paramter mu:" << parameters.mu << " paramter sigma:" << parameters.sigma << std::endl; + + for(int i = 0; i < nSteps; i++) + { + multipleRunVectorT.push_back(myBM1DProcess -> GetT()[i]); + multipleRunVectorX.push_back(myBM1DProcess -> GetX()[i]); + } + + + if(Bigger(0.5)) + { + counter ++; + } + + + + } + + std::cout <<"mu:" << parameters.mu << " sigma:" << parameters.sigma << " in:" << counter < tp, std::vector xp) //int nop, double percent, int nRunsp, std::vector tp, std::vector xp +{ + t = tp; + x = xp; + nRuns = nRunsp; + + + myAnalyse = new Analyse(); + myAnalyse -> AnalyseGaus(t,x); + + myCrusher = new Crusher(t,x); + + myLattice = new Lattice(); + + myLattice -> SetNop(nop); + myLattice -> SetPercent(percent*100); + myLattice -> SetLattice(myAnalyse -> GetMu(), myAnalyse -> GetSigma()); + + canvasB = new TCanvas("draw2Dcanv", "Lattice",800,400); + canvasB -> Divide(2,1); + + canvasB2 = new TCanvas("draw2Dcanv2", "Lattice2",800,400); + + BM1D_histo = new TH2D("BM1D_histo", "BM1D_histo", myLattice -> GetWidth() , myLattice -> GetMuMin(), (myLattice -> GetMuMax()) , myLattice -> GetHeight() , myLattice -> GetSigmaMin(), (myLattice -> GetSigmaMax()) ); + BM1D_histo2 = new TH2D("BM1D_histo2", "BM1D_histo2", myLattice -> GetWidth() , myLattice -> GetMuMin(), (myLattice -> GetMuMax()) , myLattice -> GetHeight() , myLattice -> GetSigmaMin(), (myLattice -> GetSigmaMax()) ); + gr = new TGraph(); + + delete myAnalyse; +} + +Draw2D::~Draw2D() +{ + canvasB->Clear(); + canvasB->Closed(); + delete canvasB; + delete BM1D_histo; + delete BM1D_histo2; + + canvasB2->Clear(); + canvasB2->Closed(); + delete canvasB2; + delete gr; + + delete myLattice; + delete myCrusher; +} + +void Draw2D::Histo2D() +{ + double XbinSize = (myLattice -> GetMuMax() - myLattice -> GetMuMin())/ myLattice -> GetWidth() ; + double YbinSize =( myLattice -> GetSigmaMax() - myLattice -> GetSigmaMin() ) / myLattice -> GetHeight(); + + double graphArrayX[myLattice -> GetLatticeSize()] = {}; + double graphArrayY[myLattice -> GetLatticeSize()] = {}; + + std::cout << "maxSigma:" << myLattice -> GetSigmaMax() << std::endl; + std::cout << "maxMu:" << myLattice -> GetMuMax() << std::endl; + std::cout << "minSigma:" << myLattice -> GetSigmaMin() << std::endl; + std::cout << "minMu:" << myLattice -> GetMuMin() << std::endl; + + for(int i = 0; i < (myLattice -> GetLatticeSize()); i++) + { + std::cout<<"progress: "< GetLatticeSize()<<"point\n"; + + if(myCrusher -> RunMachine(nRuns, myLattice -> GetMuSigma(i))) + { + BM1D_histo -> Fill((i / myLattice -> GetHeight()) * XbinSize + (XbinSize/2) + myLattice -> GetMuMin(),(i % myLattice -> GetHeight()) * YbinSize + (YbinSize/2) + myLattice -> GetSigmaMin() ); + BM1D_histo2 -> Fill((i / myLattice -> GetHeight()) * XbinSize + (XbinSize/2) + myLattice -> GetMuMin(),(i % myLattice -> GetHeight()) * YbinSize + (YbinSize/2) + myLattice -> GetSigmaMin()); + + graphArrayX[i] = (i / myLattice -> GetHeight()) * XbinSize; + graphArrayY[i] = (i % myLattice -> GetHeight()) * YbinSize; + + } + + } + + + std::cout << "RunMachine done" << std::endl; + + //TGraph *gr = new TGraph(myLattice -> GetLatticeSize(),graphArrayX,graphArrayY); + + gr -> SetMarkerColor(46); + canvasB2 -> cd(); + //gr -> LineStyle() + gr -> DrawGraph(myLattice -> GetLatticeSize(),graphArrayX,graphArrayY,"AP*"); + + BM1D_histo -> SetMarkerStyle(kFullCircle); + BM1D_histo-> SetMarkerStyle(21); + canvasB -> cd(1); + BM1D_histo -> Draw("PLC PMC"); + + canvasB -> cd(2); + BM1D_histo2 -> Draw("COLZ"); + + std::cout << "Histo done" << std::endl; +} \ No newline at end of file diff --git a/src/Lattice.cc b/src/Lattice.cc index 327a4c9..fcfdccc 100644 --- a/src/Lattice.cc +++ b/src/Lattice.cc @@ -12,17 +12,18 @@ void Lattice::SetNop(Int_t num){ } -void Lattice::SetPercent(Int_t num){ +void Lattice::SetPercent(Double_t num){ if((num > 1) && (num <= 100)) _percent = num/100.0; - - else + else{ + _percent = num; std::cout << "Invalid value for percent, set to default" << std::endl; + } } -MuSigma Lattice::GetMuSigma(Int_t index){ +const MuSigma & Lattice::GetMuSigma(Int_t index){ return ms_vect[index]; @@ -34,18 +35,38 @@ Int_t Lattice::GetLatticeSize(){ } -Int_t Lattice::GetWidth(){ +const Int_t & Lattice::GetWidth(){ return _width; } -Int_t Lattice::GetHeight(){ +const Int_t & Lattice::GetHeight(){ return _height; } +const double & Lattice::GetMuMin() +{ + return ms_vect[0].mu; +} + +const double & Lattice::GetMuMax() +{ + return ms_vect[ _height * (_width-1)].mu; +} + +const double & Lattice::GetSigmaMin() +{ + return ms_vect[0].sigma; +} + +const double & Lattice::GetSigmaMax() +{ + return ms_vect[_height -1].sigma; +} + void Lattice::SetLattice(Double_t mu_est, Double_t sigma_est){ Double_t loc_sigma, loc_mu; diff --git a/src/Plotter.cc b/src/Plotter.cc index 8426dcf..b4de8d5 100644 --- a/src/Plotter.cc +++ b/src/Plotter.cc @@ -26,7 +26,7 @@ TMultiGraph * Plotter::GetTmultiGraph() } -void Plotter::Plot(Int_t numRuns, Int_t nSteps, std::vector t, std::vector x) +void Plotter::Plot(Int_t numRuns, Int_t nSteps,const std::vector & t, const std::vector & x) { char str[30]; snprintf(str, 30, "BM1D Plotter canvas%d", ID); @@ -45,7 +45,10 @@ void Plotter::Plot(Int_t numRuns, Int_t nSteps, std::vector t, std::ve if(draw) { + canv -> cd(); mg -> Draw("APL"); + std::cout << "Plotter done" << std::endl; + } }