Version 1.4 - New structure

This commit is contained in:
dbalazs92 2017-10-30 09:41:11 +01:00
parent 4adbdad44b
commit fc790d0de7
14 changed files with 157 additions and 227 deletions

83
BM1D.cc
View File

@ -1,81 +1,20 @@
/// BM1D program. Date: 2017-10-29 Creators: Balázs Demeter, Balázs Ujvári, Dávid Baranyai
#include <iostream>
#include "Progress.hh"
#include "BM1DProcess.hh"
#include "Plotter.hh"
#include "TApplication.h"
#include "TGraph.h"
using namespace std;
void OpenRootFile()
{
TFile *fIn = TFile::Open("Plot1.root", "READ");
fIn->ls();
TGraph *Ga;
TGraph *Gb;
fIn->GetObject("1", Ga);
fIn->GetObject("2", Gb);
Ga->Draw();
Gb->Draw("same");
delete fIn;
}
int main(int argc, char* argv[])
{
Int_t n = 1000, p0 = 1, start = 0;
TApplication App("tapp", &argc, argv);
TCanvas* Canv = new TCanvas("c10","Live display",800,400);
if(argc==2)
{
OpenRootFile();
}
else
{
Progress* Pr1 = new Progress(n,p0,start);
Progress* Pr2 = new Progress(n,p0,start);
Pr1->Count('u');
Pr2->Count('g');
vector<Double_t> x1 = Pr1->GetX();
vector<Double_t> y1 = Pr1->GetY();
vector<Double_t> x2 = Pr2->GetX();
vector<Double_t> y2 = Pr2->GetY();
TGraph* G1 = new TGraph(n,&x1[0],&y1[0]);
TGraph* G2 = new TGraph(n,&x2[0],&y2[0]);
TFile* fOut = new TFile("Plot1.root", "RECREATE");
G1->SetLineColor(1);
G1->SetLineWidth(1);
G1->SetMarkerColor(1);
G1->SetMarkerStyle(0);
G1->SetTitle("Brownian Movement D=1");
G1->GetXaxis()->SetTitle("X");
G1->GetYaxis()->SetTitle("Time");
G2->SetLineColor(2);
G2->SetLineWidth(1);
G2->SetMarkerColor(2);
G2->SetMarkerStyle(0);
G2->SetTitle("Brownian Movement D=1");
G2->GetXaxis()->SetTitle("X");
G2->GetYaxis()->SetTitle("Time");
G1->Draw();
G2->Draw("same");
G1->Write("1");
G2->Write("2");
fOut->Close();
}
App.Run();
return 0;
TApplication App("tapp", &argc, argv);
Int_t n = 1000;
BM1DProcess *myBM1DProcess = new BM1DProcess(n);
myBM1DProcess->Init();
myBM1DProcess->Run();
Plotter* myPlotter = new Plotter();
myPlotter->Plot(n, myBM1DProcess->GetT(), myBM1DProcess->GetX());
App.Run();
return 0;
}

View File

@ -51,6 +51,8 @@ If there's command line argument, then program opens the created root file. Name
## Updates
* 2017/10/30 - 'v1.4' - New Structure
* 2017/10/29 - 'v1.3.1' - Release Candidate
* 2017/10/29 - 'v1.3' - Minor fixes in Progress

35
include/BM1DProcess.hh Normal file
View File

@ -0,0 +1,35 @@
#ifndef BM1DProcess_h
#define BM1DProcess_h 1
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include "TRandom.h"
class BM1DProcess {
public:
BM1DProcess(Int_t n); //n is the number of steps
~BM1DProcess();
void SetP0(Double_t p0_new){p0 = p0_new;}
void SetP1(Double_t p1_new){p1 = p1_new;}
void Init();
void Run();
std::vector<Double_t> GetT(){return t;}
std::vector<Double_t> GetX(){return x;}
private:
Int_t nSteps;
Double_t p0,p1,p2,p3,p4;
TRandom* randomGenerator;
Double_t rand1;
std::vector<Double_t> t;
std::vector<Double_t> x;
};
#endif

0
include/BM1DProcess.hh~ Normal file
View File

32
include/Plotter.hh Normal file
View File

@ -0,0 +1,32 @@
#ifndef Plotter_h
#define Plotter_h 1
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include "TROOT.h"
#include "TF1.h"
#include "TH1.h"
#include "TH2.h"
#include "TH3.h"
#include "TNtuple.h"
#include "TFile.h"
#include "TMath.h"
#include "TCanvas.h"
#include "TGraph.h"
#include "TTree.h"
class Plotter {
public:
Plotter();
void Plot(Int_t n, std::vector<Double_t> t, std::vector<Double_t> x);
private:
TGraph* g1;
TFile* fOut;
TCanvas* canv;
TTree *BM1DTree;
Double_t tl,xl;
};
#endif

0
include/Plotter.hh~ Normal file
View File

View File

