CERN ROOT程序使用心得1

CERN ROOT程序是使用技巧记录:

  1. Root 中THF的 bin从1开始记(不是从0)  例如: for (int m = 1; m<= xbin; m++) counts=HX->GetBinContent(m); HX为TH1F*
  2. 编译ROOT程序为可执行文件

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

g++ -fopenmp -o filename.exe  test.cxx `root-config --cflags --libs` //多线程慎用

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

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

  3. 数据保存输出为文本操作   

fstream fout;
fout.open(test.txt",std::ios::out|std::ios::app);//追加方式打开写入

fout.open(test.txt",std::ios::out|std::ios::trunc);//截断文件,然后写入

创建目录

system("mkdir  ./testdir");//在当前目录下创建名为testdir的文件夹

4.文件读取(read)

  ifstream f(file_name.c_str(),std::ios::in|std::ios::binary);//以二进制方式打开

  f1.seekg(-8,ios::cur);文件流指针f往前跳8个字节

5.对数据四舍五入

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

 

 

附:文本读取脚本实例

#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");
}

 

猜你喜欢

转载自www.cnblogs.com/huang-chang/p/11619838.html