Calculate, count and plot from a leaf

Hi there,

I have yet another Task to do in Root I cannot get around.

I measured events with a timestamp in unix seconds.
What I want to do now is to calculate the frequency of the events and plot the frequency over time.
Until now I only did hostograms with root.

Here is the quick and dirty code I mangled up with my Analyze macro.

[code]#define Analyze_cxx
#include “Analyze.h”
#include <TH2.h>
#include <TStyle.h>
#include <TCanvas.h>

void Analyze::Loop()
{
// In a ROOT session, you can do:
// Root > .L Analyze.C
// Root > Analyze t
// Root > t.GetEntry(12); // Fill t data members with entry number 12
// Root > t.Show(); // Show values of entry 12
// Root > t.Show(16); // Read and show values of entry 16
// Root > t.Loop(); // Loop on all entries
//

// This is the loop skeleton where:
// jentry is the global entry number in the chain
// ientry is the entry number in the current Tree
// Note that the argument to GetEntry must be:
// jentry for TChain::GetEntry
// ientry for TTree::GetEntry and TBranch::GetEntry
//
// To read only selected branches, Insert statements like:
// METHOD1:
// fChain->SetBranchStatus("*",0); // disable all branches
// fChain->SetBranchStatus(“branchname”,1); // activate branchname
// METHOD2: replace line
// fChain->GetEntry(jentry); //read all branches
//by b_branchname->GetEntry(ientry); //read only this branch
if (fChain == 0) return;

//The Setup Code goes here

gStyle->SetOptStat("e");

//Frequency over time

TCanvas *c_frequency = new TCanvas(“c_frequency”, “c_frequency”,284,467,700,502);
c_frequency->Range(-3.764811,-66.46194,3.732495,573.7428);
c_frequency->SetBorderSize(2);
c_frequency->SetFrameFillColor(0);

double xmin = fChain->GetMinimum("unix_seconds");
double xmax = fChain->GetMaximum("unix_seconds");
TGraph* freq = new TH1D("frequency","Zeit",,xmin,xmax);
freq->GetXaxis()->SetTitle("Time");
freq->GetXaxis()->SetTimeDisplay(1); // X axis is a time axis
freq->GetXaxis()->SetTimeFormat("%H\:%M\:%S");
freq->GetYaxis()->SetTitle("frequency");

Long64_t nentries = fChain->GetEntriesFast();

Long64_t nbytes = 0, nb = 0;
Double_t intervall;
double old_unix_seconds = 0;
for (Long64_t jentry=0; jentry<nentries;jentry++) {
Long64_t ientry = LoadTree(jentry);
if (ientry < 0) break;
nb = fChain->GetEntry(jentry); nbytes += nb;

   //The Loop Code goes here
   
   freq->Fill(calculated_frequency);
   
   	   
}


//The Wrap-Up Code goes here
c_frequency->cd();
freq->Draw("");

.....

}
[/code]

I don’t worry about the TGraph Class or something like that. I cannot get my mind around how to calculate the frequency and have that as a variable which I can plot against the time.
It should recognize when the variable unix_seconds is bigger by the number of 60, count how many entries in this intervall were and then divide this number by 60 and give it to the variable calculated_frequency.

Maybe you can help. I hope that I managed to make clear, what exactly I want :slight_smile:

Thank you

Hi,

One question is over which sub-period do you want to calculated the frequency you are plotting. One possibility is to plot the time interval between each event:[code] time_t currentTIme,prevTime;
fChain->SetBranchAddress(“unix_seconds”,&currentTime);
for (Long64_t jentry=0; jentry<nentries;jentry++) {
Long64_t ientry = LoadTree(jentry);
if (ientry < 0) break;
nb = fChain->GetEntry(jentry); nbytes += nb;
if (jentry == 0) {
prevTime = currentTime;
continue;
}
//The Loop Code goes here

  freq->Fill(currentTime - prevTime);
  prevTime = currentTime;

}
[/code]

Cheers,
Philippe.