%jsroot on
TCanvas *c1=new TCanvas("c1","c1");//创建画布
TH1D *tdiff=new TH1D("tdiff","td-tu",140,-20,50); //创建时间差histogram
//声明branch变量
Double_t tu, td, x;
//Double_t ctof,tof,x;
//int pid;
//打开root文件
TFile *ipf=new TFile("./tree.root");//打开ROOT文件
//得到tree的指针
TTree *tree=(TTree*)ipf->Get("tree");
//将tree中Branch与变量地址联系
tree->SetBranchAddress("tu",&tu);
tree->SetBranchAddress("td",&td);
//tree->SetBranchAddress("ctof",&ctof);
//tree->SetBranchAddress("tof",&tof);
tree->SetBranchAddress("x",&x);
//tree->SetBranchAddress("pid",&pid);
Long64_t nentries=tree->GetEntries();//得到事件总数
for(Long64_t jentry=0; jentry<nentries; jentry++) {//对每个事件进行遍历
tree->GetEntry(jentry);
tdiff->Fill(td-tu); // if(ng==1) tx->Fill(tu-td), 只写入满足给定条件的事件
}
tdiff->Draw();
c1->Draw();
gStyle->SetPalette(1);
tree->Draw("td-tu:x","","colz");//观察两个参量的关联
//c1->SetLogz();
c1->Draw();
//%jsroot on
TH1D *dtd=new TH1D("dtd","dt/dx",141,-20.25,50.25);//创建tdiff的微分histogram,dtd
for(int i=1;i<tdiff->GetNbinsX();i++) {//tdiff->GetNbinsX(),得到X轴bin总数,由1开始哦
Double_t df=tdiff->GetBinContent(i+1)-tdiff->GetBinContent(i);//tdiff->GetBinContent(i+1)得到某个bin对应的constant;
dtd->Fill(tdiff->GetBinLowEdge(i+1),df);//填图,dtd可看成tdiff的斜率图
}
dtd->Sumw2(0);//不显示传递误差
dtd->Draw();
dtd->Fit("gaus","","",-14,-8);//高斯拟合histogram
c1->Draw();
ps:由于模拟数据的随机性,本人模拟产生的tdiff图的微分边界并非高斯形状,高斯拟合值偏小;
TF1 *f1 = new TF1("f1","[0]*TMath::Exp(-0.5*((x-[1])/[2])^2)",39,43);//定义函数的方法 TF1
//设置初始参数
//进行参数拟合时,设置合理的初始参数至关重要!
f1->SetParameter(0,-350);//constants
f1->SetParameter(1,41.5);//mean
f1->SetParameter(2,0.5);//sigma
dtd->Fit("f1","R");
dtd->Draw();
c1->SetLogy(0);
c1->Draw();
TH1D *htx=new TH1D("htx","htx",500,-120,120);
TH2D *hdx=new TH2D("hdx","htx-hx:hx",100,-20,20,500,-120,120);
for(Long64_t jentry=0; jentry<nentries; jentry++) {//对事件进行遍历
tree->GetEntry(jentry);
Double_t tx=3.742*(td-tu-14.915);
htx->Fill(tx);//刻度后的tx
hdx->Fill(tx-x,x);//二维图,check刻度效果
}
htx->Draw();
c1->Draw();
hdx->Draw("colz");//为一条竖线,没有关联
c1->Draw();
TH1D *hdx1=hdx->ProjectionX("projx of hdx");
hdx1->Draw();
hdx1->Fit("gaus");//如果中心值 mean x偏离0, 说明确定td-tu的边界有问题
c1->Draw();
//ipt->Close();//
ipf->Close();//关闭root文件
!jupyter nbconvert homework1.1-1 --to html