%jsroot on
//创建画布
TCanvas *c1=new TCanvas("c1","c1");
//创建时间差histogram
//root中,log():e为底;log10():10为底;
TH1D *qratio=new TH1D("qratio","log(qu/qd)",160,-0.8,0.8);
//声明branch变量
Double_t qu, qd, x;
Double_t ln_R = 0;
//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("qu",&qu);
tree->SetBranchAddress("qd",&qd);
//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);
//root中,log():e为底;log10():10为底;
qratio->Fill(log(qu/qd)); // if(ng==1) tx->Fill(tu-td), 只写入满足给定条件的事件
}
qratio->Draw();
c1->Draw();
tree->Draw("log(qu/qd):x","","colz");
c1->Draw();
//%jsroot on
TH1D *dqr=new TH1D("dqr","dq/dx",30,-0.8,0.8);//创建qratio的微分histogram,dqr
for(int i=1;i<qratio->GetNbinsX();i++) {//qratio>GetNbinsX(),得到X轴bin总数,由1开始哦
Double_t dq=qratio->GetBinContent(i+1)-qratio->GetBinContent(i);//qratio>GetBinContent(i+1)得到某个bin对应的constant;
dqr->Fill(qratio->GetBinLowEdge(i+1),dq);//填图,dtd可看成tdiff的斜率图
}
dqr->Sumw2(0);//不显示传递误差
dqr->Draw();
dqr->Fit("gaus","","",-0.7,-0.35);//高斯拟合histogram
c1->Draw();
TF1 *f1 = new TF1("f1","[0]*TMath::Exp(-0.5*((x-[1])/[2])^2)",0.35,0.65);//定义函数的方法 TF1
//设置初始参数
//进行参数拟合时,设置合理的初始参数至关重要!
f1->SetParameter(0,-200);//constants
f1->SetParameter(1,0.5);//mean
f1->SetParameter(2,0.05);//sigma
dqr->Fit("f1","R");
dqr->Draw();
c1->SetLogy(0);
c1->Draw();
TH1D *hqx=new TH1D("hqx","hqx",300,-150,150);
TH2D *hdqx=new TH2D("hdqx","hdqx-hx:hx",100,-50,50,150,-150,150);
for(Long64_t jentry=0; jentry<nentries; jentry++) {//对事件进行遍历
tree->GetEntry(jentry);
Double_t qx=190.4*(log(qu/qd)+0.003);
hqx->Fill(qx);//刻度后的tx
hdqx->Fill(qx-x,x);//二维图,check刻度效果
}
hqx->Draw();
c1->Draw();
hdqx->Draw("colz");//为一条竖线,没有关联
c1->Draw();
TH1D *hdqx1=hdqx->ProjectionX("projx of hdx");
hdqx1->Draw();
hdqx1->Fit("gaus");//如果中心值 mean x偏离0, 说明确定td-tu的边界有问题
c1->Draw();
ipf->Close();//关闭root文件
!jupyter nbconvert homework1.1-2.ipynb --to html