@ -1,58 +0,0 @@
/// BM1D program. Date: 2017-10-29 Creators: Balázs Demeter, Balázs Ujvári, Dávid Baranyai
#ifndef Progress_h
#define Progress_h 1
#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include "TROOT.h"
#include "TF1.h"
#include "TH1.h"
#include "TH2.h"
#include "TH3.h"
#include "TNtuple.h"
#include "TFile.h"
#include "TRandom.h"
#include "TMath.h"
#include "TCanvas.h"
class Progress {
public:
Progress(Int_t n);
Progress(Int_t n, Double_t p0_Set);
Progress(Int_t n, Double_t p0_Set, Double_t startposition);
~Progress();
void Count(char a);
Double_t GetP0();
Int_t GetSteps();
Double_t GetNext(char a);
Double_t GetPrevious();
void SetP0(double p0_new);
std::vector<Double_t> GetX();
std::vector<Double_t> GetY();
private:
TRandom* random1;
const Int_t n;
std::vector<Double_t> x;
std::vector<Double_t> y;
Double_t p0;
Double_t preVstate;
Int_t steps;
};
#endif

BIN
result.root Normal file

Binary file not shown.

6
setup.sh Executable file → Normal file
View File

@ -1,10 +1,6 @@
#!/bin/bash
#
# Author: Balázs Demeter (balazsdemeter92@gmail.com)
# Version: 1.0
#
# Script for build and make Brownian movement in one dimension
rm -rf bm1d_build
mkdir bm1d_build
cd bm1d_build
cmake ../

40
src/BM1DProcess.cc Normal file
View File

@ -0,0 +1,40 @@
#include "BM1DProcess.hh"
BM1DProcess::BM1DProcess(Int_t nP) :
nSteps (nP), p0(0.5), p1(0.3)
{
randomGenerator = new TRandom();
}
BM1DProcess::~BM1DProcess()
{
;
}
void BM1DProcess::Init(){
t.push_back(0.0); //let's start at t=0, x=0, you can change it if you vant, please use Set methods
x.push_back(0.0);
}
void BM1DProcess::Run(){
for (Int_t i = 1;i < nSteps;i++)
{
rand1 = randomGenerator->Uniform();
if(rand1 < p0) { //step in time, but no step in x
t.push_back(t[i-1] + 1);
x.push_back(x[i-1]);
}
else {//step left or right
if(rand1 < p0 + p1)
{
t.push_back(t[i-1]+1);
x.push_back(x[i-1]+1); //one step right
}
else
{
t.push_back(t[i-1]+1);
x.push_back(x[i-1]-1); //one step left
}
}
}
}

1
src/BM1DProcess.cc~ Normal file
View File

@ -0,0 +1 @@
#

35
src/Plotter.cc Normal file
View File

@ -0,0 +1,35 @@
#include "Plotter.hh"
Plotter::Plotter()
{
;
}
void Plotter::Plot(Int_t n, std::vector<Double_t> t, std::vector<Double_t> x){
fOut = new TFile("result.root", "RECREATE");
g1 = new TGraph(n,&t[0],&x[0]);
canv = new TCanvas("canc","display",800,400);
TTree *BM1DTree = new TTree("BM1DTree","BM1DTree");
g1->Draw();
g1->SetLineColor(1);
g1->SetLineWidth(1);
g1->SetMarkerColor(1);
g1->SetMarkerStyle(0);
g1->SetTitle("Brownian Movement D=1");
g1->GetYaxis()->SetTitle("X");
g1->GetXaxis()->SetTitle("Time");
BM1DTree->Branch("tl",&tl, "tl/D");
BM1DTree->Branch("xl",&xl, "xl/D");
for (unsigned int i = 0; i < t.size(); i++){
tl = t[i];
xl = x[i];
BM1DTree->Fill();
}
g1->Write();
BM1DTree->Write();
fOut->Close();
}

0
src/Plotter.cc~ Normal file
View File

View File

@ -1,92 +0,0 @@
/// BM1D program. Date: 2017-10-29 Creators: Balázs Demeter, Balázs Ujvári, Dávid Baranyai
#include "Progress.hh"
Progress::Progress(Int_t nP) :
n (nP), p0(1), preVstate(0), steps(0)
{
random1 = new TRandom();
}
Progress::Progress(Int_t nP, Double_t p0_Set) :
n (nP), p0(p0_Set), preVstate(0), steps(0)
{
random1 = new TRandom();
}
Progress::Progress(Int_t nP, Double_t p0_Set, Double_t startposition) :
n (nP), p0(p0_Set), preVstate(startposition), steps(0)
{
random1 = new TRandom();
}
Progress::~Progress()
{
}
void Progress::Count(char a)
{
for (Int_t i=0;i<n;i++)
{
if(i==0)
{
x.push_back(p0);
y.push_back(0.0);
}
x.push_back(GetNext(a));
y.push_back(steps);
}
}
Double_t Progress::GetNext(char a)
{
switch(a){
case 'u':
preVstate+=random1->Uniform(-p0,p0);
break;
case 'g':
preVstate+=random1->Gaus(-p0,p0);
break;
default:
std::cout<<"Invalid argument!"<<std::endl;
preVstate+=0.0;
break;
}
steps++;
return preVstate;
}
Double_t Progress::GetPrevious()
{
return preVstate;
}
void Progress::SetP0(Double_t p0_new)
{
p0=p0_new;
}
Double_t Progress::GetP0()
{
return p0;
}
Int_t Progress::GetSteps()
{
return steps;
}
std::vector<Double_t> Progress::GetX()
{
return x;
}
std::vector<Double_t> Progress::GetY()
{
return y;
}