Reading Trees

Hi ,

I have to read a Tree containing TH2Fs. I wrote the following code but it does not seem to work. I attach the root file I am trying to read . There are also two data files used by the code. The code is below. Thanks a lot,

Sabrina

;//macro reads slewing curve data files
//pedestal is added to curve (includes pmt distance from laser ball and fiber length difference)
//The value of the curve at TOT=400 is pulled out and made the pedestal
//so the curve is effectively shifted to pass through 0 at TOT=400
#include <iostream.h>
#include <iomanip.h>
#include “TFile.h”
#include “TH1.h”
#include “TH2.h”
#include “TProfile.h”
#include “TRandom.h”
#include “TTree.h”

#define N_PMT 1000
#define FOREVER 1

typedef struct{
float hp0[N_PMT];
float hp1[N_PMT];
float hp2[N_PMT];
float hp3[N_PMT];
float hp4[N_PMT];
float hp5[N_PMT];
float lp0[N_PMT];
float lp1[N_PMT];
float lp2[N_PMT];
float lp3[N_PMT];
float lp4[N_PMT];
float lp5[N_PMT];
float hped[N_PMT];
float lped[N_PMT];
} SLEW_PARAM;

ReadSlewParams3()
{
int i;
char name[20];
Float_t minTOT,maxTOT,rms;
Int_t nTOT,nEntries;
Int_t i,nCycle;
int pmtWant;
int oldpmtWant=0;

static int firstread = 1;
static SLEW_PARAM slewParams;
const int MIN_PMT=1;

char astring[10];
int first = 1; // first time through loop
int pmt; //electronics channel #
int ball; //laser ball #
float ped; //place holder
float param[6]; //array to hold slewing curve fit parameters

if(firstread){
// cout<<“I’m here”<<endl;
//Initialize param arrays
for(i=0;i<N_PMT;i++){
slewParams.hp0[pmt] = 0;
slewParams.hp1[pmt] = 0;
slewParams.hp2[pmt] = 0;
slewParams.hp3[pmt] = 0;
slewParams.hp4[pmt] = 0;
slewParams.hp5[pmt] = 0;
slewParams.lp0[pmt] = 0;
slewParams.lp1[pmt] = 0;
slewParams.lp2[pmt] = 0;
slewParams.lp3[pmt] = 0;
slewParams.lp4[pmt] = 0;
slewParams.lp5[pmt] = 0;
slewParams.hped[pmt] = 0;
slewParams.lped[pmt] = 0;
}
//Grab slewing parameters from file and put in parameter structure
//Read in hi slewing fit parameters
ifstream infile_hi(“Hi_Slew_Final_Shifted.txt”);
/*
ifstream infile_hi("/disk01/software/calib/calib/300/Hi_Slew.dat");
char junk[512];
while(infile_hi>>junk){
//Skip header junk
infile_hi>>junk;
if(strcmp(junk,“300”)==0) break;
// if(strcmp(junk,“Version”)==0) break;
}
/
while(infile_hi>>pmt>>ball>>ped>>param[0]>>param[1]>>param[2]>>param[3]>>param[4]>>param[5]){
// cout<<pmt<<" "<<ped<<endl;
slewParams.hp0[pmt]=param[0];
slewParams.hp1[pmt]=param[1];
slewParams.hp2[pmt]=param[2];
slewParams.hp3[pmt]=param[3];
slewParams.hp4[pmt]=param[4];
slewParams.hp5[pmt]=param[5];
slewParams.hped[pmt]=ped;
}
ifstream infile_lo(“Lo_Slew_Final_Shifted.txt”);
/

ifstream infile_lo("/disk01/software/calib/calib/300/Lo_Slew.dat");
while(infile_lo>>junk){
//Skip header junk
infile_lo>>junk;
if(strcmp(junk,“300”)==0) break;
}
*/
while(infile_lo>>pmt>>ball>>ped>>param[0]>>param[1]>>param[2]>>param[3]>>param[4]>>param[5]){
//Read in Lo Slew fit parameters
cout<<pmt<<" "<<ped<<endl;
slewParams.lp0[pmt]=param[0];
slewParams.lp1[pmt]=param[1];
slewParams.lp2[pmt]=param[2];
slewParams.lp3[pmt]=param[3];
slewParams.lp4[pmt]=param[4];
slewParams.lp5[pmt]=param[5];
slewParams.lped[pmt]=ped;
}

TCanvas* c1 = new TCanvas("c1","c1",800,800);


TH2F *pmt290_lo = new TH2F("pmt290_lo","Lo",40,0,1000,40,2000,2500);
TH2F *pmt290_hi = new TH2F("pmt290_hi","Hi",40,0,1000,40,2000,2500);

pmt290_lo->SetLineColor(kRed);
pmt290_lo->SetLineWidth(2);
pmt290_hi->SetLineWidth(2);


TFile *file1 = new TFile("Slew_ball_27.root","READ");

TTree *tree1 = (TTree*)file1->Get("mtree");

tree1->SetBranchAddress("pmt290_lo",&pmt290_lo);
tree1->SetBranchAddress("pmt290_hi",&pmt290_hi);

for (int i=0; i<tree1->GetEntries(); i++) {
  tree1->GetEntry(i);
  pmt290_lo->Fill(pmt290_lo);

// }
for (int i=0; iGetEntries(); i++) {
tree1->GetEntry(i);
pmt290_hi->Fill(pmt290_hi);
}

ps->NewPage();
c1->cd();
gStyle->SetOptStat(kFALSE);
pmt290_lo->Draw();
pmt290_hi->Draw(“same”);
c1->Update();
}

while(FOREVER){
// cout<<“I’m at FOREVER”<<endl;

//query for pmt to draw slewing curves
cout<<"Enter a PMT number: ";
scanf("%s",astring);
pmtWant = atoi(astring);

cout<<"Enter a ball number: ";
scanf("%b",astring);
ballWant = atoi(astring);

TF1* funcLine = new TF1("funcLine","[0]*x+[1]",0,1000);
TF1* funcTwoLineLo = new TF1("funcTwoLineLo","([0]*x + [1])*exp(-(x-[5])/[2])/(1+exp(-(x-[5])/[2])) + ([3]*x + [4])*exp((x-[5])/[2])/(1+exp((x-[5])/[2]))",0,1000);
TF1* funcTwoLineHi = new TF1("funcTwoLineHi","([0]*x + [1])*exp(-(x-[5])/[2])/(1+exp(-(x-[5])/[2])) + ([3]*x + [4])*exp((x-[5])/[2])/(1+exp((x-[5])/[2]))",0,1000);
funcLine->SetMaximum(50);
funcLine->SetMinimum(-75);
funcTwoLineLo->SetMaximum(50);
funcTwoLineLo->SetMinimum(-75);
funcTwoLineHi->SetLineColor(2);

cout<<pmtWant<<"   "<<oldpmtWant<<endl;
if(0==pmtWant){
  pmtWant = oldpmtWant+1;
  cout<<"Plotting PMT "<<pmtWant<<endl;
}
if(pmtWant<1 || pmtWant>=N_PMT){
  cerr<<"PMT number exceeds bounds: "<<pmtWant<<endl;
  return;
}
c1->cd();
if(0==slewParams.lp0[pmtWant]){
  cout<<"No Lo Slewing calibration for PMT "<<pmtWant<<endl;
}
else if(0==slewParams.lp2[pmtWant]){
  funcLine->SetParameters(slewParams.lp0[pmtWant],slewParams.lp1[pmtWant]);
  funcLine->Draw();
}
else{
  funcTwoLineLo->SetParameters(slewParams.lp0[pmtWant],slewParams.lp1[pmtWant],slewParams.lp2[pmtWant],slewParams.lp3[pmtWant],slewParams.lp4[pmtWant],slewParams.lp5[pmtWant]);
  funcTwoLineLo->Draw();
}
c1->Update();
if(0==slewParams.hp0[pmtWant]){
  cout<<"No Hi Slewing calibration for PMT "<<pmtWant<<endl;
}
else{
  funcTwoLineHi->SetParameters(slewParams.hp0[pmtWant],slewParams.hp1[pmtWant],slewParams.hp2[pmtWant],slewParams.hp3[pmtWant],slewParams.hp4[pmtWant],slewParams.hp5[pmtWant]);
  funcTwoLineHi->Draw("same");
}
c1->Update();
oldpmtWant = pmtWant;
cout<<"q for quit or another pmt and ball # to continue:"<<endl;
scanf("%s",astring);
if(strncmp(astring,"q",1)==0)  return;
else  {pmtWant = atoi(astring); 
  ballWant = atoi(astring);}

// TCanvas* myc=new TCanvas(“myc”);
// myc->Divide(1,2);
// myc->cd(1);
// h2_ped->Draw(“COLZ”);
// myc->cd(2);
// h2_pedlo->Draw(“COLZ”);
// myc->Update();
}};
}
slew.tar.gz (1.93 MB)
Lo_Slew_Final_Shifted.txt (62.8 KB)
Hi_Slew_Final_Shifted.txt (62.9 KB)

