I'm having trouble running a couple of simple scripts to make hisograms
from TTree data:
// Draws a weighted histogram. The tree you pass must have a branch named "weight"
// that will be applied.
void weighthist(TTree *t, TString name, int nbins, double xmin, double xmax)
{
TH1F *hist;
double val, weight;
double binwidth = (xmax-xmin)/nbins;
int nentries = (int)t->GetEntries();
int i;
double integral=0.0;
if(hist = (TH1F*)gROOT->FindObject("hist-" + name)) delete hist;
hist = new TH1F("hist-" + name, name + " distribution", nbins, xmin, xmax);
if(!hist) {
cout << "new failed for TH1F!" << endl;
return;
}
t->SetBranchAddress(name, &val);
t->SetBranchAddress("weight", &weight);
for(i=0;i<nentries;i++) {
t->GetEntry(i);
hist->Fill(val,weight);
}
for(i=0;i<nbins;i++) {
integral += binwidth*hist->GetBinContent(i);
}
cout << "Integral is: " << integral << endl;
}
I then run this script a couple of times:
root [1] .L ~/.root/weighthist.C
root [2] TFile f("ppmumu.root", "UPDATE");
root [3] TTree *t = (TTree*)f.Get("ppgammazmumu");
root [4] TTree *st = (TTree*)f.Get("pphmumu");
root [5] weighthist(t, "q", 50, 50, 200);
Integral is: 3.20927e+12
root [6] weighthist(t, "ptl", 50, 0, 200);
Integral is: 4.28335e+12
root [7] weighthist(t, "costheta", 50, -1, 1);
*** Break *** segmentation violation
Root > Function weighthist() busy flag cleared
What does that mean?!? And how do I fix it? Note that the function ran
successfully *twice* before bombing. Turning on debug (gDebug=1) does
not provide any more information than the above. (about the segfault,
anyway)
Lastly, where can I put commands like .L ~/.root/weighthist.C so I can
load a set of often-used scripts at startup? Scripts don't seem to like
dot commands (.L) or I'd put it in login.C or something.
Thanks,
-- Bob
Bob McElrath (rsmcelrath@students.wisc.edu)
Univ. of Wisconsin at Madison, Department of Physics
This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:51:00 MET