CERN ROOT program 1 Reviews

CERN ROOT program is recorded tips:

  1. Root in THF bin from 1 starts counting (not 0), for example: for (int m = 1; m <= xbin; m ++) counts = HX-> GetBinContent (m); HX is TH1F *
  2. ROOT program compiled into an executable file

g++ -o filename.exe test.cxx `root-config --cflags --libs`

g ++ -fopenmp -o filename.exe test.cxx `root-config --cflags --libs` // multithreaded caution

g++ -O2  test.cxx `root-config --cflags --libs` -o filename.exe

g++ -O3 -DNDEBUG  test.cxx `root-config --cflags --libs` -o filename.exe

 

  3. The output data is stored as text operation   

FOUT fstream;
fout.open (test.txt ", std :: ios :: OUT | std :: ios :: App); // append write Open

fout.open (test.txt ", std :: ios :: out | std :: ios :: trunc); // truncate the file, and then write

Create a directory

system ( "mkdir ./testdir");// created called testdir in the current directory folder

4. file read (Read)

  ifstream f (file_name.c_str (), std :: ios :: in | std :: ios :: binary); // opened in binary mode

  f1.seekg (-8, ios :: cur); f jump forward pointer file stream 8 bytes

The rounding of the data

//4舍5入函数
int round_double(double number)
{
  return (number > 0.0) ? floor(number + 0.5) : ceil(number - 0.5);
}

 

 

Annex: script text reading Examples

#include <iostream>
#include <fstream>
#include <TH1F.h>
#include <TApplication.h>
using namespace std;
void test()
{
    //read in data, reference to "read_data.C"
    double x[51];
    int y[51];
    int i = 0;
    int j=0;
    ifstream myfile("data.txt");
    if(!myfile) {
        cout << "Unable to open myfile";
        exit(1); // terminate with error  
    }
    else
    {
        char str[51] = {0}; //define and initialize an array
        while (!myfile.eof())
        {
            myfile.getline (str, 51); //读取一行数据
            sscanf(str, "%le, %d", &x[i], &y[i]);
            i++;
        }
    }
        
    TH1F *h = new TH1F("h","delta time;delta time;ampl",51,x[0],x[50]);
    //TH1F *h = new TH1F();
    for( j=0;j<51;j++)         
    {    
        cout<<x[j]<<"\t"<<y[j]<<endl;
        h->Fill(x[j],y[j]);
    }
    //h->Fill(x[0]);h->Fill(x[1]);h->Fill(x[2]);
    /*for (j=0; j<51; j++) 
    {
        for(int k=0;k<y[j];k++)
        h->Fill(x[j],1);
    }*/
    h->Draw("Hist");
}

 

 

Guess you like

Origin www.cnblogs.com/huang-chang/p/11619838.html