Hi,

I am a little confused in what you have and what you trying to do:TFile *file1 = new TFile("Slew_ball_27.root","READ"); TTree *tree1 = (TTree*)file1->Get("mtree");

The file you provided does NOT contain any TTree object
(much less one named mtree). After the excution of
this line tree1 would be equal to 0. (lead the rest of the
code to core dump.

Assuming that there is such a tree, you still have:TH2F *pmt290_lo = new TH2F("pmt290_lo","Lo",40,0,1000,40,2000,2500); TH2F *pmt290_hi = new TH2F("pmt290_hi","Hi",40,0,1000,40,2000,2500); .... tree1->SetBranchAddress("pmt290_lo",&pmt290_lo); tree1->SetBranchAddress("pmt290_hi",&pmt290_hi);

This would work if and only if the branch pmt290_lo and pmt290_hi where really containing some TH2F objects (I doubt they they do, more likely
you stored a flloat or a double in this branches).
Worse you have: tree1->GetEntry(i); pmt290_lo->Fill(pmt290_lo);
Which says, (given the SetAddress you have), please add to the histogram I just read from file, the value of its address … but then this is ‘lost’ since you read again on the next loop.

I assume that you meant:double pmt290_lo_val; double pmt298_hi_val; tree1->SetBranchAddress("pmt290_lo",&pmt290_lo_val); tree1->SetBranchAddress("pmt290_hi",&pmt290_hi_val); for (int i=0; i<tree1->GetEntries(); i++) { tree1->GetEntry(i); pmt290_lo->Fill(pmt290_lo_val); pmt290_hi->Fill(pmt290_hi_val); }

Cheers,
Philippe

PS. Note that you did not mention how this is not working

Hi Philippe,

I know that I should understand at least a very basic ROOT programming. Unfortunately I have no time at the moment. Sorry for bothering you with stupid questions. I must say that was you wrote in reply to my help request helped me a lot.

Sabrina