#include <TFile.h>
#include <TTree.h>
#include <TString.h>
#include <TRandom3.h>
void dithering()
{
//打开s4.root文件,继承tree
TFile * ipf = new TFile("../s4.root");
if (ipf->IsZombie())
{
cout << "Error opening file" << endl;
exit(-1);
}
ipf->cd();
TTree * ipt = (TTree *)ipf->Get("tree");
//ipf中Branch以及参量
Int_t pe[48],re[48];
for(Int_t i = 0;i<48;i++)
{
pe[i] = -1.0;
re[i] = -1.0;
}
ipt->SetBranchAddress("pe",&pe);
ipt->SetBranchAddress("re",&re);
//新root文件与tree
TFile * opf = new TFile("../s4dith.root","recreate");
TTree * opt = new TTree("tree","dithering");
//新root文件变量,与新Branch
Double_t pedith[48],redith[48];
opt->Branch("pedith",&pedith,"pedith[48]/D");
opt->Branch("redith",&redith,"redith[48]/D");
//随机数
TRandom3 * gr = new TRandom3(0);
//遍历事件,加入(0,1)随计数
Long64_t nentries = ipt->GetEntries();
for(Long64_t jentry = 0; jentry<nentries;jentry++)
{
ipt->GetEntry(jentry);
for(Int_t i = 0;i<48;i++)
{
pedith[i] = pe[i] + gr->Uniform(0,1);
redith[i] = re[i] + gr->Uniform(0,1);
}
opt->Fill();
if(jentry%100000==0) cout<<"process "<<jentry<<" of "<<nentries<<endl;
}
opt->Write();
opf->Close();
ipf->Close();
}
%jsroot on
TFile *ipf = new TFile("s4dith.root","read");
TTree * tree = (TTree*)ipf->Get("tree");
TCanvas *c1 = new TCanvas();
tree->Print();
tree->Scan("pedith:redith","pedith>0&& redith>0","",1000,1);//第4参数-事件数,第5参数-起始事件编号
//jupyter上的Scan必须指定第4个参数(事件数)
tree->Draw("pedith[1]*0.007179-1.565575>>(1000,-1,10)");
c1->Draw();
ipf->Close();
ipf = new TFile("s4.root","read");
tree = (TTree*)ipf->Get("tree");
tree->Draw("pe[1]*0.007179-1.565575>>(1000,-1,10)");
c1->Draw();
ipf->Close();
对比发现连续化之后加入刻度系数没有了锯齿,谱更加圆滑!
!jupyter nbconvert homework3.1_1_dithering.ipynb --to html