This is the macro where I create the root file. It’s identical to the other but here I create the root file that later I pass to the other macro to get one histogram
Double_t PeakFitErf(Double_t *x, Double_t *p){
return TMath::Erf((x[0]-p[1])/sqrt(2)/p[2])/2 +p[4];
}
Double_t PeakFit(Double_t *x, Double_t *p){
return (p[0]* exp( -0.5 * ((x[0]-p[1])/p[2]) * ((x[0]-p[1])/p[2])))+ p[3]*TMath::Erf((x[0]-p[1])/sqrt(2)/p[2])/2 +p[4];
}
void TryPush()
{
//TFile * inputfile = new TFile("TryPush.root","READ");
TFile* inputfile = TFile::Open("TryPush1.root");
//---list of vector that I use for the program
vector<Float_t>SBdx;
vector<Float_t>SBsx;
vector<Float_t>Count;
vector<Float_t>Err;
vector<TH1F*> histos;
vector<Int_t> Time;
//-----list of histogram
TH1F * hh = new TH1F("hh", "hh", 16384, 0,16383);
TH1F * h1;
TH1F * hc;//istogramma che poi clono per plottare
TH1F *hccal;
//------list of fitting function
TF1 * fitfondo;
TF1 *fit;
//----convertion factors for calibration
Double_t p=0.2318;
Double_t q=-0.04483;
int i=0;
double num1=0;
ifstream infile;
char filename[15];
char histoname[15];
int Ngroup = 10;//questo è il numero di istogrammi che raggruppa ad ogni ciclo
int Nhours = 5;//questo è il numero di ore che voglio
int Nhistos = Nhours*2;//numero di istogrammi che somma
for(int j=0; j<Nhistos; j++) {// questo ciclo è quello che prende gli istogrammi da raggruppare
for(int l=0; l<Ngroup; l++) {//questo ciclo con gli if dentro mi apre i file da sommare e li mette tutti insieme a seconda di quanti ne voglio insieme
sprintf(histoname, "h%d", l+j*Ngroup);
h1 = new TH1F(histoname,"Quartz",16384,0,16383);
if(l+j*Ngroup<10) {
sprintf(filename, "LoAx00%d.txt", l+j*Ngroup);
} else if(l+j*Ngroup<100){
sprintf(filename, "LoAx0%d.txt", l+j*Ngroup);
}else if(l+j*Ngroup<1000){
sprintf(filename, "LoAx%d.txt", l+j*Ngroup);
}
cout << filename << endl;
infile.open(filename);
//----------questo if riempie l'istogramma
if(infile.is_open()) {
i=0;
while (1) {
i+=1;
infile >> num1;
h1-> Fill(i,num1);
if (infile.eof()) break;
}
infile.close();
} else {
cout << "Failed to open file!" << endl;
}
hh->Add(h1, +1);
h1->Reset();
}
hc = (TH1F*)hh->Clone();
hccal= new TH1F(histoname, "Quartz", 16384,0,(16383*p)+q);
for(int i=0; i<hc->GetNbinsX(); i++){
hccal->SetBinContent(i, hc->GetBinContent(i));
}
//-------fitting the peak
fitfondo = new TF1("fitfondo", PeakFitErf,310.5,313.4,3);
fit= new TF1("fit", PeakFit ,310.5,313.4,5);
fitfondo-> SetParName(1,"offsetshelf");
fitfondo-> SetParameter(1, 80);
fitfondo-> SetParName(2,"shiftshelf");
fitfondo-> SetParameter(2, 388);
fit-> SetParName(0,"normgaus");
fit-> SetParameter(0,800);
fit-> SetParName(1,"mean");
fit-> SetParameter(1,312.);
fit-> SetParName(2,"sigma");
fit-> SetParameter(2, 3);
fit-> SetParName(3,"offsetshelf");
fit-> SetParameter(3, 80);
fit-> SetParName(4,"shiftshelf");
fit-> SetParameter(4, 388);(1, 80);
hccal-> Fit(fit, "R", "L");
hccal-> Fit(fitfondo, "R", "L");
fitfondo-> SetLineColor(kGreen);
fit-> SetLineColor(kRed);
fit-> Draw("same");
fitfondo-> Draw("same");
Double_t sigma =fit-> GetParameter(2);
Double_t media=fit-> GetParameter(1);
Double_t MediaMeno= media-(2*sigma);
Double_t MediaPiu= media+(2*sigma);
Double_t conteggi= fit-> Integral(hccal->GetBin(MediaMeno),hccal->GetBin(MediaPiu));
Double_t error= sqrt(conteggi);
Double_t conteggifondo= fitfondo-> Integral(hccal->GetBin(MediaMeno),hccal->GetBin(MediaPiu));
Double_t conteggieffettivi=conteggi-conteggifondo;
Double_t erroredef= sqrt(conteggieffettivi);
Double_t sideband_sx= hccal-> Integral(hccal->GetBin(282),hccal->GetBin(293));//metti il umeor del fit che ti interessa
Double_t sideband_dx= hccal-> Integral(hccal-> GetBin(347), hccal->GetBin(358));
//------cout useful
cout<< "area fit="<<conteggi << endl;
cout<<"errore area fit="<<error<<endl;
cout<<"energia picco fit="<<media<<endl;
cout<<"conteggi del fondo="<<conteggifondo<<endl;
cout<<"conteggi con fondo sottratto="<<conteggieffettivi<<endl;
cout<<"errore conteggi finale="<<erroredef<<endl;
cout<<"sbsx="<<sideband_sx<<endl;
cout<<"sbdx="<<sideband_dx<<endl;
//------push back values filling the vectors
SBdx.push_back(sideband_dx);//sideband destra
SBsx.push_back(sideband_sx);//side band sinistra
Count.push_back(conteggieffettivi);//CONTEGGI DEL PICCO
Err.push_back(erroredef);//errore sul count del picco
histos.push_back(hccal);
hh->Reset();
}
TCanvas *c1 = new TCanvas("c1","histogram1",1000,700);
//TH1F * ht = (TH1F*)inputfile->Get(histos[7]);
//ht->SetLineColor(1);
//ht->Draw();
histos[0]->SetTitle("Quartz etched-measure for 10h");
histos[0]->GetXaxis()->SetTitle("Energy[KeV]");
histos[0]->GetYaxis()->SetTitle("Count[a.u]");
histos[0]->SetLineColor(6);
histos[0]->Draw("histo");
//------------queste 3 righe mi servono in teoria per.
//-prendere in considerazione inputfile;
//-fare il graph e mettere dentro inputfile;
//-disegnare insieme a histos[0] del file corroso.
//inputfile->ls();
//TGraph* g = (TGraph*) inputfile -> Get("histos[7]");
//g->Draw("same");
//------cout of values of side bands
//------ofstream to put in a txt file the results
ofstream outfile("risultati");
for(i=0; i<10; i++){
cout<<"Sidebandsx="<<SBsx.at(i)<<endl;
cout<<"Sidebanddx="<<SBdx.at(i)<<endl;
cout<<"peak count="<<Count.at(i)<<endl;
cout<<"errore conteggi="<<Err.at(i)<<endl;
outfile<< SBsx.at(i)<<" "<<SBdx.at(i)<<" "<<Count.at(i)<<endl;
}
//-----plotting the data in a TGraph
TCanvas *c2 =new TCanvas ("C2","SIDEBANDS & COUNT RATE",1000,700);
int n=10;
int time[n];
int count[n];
int sbsx[n];
int sbdx[n];
int errorcount[n];
for(int i=0; i<10; i++){
time[i]=i+1;
//cout<<"time="<<time[i]<<endl;
count[i]=Count.at(i);
sbsx[i]= SBsx.at(i);
sbdx[i]= SBdx.at(i);
errorcount[i]=Err.at(i);
//-----------tgraph per i sidebands e i count rate
TGraph * grcount = new TGraph(n, &time[i], &count[i]);
TGraph * grsbsx = new TGraph(n, &time[i], &sbsx[i]);
TGraph * grsbdx = new TGraph(n, &time[i], &sbdx[i]);
grcount->SetTitle("Sidebands and count rate");
grcount->GetXaxis()->SetTitle("time[h]");
grcount->GetYaxis()->SetTitle("Count[a.u]");
grcount->SetLineColor(1);//black
grcount->SetLineWidth(2);
grcount->SetMarkerColor(4);
grcount->SetMarkerSize(1.5);
grcount->SetMarkerStyle(21);
grsbsx->SetLineColor(4);//blue
grsbsx->SetLineWidth(2);
grsbsx->SetMarkerColor(5);
grsbsx->SetMarkerSize(1.5);
grsbsx->SetMarkerStyle(21);
grsbdx->SetLineColor(6);//magenta
grsbdx->SetLineWidth(2);
grsbdx->SetMarkerColor(6);
grsbdx->SetMarkerSize(1.5);
grsbdx->SetMarkerStyle(21);
grcount->Draw("AC*");
grsbsx->Draw("same");
grsbdx->Draw("same");
}
return 0;
}
I’m not an expert of root file