# Defining,Filling &Fitting histograms from tree::trouble

Hi Lorenzo,
This is a new problem I am running into. Since you have my data file so you can help me a little with this.I trying to Define, Fill and then FIt the histogram for a particular event number from a tree. In function
::Loop(Int_t eventnum) in my code I have declared the fit function. The for loop in my code loops over all entries of the tree. Each event in the tree has 100 entries. So if I fill a histogram for a particular event number then it will have 100 entries. The event number is denoted as eventn_iev in the code. The problem is when I am trying to run the code for a paricular event number e.g. 42, then I am getting one histogram for each entry. So in total I am getting 100 histograms saved for one event.

So how can I create one histogram and then fill it with all 100 entries and then fit the histogram with the defined TF! function, and finally save the histogram in one single event loop.Please help. I have attached the code here.
timewalk2bset10.h (5.99 KB)
timewalk2bset10.C (5.27 KB)

Hi,

If I have understood well your tree, what you call “event” contains blocks of 100 tree entries, and the structure is alway the same for all events.

So, to read a single event, just loop on the corresponding 100 entries in the tree, and fill the histogram. An example is in the following code:

``````   // get entries in the tree for event number "eventnum"

// each event is composed of 100 tree entries
const int nentries_event = 100;
Long64_t first = (eventnum-1)*nentries_event;
Long64_t last  = first+ nentries_event;

h2b_set10_1=new TH1F("set10_1","histogram_set10_1",101,-0.5,100.5);

// loop on the entries for the corresponding event
for (Long64_t jentry=first; jentry<last;jentry++)
{

if (ientry < 0) break;
nb = fChain->GetEntry(jentry);   nbytes += nb;

// to be sure we have the right event
if (eventn_iev != eventnum) { Error("Loop","Read wrong event - %d instead of %d",eventn_iev,eventnum); return;}