Skip to content

Instantly share code, notes, and snippets.

@dmb2
Created March 17, 2013 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmb2/5183895 to your computer and use it in GitHub Desktop.
Save dmb2/5183895 to your computer and use it in GitHub Desktop.
Two macros that open a root file and save a histogram. The other one opens a root file, retrieves a histogram, and fits a function to it.
{
float binWeights[12]={180,135,120,105,76,68,60,41,30,35,26,18};
TH1F kDecayHist("kDecayHist", "Decay length of K^{0} decays;decay length(cm);events",12,0,60);
for(int i=0; i < kDecayHist.GetNbinsX(); ++i){
kDecayHist.SetBinContent(i+1,binWeights[i]);
}
//recreate overwrites any old files and puts our new stuff inside without warning!
TFile f("kaondecay.root","RECREATE");
kDecayHist.Write();
f.Write();
f.Close();
}
Double_t expDecay(Double_t* v,Double_t *par) {
return par[0]*TMath::Exp(-v[0]/par[1]);
}
void root_tutorial2(){
TFile f("kaondecay.root","READ");
TH1F* hist=f.Get("kDecayHist");
hist->Sumw2();
TF1 fitfn("fitfn",expDecay,0,60,2);
fitfn.SetParameters(200,20);
fitfn.SetParNames("Norm","Decay_length");
hist->Fit("fitfn");
hist->Draw();
fitfn.Draw("same");
//L=beta*gamma*c*tau
//tau=L/(beta*gamma*c)
Double_t bgc=(5/0.497)*299792458;
Double_t L=fitfn.GetParameter(1)/100;
Double_t err=fitfn.GetParError(1);
Double_t tau=L/bgc;
cout<<"measured K0 lifetime: "<<tau<<" +/-"<<err/bgc<<endl;
cout<<"PDG K_L: (5.116+/-0.021)e-8 s"<<endl;
cout<<"PDG K_S: (8.9564+/-0.0033)e-11 s"<<endl;
}
@dmb2
Copy link
Author

dmb2 commented Mar 18, 2013

find me at http://goo.gl/5J63h

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment