make_binned_and_fitted_to_root.C

#include "TFile.h"
#include "TGraphErrors.h"
#include "TCanvas.h"
#include "TAxis.h"
//g++ `root-config --libs --cflags --ldflags` make_graph.C -o make_graph
//.x ./make_binned_and_fitted_to_root.C("binned_and_fitted_channel_signal_1_channel_trigger_2_pulse_length_3.csv", "graphs.root", "title", "x_name", "y_name")
#include "Riostream.h"
using namespace std;

void make_binned_and_fitted_to_root(TString csv_file, TString root_file, TString Graph_Name="", TString x_name="", TString y_name=""){
    ifstream in;
    in.open(csv_file);
    TFile *f = new TFile(root_file, "UPDATE");
    TGraphErrors *g1 = new TGraphErrors();
    TCanvas *c1 = new TCanvas(Graph_Name);
    Double_t dac, A, mu, sigma;
    Int_t point = 0;

    while(1){
        in>>dac>>A>>mu>>sigma;
        point++;
        if (!in.good()){
            break;
        }
        g1->Set(point);
        g1->SetPoint(point, dac, mu);
        g1->SetPointError(point, 50, sigma);
    }
    c1->cd();
    g1->SetTitle(Graph_Name);
    g1->GetXaxis()->SetTitle(x_name);
    g1->GetYaxis()->SetTitle(y_name);
    g1->Draw("ap");
    f->cd();
    c1->Write();
    f->Close();
    delete f;
    delete c1;
    delete g1;
}

int main(int argc, char **argv){
    switch(argc){
        case 3:
            make_binned_and_fitted_to_root(argv[1], argv[2]);
            break;
        case 4:
            make_binned_and_fitted_to_root(argv[1], argv[2], argv[3]);
            break;
        case 5:
            make_binned_and_fitted_to_root(argv[1], argv[2], argv[3],argv[4]);
            break;
        case 6:
            make_binned_and_fitted_to_root(argv[1], argv[2], argv[3],argv[4],argv[5]);
            break;
        default:
            return 1;
    }
    return 0;
}

make_graph.C

#include "TFile.h"
#include "TGraph.h"
#include "TCanvas.h"
#include "TAxis.h"
//g++ `root-config --libs --cflags --ldflags` make_graph.C -o make_graph
#include "Riostream.h"
using namespace std;

void make_graph(TString csv_file, TString root_file, TString Graph_Name="", TString x_name="", TString y_name=""){
    ifstream in;
    in.open(csv_file);
    TFile *f = new TFile(root_file, "UPDATE");
    TGraph *g1 = new TGraph();
    TCanvas *c1 = new TCanvas(Graph_Name);
    Double_t x, y;
    Int_t point = 0;

    while(1){
        in>>x>>y;
        point++;
        if (!in.good()){
            break;
        }
        g1->Set(point);
        g1->SetPoint(point, x, y);
    }
    c1->cd();
    g1->SetTitle(Graph_Name);
    g1->GetXaxis()->SetTitle(x_name);
    g1->GetYaxis()->SetTitle(y_name);
    g1->Draw("ap");
    f->cd();
    c1->Write();
    f->Close();
}

int main(int argc, char **argv){
    switch(argc){
        case 3:
            make_graph(argv[1], argv[2]);
            break;
        case 4:
            make_graph(argv[1], argv[2], argv[3]);
            break;
        case 5:
            make_graph(argv[1], argv[2], argv[3],argv[4]);
            break;
        case 6:
            make_graph(argv[1], argv[2], argv[3],argv[4],argv[5]);
            break;
        default:
            return 1;
    }
    return 0;
}

Published

Category

snippets

Tags