Reading multiple *.root files in TChain

Hello!
I have a problem reading in multiple *.root files in a TChain object. It reads just the first *.root file?!

  1. I create a TChain object:

TChain *elasticchain=new TChain("pdelastic"); elasticchain->Add("/media/9C2C3AB22C3A8774/ntupleJuly2016_with40sub_1.root"); elasticchain->Add("/media/9C2C3AB22C3A8774/ntupleJuly2016_with40sub_2.root"); elasticchain->Add("/media/9C2C3AB22C3A8774/ntupleJuly2016_with40sub_1.root"); elasticchain->Add("/media/9C2C3AB22C3A8774/Ntuples/ntupleJuly2016_with40sub_2.root");
2) Then I loop the events

for (Long64_t jentry=Firstentry; jentry<Nentries; jentry++) { elasticchain->GetTree()->GetEntry(jentry); } elasticchain->ResetBranchAddresses();
If I use only one *.root file, it runs fine, loading all branches properly and processing the events.
If I use multiple *.root files, it takes long time but it fills histograms just using the first *.root file.
Any help is very appreciated! Thanks!

elasticchain->GetEntry(jentry);

thanks, But this doesn’t work.

[quote]thanks, But this doesn’t work.[/quote]Very strange, Adding file the way you did and calling GetEntry directly on the TChain object (and/or using LoadTree and GetEntry on some of the branches) is the typical way to access chain and works in all the cases we know. So there is something special about the actual code/environment that are you are running and which you have not described yet.

The best way forward is to provide a complete (small) running reproducer of the problem … or at least a complete code set.

Cheers,
Philippe.

[quote=“pcanal”][quote]thanks, But this doesn’t work.[/quote]Very strange, Adding file the way you did and calling GetEntry directly on the TChain object (and/or using LoadTree and GetEntry on some of the branches) is the typical way to access chain and works in all the cases we know. So there is something special about the actual code/environment that are you are running and which you have not described yet.

The best way forward is to provide a complete (small) running reproducer of the problem … or at least a complete code set.

Cheers,
Philippe.[/quote]

Thank you,
Here is the my code:

[code]#include
#include
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include
#include

#define printflag false

#define Firstentry ( 30197259 )
#define Lastentry ( 30197259+1e6 ) //3e7 breakup Lmds // 1.89e7 elastic Lmds// 3.5e8

#define Nentries ( 1e6 )

#define ringNo ( 4 )
#define struNo ( 1 )

#define dphi (20)
#define Bdth (4)

#define WPcalib “…/…/database/WallCalibData/FinalScalingFactors138.dat” /* Wall proton calibration file /
#define WDcalib “…/…/database/WallCalibData/EWDcalibrationBayat.dat” /
Wall Deuteron calibration file */

#define BPCalibA “…/…/database/BallCalibData/BPEnergyCalibBayat_A_.dat” /* Ball Proton calibration file */
#define BPCalibB “…/…/database/BallCalibData/BPEnergyCalibBayat_B_.dat”
#define BPCalibC “…/…/database/BallCalibData/BPEnergyCalibBayat_C_.dat”

#define psfile “…/…/prints/BallCalibPrint/E_calib_wall_ball_r4s1_pol2/E_calib_wall_ball_r4s1_Breakuptest_102_elasticLmds_EPadded.ps”
#define rootfile “…/…/prints/BallCalibPrint/E_calib_wall_ball_r4s1_pol2/E_calib_wall_ball_r4s1_Breakuptest_102_elasticLmds_EPadded.root”

double Pcalib[11][2]; /* wall calibartion values for proton /
double Dcalib[11][3]; /
wall calibartion values for deuteron /
double PcalibA[150][3]; /
ball calibartion values for proton */
double PcalibB[150][3];
double PcalibC[150][3];

char name[15][250];

/************************************************************ main *********************************************************/

void E_calib_wall_ball2 ()
{
int i,j,k,l,m,n;
TStopwatch timer;
timer.Start();
myrootFile=new TFile(rootfile,“RECREATE”);
char rNosNo[250];
sprintf(rNosNo,“r%ds%d”,ringNo,struNo);

TPaveText pt[21];
/
*************************** Define Rings & structures ****************************/

int rsSize, Nx, Ny;
float Wminth, Wmaxth,rswallth;

if ( strcmp(rNosNo,“r4s1”) == 0)
{
rsSize=5;
int rsArray[5]={84,90,96,102,108};
rswallth=22;
Nx=2;
Ny=3;
}

/* wall proton calibration file*/
/************************************************************************************************************************************/
ifstream WprotnCalib;
WprotnCalib.open(WPcalib);
if(WprotnCalib) fprintf(stdout,“File %s is open.\n”,WPcalib);
else { fprintf(stdout,“Error in opening the file %s.\n”,WPcalib);}

// fprintf(stdout,"--------\nProton calibration values:\n");
for ( i=1; i<11; i++)
{
WprotnCalib >> Pcalib[i][0] >> Pcalib[i][1];
if(printflag) cout << Pcalib[i][0] << " " << Pcalib[i][1] << “\n” ;
}
WprotnCalib.close();
/elastic wall deuteron calibration file/
/************************************************************************************************************************************/
ifstream WdeuteronCalib;
WdeuteronCalib.open(WDcalib);
if(WdeuteronCalib) fprintf(stdout,“File %s is open.\n”,WDcalib);
else { fprintf(stdout,“Error in opening the file %s.\n”,WDcalib);}

char cha[250];
WdeuteronCalib >> cha;
// fprintf(stdout,"--------\nDeuteron calibration values:\n");
for ( i=1; i<11; i++)
{
WdeuteronCalib >> k >> Dcalib[i][0] >> Dcalib[i][1] >> Dcalib[i][2];
if(printflag) cout << k << " " << Dcalib[i][0] << " " << Dcalib[i][1] << " " << Dcalib[i][2] << “\n” ;
}
WdeuteronCalib.close();

/*elastic Ball proton calibration file */
/********************************************************************************************************************************/
ifstream BprotonCalibA;
BprotonCalibA.open(BPCalibA);
if(BprotonCalibA) fprintf(stdout,“File %s is open.\n”,BPCalibA);
else { fprintf(stdout,“Error in opening the file %s.\n”,BPCalibA);}

char chA[250];
BprotonCalibA >> chA >> chA >> chA >> chA;
for ( j=0; j<rsSize; j++)
{

  BprotonCalibA >> l >> PcalibA[rsArray[j]][0] >>  PcalibA[rsArray[j]][1] >>  PcalibA[rsArray[j]][2];
  if(printflag) cout << l << " " << PcalibA[rsArray[j]][0] << " " << PcalibA[rsArray[j]][1] << " " << PcalibA[rsArray[j]][2] <<"\n" ;
 }

BprotonCalibA.close();

ifstream BprotonCalibB;
BprotonCalibB.open(BPCalibB);
if(BprotonCalibB) fprintf(stdout,“File %s is open.\n”,BPCalibB);
else { fprintf(stdout,“Error in opening the file %s.\n”,BPCalibB);}

char chB[250];
BprotonCalibB >> chB >> chB >> chB >> chB;
for ( j=0; j<rsSize; j++)
{

  BprotonCalibB >> l >> PcalibB[rsArray[j]][0] >>  PcalibB[rsArray[j]][1] >>  PcalibB[rsArray[j]][2];
  if(printflag) cout << l << " " << PcalibB[rsArray[j]][0] << " " << PcalibB[rsArray[j]][1] << " " << PcalibB[rsArray[j]][2] << "\n" ;
 }

BprotonCalibB.close();

ifstream BprotonCalibC;
BprotonCalibC.open(BPCalibC);
if(BprotonCalibC) fprintf(stdout,“File %s is open.\n”,BPCalibC);
else { fprintf(stdout,“Error in opening the file %s.”,BPCalibC);}

char chC[250];
BprotonCalibC >> chC >> chC >> chC >> chC;
for ( j=0; j<rsSize; j++)
{

  BprotonCalibC >> l >> PcalibC[rsArray[j]][0] >>  PcalibC[rsArray[j]][1] >>  PcalibC[rsArray[j]][2] ;
  if(printflag) cout << l << " " << PcalibC[rsArray[j]][0] << " " << PcalibC[rsArray[j]][1] << " " << PcalibC[rsArray[j]][2] << "\n" ;
 }

BprotonCalibC.close();

/************************************************************************************************************************************/
TPostScript *ps = new TPostScript(psfile,112);
ps->Range(25,19); //set x,y of printed page

gROOT->Macro("…/openntuple.cxx");
gROOT->Macro("…/ElasticKinemaGraphs.cxx");

gROOT->Macro("…/E1_E2_E3_1.cxx");
gROOT->Macro("…/E1_E2_E3_2.cxx");
gROOT->Macro("…/E1_E2_E3_3.cxx");
gROOT->Macro("…/E1_E2_E3_4.cxx");

TCanvas *c2 = new TCanvas(“c2”,“canvas 2”);
c2->SetFillColor(10);
c2->Size(25,19);

/**************************** Define cuts ***************************************/

/************************************** cut for MWPCtheta & EFW **************************************/
TCutG *cutEFW = new TCutG(“cutEFW”,14);
cutEFW->SetPoint(0,13.45412,120.8729);
cutEFW->SetPoint(1,16.19121,117.8876);
cutEFW->SetPoint(2,21.08712,110.9218);
cutEFW->SetPoint(3,28.18042,99.64385);
cutEFW->SetPoint(4,33.34618,88.03422);
cutEFW->SetPoint(5,33.07633,79.40992);
cutEFW->SetPoint(6,29.60678,83.05866);
cutEFW->SetPoint(7,25.67463,89.02933);
cutEFW->SetPoint(8,21.39553,95.3317);
cutEFW->SetPoint(9,17.23207,101.6341);
cutEFW->SetPoint(10,12.64456,107.6047);
cutEFW->SetPoint(11,13.37702,120.8729);
cutEFW->SetPoint(12,13.45412,120.8729);
cutEFW->SetPoint(13,13.45412,120.8729);

/**************************** Define Histograms ***************************************/
TH2D *ballEwallE=new TH2D(“ballEwallE”,";E_{wall} (MeV); E_{ball} (MeV)",75,0,150,30,0,60); // 80,90,130,90,5,50
CenterTitle(ballEwallE);
TH2D *ballEwallE_A=new TH2D(“ballEwallE_A”,";E_{wall} (MeV); E_{ball} (MeV)",75,0,150,30,0,60); // 80,90,130,90,5,50
CenterTitle(ballEwallE_A);
TH2D *ballEwallE_B=new TH2D(“ballEwallE_B”,";E_{wall} (MeV); E_{ball} (MeV)",75,0,150,30,0,60); // 80,90,130,90,5,50
CenterTitle(ballEwallE_B);
TH2D *ballEwallE_C=new TH2D(“ballEwallE_C”,";E_{wall} (MeV); E_{ball} (MeV)",75,0,150,30,0,60); // 80,90,130,90,5,50
CenterTitle(ballEwallE_C);
TH2D *BWchan_A=new TH2D(“BWchan_A”,";chan_{wall}; chan_{ball}",250,0,2500,250,0,2500);
TH2D *BWchan_B=new TH2D(“BWchan_B”,";chan_{wall}; chan_{ball}",250,0,2500,250,0,2500);
TH2D *BWchan_C=new TH2D(“BWchan_C”,";chan_{wall}; chan_{ball}",250,0,2500,250,0,2500);

/***************************************************************************************/
const int arraysize=150;
Double_t Bnr[arraysize],BLE[arraysize],BSE[arraysize], Btheta[arraysize], Bphi[arraysize];
Double_t MWPCphi, MWPCtheta, WDdE, WUdE, WLE, WRE, WLdet, WRdet, WDdet, WUdet, MWPCX, MWPCY, EFW;
Double_t T1, T2, T3, pureT1, pureT2, pureT3, pureRF;
Double_t EventType, ballhits;

int counter=-1, counterA=0 ,counterB=0,counterC=0, det, size, Centtheta, index102, index101, index103, index75;
float Lowtheta;
elasticchain->SetBranchAddress(“b3”,Bnr);
elasticchain->SetBranchAddress(“b4”,BLE);
elasticchain->SetBranchAddress(“b5”,BSE);
elasticchain->SetBranchAddress(“b6”,Btheta);
elasticchain->SetBranchAddress(“b7”,Bphi);

TBranch *b2 = elasticchain->GetBranch(“b2”);
TLeaf *ballhitsleaf = b2->GetLeaf(“ballhits”);
TLeaf *MWPCphileaf = b2->GetLeaf(“MWPCphi”);
TLeaf *MWPCthetaleaf = b2->GetLeaf(“MWPCtheta”);
TLeaf *WDdEleaf = b2->GetLeaf(“WDdE”);
TLeaf *WUdEleaf = b2->GetLeaf(“WUdE”);
TLeaf *WLEleaf = b2->GetLeaf(“WLE”);
TLeaf *WREleaf = b2->GetLeaf(“WRE”);
TLeaf *WLdetleaf = b2->GetLeaf(“WLdet”);
TLeaf *WRdetleaf = b2->GetLeaf(“WRdet”);
TLeaf *WDdetleaf = b2->GetLeaf(“WDdet”);
TLeaf *WUdetleaf = b2->GetLeaf(“WUdet”);
TLeaf *MWPCXleaf = b2->GetLeaf(“MWPCX”);
TLeaf *MWPCYleaf = b2->GetLeaf(“MWPCY”);
TLeaf *EFWleaf = b2->GetLeaf(“EFW”);
TLeaf *T1leaf = b2->GetLeaf(“T1”);
TLeaf *T2leaf = b2->GetLeaf(“T2”);
TLeaf *T3leaf = b2->GetLeaf(“T3”);
TLeaf *pureT1leaf = b2->GetLeaf(“pureT1”);
TLeaf *pureT2leaf = b2->GetLeaf(“pureT2”);
TLeaf *pureT3leaf = b2->GetLeaf(“pureT3”);
TLeaf *EventTypeleaf = b2->GetLeaf(“EventType”);
TLeaf *pureRFleaf = b2->GetLeaf(“pureRF”);

/************************************* loop over events *************************************/

for (Long64_t jentry=Firstentry; jentry<Lastentry; jentry++)
{
counter++;
fprintf(stdout,"\rEvent No=%2.2e\t",counter);

  elasticchain->GetEntry(jentry);

  ballhits=ballhitsleaf->GetValue();
  MWPCphi=MWPCphileaf->GetValue();
  MWPCtheta=MWPCthetaleaf->GetValue();
  WDdE=WDdEleaf->GetValue();
  WUdE=WUdEleaf->GetValue();
  WLE=WLEleaf->GetValue();
  WRE=WREleaf->GetValue();
  WLdet=WLdetleaf->GetValue();
  WRdet=WRdetleaf->GetValue();
  WDdet=WDdetleaf->GetValue();
  WUdet=WUdetleaf->GetValue();
  MWPCX=MWPCXleaf->GetValue();
  MWPCY=MWPCYleaf->GetValue();
  EFW=EFWleaf->GetValue();
  T1=T1leaf->GetValue();
  T2=T2leaf->GetValue();
  T3=T3leaf->GetValue();
  pureT1=pureT1leaf->GetValue();
  pureT2=pureT2leaf->GetValue();
  pureT3=pureT3leaf->GetValue();
  EventType=EventTypeleaf->GetValue();
  pureRF=pureRFleaf->GetValue();
 
  bool clustflag=true;
  double PEnergy=TMath::Sqrt(WLE*WRE)*ScalingFactor((int)WRdet); 
 
  if ( EventType==1 )
    {
     for (i=1; i<=1; i=i+1)  
        {
          det=(int) Bnr[i];

          if ( det==102 ) //findvalue(rsSize,rsArray,det)==1
            {
              if ( MWPCtheta>26.5 && MWPCtheta<28.5 && TMath::Abs(TMath::Abs(Bphi[i]-MWPCphi)-180)<dphi/2. )  
                 {   
                   if( TMath::Abs(pureRF-798.)<=3. || TMath::Abs(pureRF-(798.+38))<=3. || TMath::Abs(pureRF-(798.+2*38))<=3. || TMath::Abs(pureRF-(798.+3*38))<=3. || TMath::Abs(pureRF-(798.+4*38))<=3. || TMath::Abs(pureRF-(798.+5*38))<=3. )
                                 {
                                   counterA++;
                                   ballEwallE_A->Fill(PEnergy,E_calib(BSE[i],0,det,0,-1,1)); 
                                   ballEwallE->Fill(PEnergy,E_calib(BSE[i],0,det,0,-1,1));
                                   BWchan_A->Fill(TMath::Sqrt(WLE*WRE),BSE[i]); 
                                 }
                   if( TMath::Abs(pureRF-812.)<=3. || TMath::Abs(pureRF-(812.+38))<=3. || TMath::Abs(pureRF-(812.+2*38))<=3. || TMath::Abs(pureRF-(812.+3*38))<=3. || TMath::Abs(pureRF-(812.+4*38))<=3. || TMath::Abs(pureRF-(812.+5*38))<=3. )
                                 {
                                   counterB++;
                                   ballEwallE_B->Fill(PEnergy,E_calib(BSE[i],0,det,0,-1,2));
                                   ballEwallE->Fill(PEnergy,E_calib(BSE[i],0,det,0,-1,2)); 
                                   BWchan_B->Fill(TMath::Sqrt(WLE*WRE),BSE[i]);                                   
                                 }
                   if( TMath::Abs(pureRF-819.)<=3. || TMath::Abs(pureRF-(819.+38))<=3. || TMath::Abs(pureRF-(819.+2*38))<=3. || TMath::Abs(pureRF-(819.+3*38))<=3. || TMath::Abs(pureRF-(819.+4*38))<=3. || TMath::Abs(pureRF-(819.+5*38))<=3. )
                                 {
                                   counterC++;
                                   ballEwallE_C->Fill(PEnergy,E_calib(BSE[i],0,det,0,-1,3));
                                   ballEwallE->Fill(PEnergy,E_calib(BSE[i],0,det,0,-1,3));
                                   BWchan_C->Fill(TMath::Sqrt(WLE*WRE),BSE[i]);                                         
                                 }  
                 }  
            }
        }
    }
}

/************************************ end of loop over events *********************************/

fprintf(stdout,"\nevents=%d\n",counter+1);
fprintf(stdout,"\counterA=%d\n",counterA);
fprintf(stdout,"\counterB=%d\n",counterB);
fprintf(stdout,"\counterC=%d\n",counterC);

/************************************************ Draw histograms ***********************************************/

gStyle->SetOptStat(10);
c2->cd();
sprintf(name[4]," Group A MWPCtheta>25. && MWPCtheta<30. && TMath::Abs(TMath::Abs(Bphi[102]-MWPCphi)-180)<%d",dphi/2);
ballEwallE_A->SetTitle(name[4]);
ballEwallE_A->Draw(“colz”);
EdEp->Draw(“SAME”);
E1E2gr_1->Draw(“SAME”);
E1E2gr_2->Draw(“SAME”);
E1E2gr_3->Draw(“SAME”);
E1E2gr_4->Draw(“SAME”);
c2->Update();

ps->NewPage();
c2->cd();
sprintf(name[4]," Group B MWPCtheta>25. && MWPCtheta<30. && TMath::Abs(TMath::Abs(Bphi[102]-MWPCphi)-180)<%d",dphi/2);
ballEwallE_B->SetTitle(name[4]);
ballEwallE_B->Draw(“colz”);
EdEp->Draw(“SAME”);
E1E2gr_1->Draw(“SAME”);
//E1E2gr_2->Draw(“SAME”);
//E1E2gr_3->Draw(“SAME”);
//E1E2gr_4->Draw(“SAME”);
c2->Update();

ps->NewPage();
c2->cd();
sprintf(name[4]," Group C MWPCtheta>25. && MWPCtheta<30. && TMath::Abs(TMath::Abs(Bphi[102]-MWPCphi)-180)<%d",dphi/2);
ballEwallE_C->SetTitle(name[4]);
ballEwallE_C->Draw(“colz”);
EdEp->Draw(“SAME”);
E1E2gr_1->Draw(“SAME”);
//E1E2gr_2->Draw(“SAME”);
//E1E2gr_3->Draw(“SAME”);
//E1E2gr_4->Draw(“SAME”);
c2->Update();

ps->NewPage();
c2->cd();
sprintf(name[4]," all Groups MWPCtheta>25. && MWPCtheta<30. && TMath::Abs(TMath::Abs(Bphi[102]-MWPCphi)-180)<%d",dphi/2);
ballEwallE->SetTitle(name[4]);
ballEwallE->Draw(“colz”);
EdEp->Draw(“SAME”);
E1E2gr_1->Draw(“SAME”);
//E1E2gr_2->Draw(“SAME”);
//E1E2gr_3->Draw(“SAME”);
//E1E2gr_4->Draw(“SAME”);
c2->Update();

EdEp->Write();
E1E2gr_1->Write();
//E1E2gr_2->Write();
//E1E2gr_3->Write();
//E1E2gr_4->Write();

ps->NewPage();
c2->cd();
BWchan_A->Draw(“colz”);
c2->Update();

ps->NewPage();
c2->cd();
BWchan_B->Draw(“colz”);
c2->Update();

ps->NewPage();
c2->cd();
BWchan_C->Draw(“colz”);
c2->Update();

ps->Close();
fprintf(stdout,“All histograms are saved in %s”,psfile);
//elasticchain->ResetBranchAddresses();
myrootFile->Write();
myrootFile->Close();
timer.Stop();
Double_t rtime = timer.RealTime();
Double_t ctime = timer.CpuTime();
printf("\nRealTime=%d min, CpuTime=%d min\n",rtime/60.,ctime/60.);
}

/**************************************************************** main *************************************************************/

void CenterTitle(TH2D *h){
h->GetXaxis()->CenterTitle(kTRUE);
h->GetYaxis()->CenterTitle(kTRUE);
h->GetXaxis()->SetTitleSize(0.05);
h->GetYaxis()->SetTitleSize(0.05);
h->GetXaxis()->SetTitleOffset(0.9);
h->GetYaxis()->SetTitleOffset(1);
}

void CenterTitle(TH1D h){
h->GetXaxis()->CenterTitle(kTRUE);
h->GetYaxis()->CenterTitle(kTRUE);
h->GetXaxis()->SetTitleSize(0.05);
h->GetYaxis()->SetTitleSize(0.05);
h->GetXaxis()->SetTitleOffset(0.9);
h->GetYaxis()->SetTitleOffset(1);
}
/
********************************************************************************************************************************/
double E_calib(double Lchan, double Rchan, double det, double theta, int id, int groupNo)
{
int scint;
scint=(int) det;
switch(id)
{
case 1: /Wall D calibration/
double energy=0.;
double scale=0.;
for (int i=0;i<3;i++) scale = scale + (Dcalib[scint][i]pow(theta,i)); // scale = a0x^0 + a1
x^1 + a2
x^2 //
if (theta>0) energy=sqrt(Lchan
Rchan)/scale;
//fprintf(stdout,"E_calib %f\n ",energy);
return energy;

case -1:     /*Ball P calibration*/
   double energy=0.;
   double scale=0.;
   if ( groupNo == 1 ) 
      {    
        for (int j=0;j<3;j++)  scale = scale + (PcalibA[scint][j]*pow(Lchan,j));    // for line //
      }
   if ( groupNo == 2 ) 
      {    
        for (int j=0;j<3;j++)  scale = scale + (PcalibB[scint][j]*pow(Lchan,j));    // for line //
      }
   if ( groupNo == 3 ) 
      {    
        for (int j=0;j<3;j++)  scale = scale + (PcalibC[scint][j]*pow(Lchan,j));    // for line //
      }
   if (Lchan>0) energy = scale;                                              // for line //
   // for (int j=0;j<1;j++)  scale = scale + (Pcalib[scint][j]*(Lchan+Rchan));   // for scale //
   // if ((Lchan+Rchan)>0) energy = scale;                                       // for scale //
   // fprintf(stdout,"id %d detector ball  %d  chan %f  energy %f \n",id,scint,Lchan,energy);
   return energy;
default:
fprintf(stdout," Calibration id=%d is not correct \n",id);

}
return 0;
}

//
/
Check if a particular value exist in the array /
int findvalue ( int arrsize, int myarray[arrsize], int value)
{
int * p;
p = std::find (myarray, myarray+arrsize, value);
if (p!= myarray+arrsize) return 1;
else return 0;
}
/
/
double ScalingFactor(int BarNo){
double SF=0.0;
if(BarNo==11){
SF=Pcalib[1][1];
}
else if(BarNo==12){
SF=Pcalib[2][1];
}
else if(BarNo==13){
SF=Pcalib[3][1];
}
else if(BarNo==14){
SF=Pcalib[4][1];
}
else if(BarNo==15){
SF=Pcalib[5][1];
}
else if(BarNo==16){
SF=Pcalib[6][1];
}
else if(BarNo==17){
SF=Pcalib[7][1];
}
else if(BarNo==18){
SF=Pcalib[8][1];
}
else if(BarNo==19){
SF=Pcalib[9][1];
}
else if(BarNo==20){
SF=Pcalib[10][1];
}
return SF;
}

/*******************************************************************************************************************/
[/code]
and the macro that makes chain is called openntuple.cxx:

[code]
{

Double_t X[66];
Double_t Bnr[150];
Double_t BLE[150];
Double_t BSE[150];
Double_t Bphi[150];
Double_t Btheta[150];

//TCanvas *c1 = new TCanvas(“c1”,“BINA Canvas 1”); c1->SetFillColor(10); c1->Size(19,25);
//TCanvas *c2 = new TCanvas(“c2”,“BINA Canvas 2”); c2->SetFillColor(10); c2->Size(18,27);
//TCanvas *c3 = new TCanvas(“c3”,“BINA Canvas 3”); c3->SetFillColor(10); c3->Size(18,25);

TChain *elasticchain=new TChain("pdelastic");           
elasticchain->SetBranchAddress("b2",X);
elasticchain->SetBranchAddress("b3",Bnr);
elasticchain->SetBranchAddress("b4",BLE);
elasticchain->SetBranchAddress("b5",BSE);
elasticchain->SetBranchAddress("b6",Bphi);
elasticchain->SetBranchAddress("b7",Btheta);



elasticchain->Add("/media/9C2C3AB22C3A8774/Ntuples/21JunLmds/ntupleJuly2016_with40sub.root");
elasticchain->Add("/media/9C2C3AB22C3A8774/Ntuples/22JunLmds/ntupleJuly2016_with40sub_1.root");
//elasticchain->Add("/media/9C2C3AB22C3A8774/Ntuples/22JunLmds/ntupleJuly2016_with40sub_2.root");
//elasticchain->Add("/media/9C2C3AB22C3A8774/Ntuples/23JunLmds/ntupleJuly2016_with40sub_1.root");
//elasticchain->Add("/media/9C2C3AB22C3A8774/Ntuples/23JunLmds/ntupleJuly2016_with40sub_2.root");

//elasticchain->Add("/media/9C2C3AB22C3A8774/Ntuples/ElasticLmds/ntupleJuly2016_with40sub.root");

//TTree * pdelastic = elasticchain;
 //elasticchain.MakeClass("pdelastic");

// elasticchainHexoff->Print();

fprintf(stdout,"\n*********************************\n");
fprintf(stdout,"* OpenExprimentNtuple is loaded \n");
fprintf(stdout,"
********************************\n");
}[/code]
when if I want to read and fill histograms using the second rootfile the following error appears:
By the way, the first file has 30197259 events.

[code]*** Break *** segmentation violation
Event No=0.00e+00

===========================================================
There was a crash (#6 0xb7255c4b in SigHandler(ESignals) () from /home/bayat/root/lib/libCore.so).
This is the entire stack trace of all threads:

#0 0xb77b3424 in __kernel_vsyscall ()
#1 0xb6e12943 in waitpid () from /lib/i386-linux-gnu/libc.so.6
#2 0xb6d99ff3 in ?? () from /lib/i386-linux-gnu/libc.so.6
#3 0xb724e5cb in TUnixSystem::Exec(char const*) () from /home/bayat/root/lib/libCore.so
#4 0xb7252fb0 in TUnixSystem::StackTrace() () from /home/bayat/root/lib/libCore.so
#5 0xb7255b37 in TUnixSystem::DispatchSignals(ESignals) () from /home/bayat/root/lib/libCore.so
#6 0xb7255c4b in SigHandler(ESignals) () from /home/bayat/root/lib/libCore.so
#7 0xb724c672 in sighandler(int) () from /home/bayat/root/lib/libCore.so
#8 0xb727fde5 in textinput::TerminalConfigUnix::HandleSignal(int) () from /home/bayat/root/lib/libCore.so
#9 0xb727fe24 in (anonymous namespace)::TerminalConfigUnix__handleSignal(int) () from /home/bayat/root/lib/libCore.so
#10
#11 0xb4f4eaec in G__G__Tree_172_0_20(G__value*, char const*, G__param*, int) () from /home/bayat/root/lib/libTree.so
#12 0xb67b752c in Cint::G__ExceptionWrapper(int ()(G__value, char const*, G__param*, int), G__value*, char*, G__param*, int) () from /home/bayat/root/lib/libCint.so
#13 0xb6868a00 in G__execute_call () from /home/bayat/root/lib/libCint.so
#14 0xb6868dc4 in G__call_cppfunc () from /home/bayat/root/lib/libCint.so
#15 0xb6845817 in G__interpret_func () from /home/bayat/root/lib/libCint.so
#16 0xb6833542 in G__getfunction () from /home/bayat/root/lib/libCint.so
#17 0xb692d8e1 in G__getstructmem(int, G__FastAllocString&, char*, int, char*, int*, G__var_array*, int) () from /home/bayat/root/lib/libCint.so
#18 0xb6925b03 in G__getvariable () from /home/bayat/root/lib/libCint.so
#19 0xb680995a in G__getitem () from /home/bayat/root/lib/libCint.so
#20 0xb6810d08 in G__getexpr () from /home/bayat/root/lib/libCint.so
#21 0xb681a282 in G__getexpr () from /home/bayat/root/lib/libCint.so
#22 0xb689d8ac in G__exec_statement () from /home/bayat/root/lib/libCint.so
#23 0xb68a6c5a in G__exec_loop(char const*, char*, std::list<G__FastAllocString, std::allocator<G__FastAllocString> > const&) [clone .constprop.69] () from /home/bayat/root/lib/libCint.so
#24 0xb68a3d26 in G__exec_statement () from /home/bayat/root/lib/libCint.so
#25 0xb6846cb1 in G__interpret_func () from /home/bayat/root/lib/libCint.so
#26 0xb68335b8 in G__getfunction () from /home/bayat/root/lib/libCint.so
#27 0xb680a1e5 in G__getitem () from /home/bayat/root/lib/libCint.so
#28 0xb6810d08 in G__getexpr () from /home/bayat/root/lib/libCint.so
#29 0xb681ccf3 in G__calc_internal () from /home/bayat/root/lib/libCint.so
#30 0xb68adde4 in G__process_cmd () from /home/bayat/root/lib/libCint.so
#31 0xb721582a in TCint::ProcessLine(char const*, TInterpreter::EErrorCode*) () from /home/bayat/root/lib/libCore.so
#32 0xb720a84f in TCint::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) () from /home/bayat/root/lib/libCore.so
#33 0xb7164bd7 in TApplication::ExecuteFile(char const*, int*, bool) () from /home/bayat/root/lib/libCore.so
#34 0xb7164fac in TApplication::ProcessFile(char const*, int*, bool) () from /home/bayat/root/lib/libCore.so
#35 0xb71635c7 in TApplication::ProcessLine(char const*, bool, int*) () from /home/bayat/root/lib/libCore.so
#36 0xb702bf25 in TRint::HandleTermInput() () from /home/bayat/root/lib/libRint.so
#37 0xb702b922 in TTermInputHandler::Notify() () from /home/bayat/root/lib/libRint.so
#38 0xb702de82 in TTermInputHandler::ReadNotify() () from /home/bayat/root/lib/libRint.so
#39 0xb7254a60 in TUnixSystem::CheckDescriptors() () from /home/bayat/root/lib/libCore.so
#40 0xb72562fc in TUnixSystem::DispatchOneEvent(bool) () from /home/bayat/root/lib/libCore.so
#41 0xb71c6ac2 in TSystem::InnerLoop() () from /home/bayat/root/lib/libCore.so
#42 0xb71c922c in TSystem::Run() () from /home/bayat/root/lib/libCore.so
#43 0xb7160b48 in TApplication::Run(bool) () from /home/bayat/root/lib/libCore.so
#44 0xb702ca31 in TRint::Run(bool) () from /home/bayat/root/lib/libRint.so
#45 0x08048c7f in main ()

Root > Function E_calib_wall_ball2() busy flag cleared[/code]

Hi,

The issues stems from this pattern: TChain *elasticchain=new TChain("pdelastic"); ... TBranch *b2 = elasticchain->GetBranch("b2"); TLeaf *ballhitsleaf = b2->GetLeaf("ballhits"); ... for (Long64_t jentry=Firstentry; jentry<Lastentry; jentry++) { elasticchain->GetEntry(jentry); ballhits=ballhitsleaf->GetValue(); The address of the branch object and leaf object in a TChain do change whenever the TChain moves from one file to another. So at best this would use the value of the last entry of the first file from all the entries after the first file and at worse would crash (because the once the first file is done the branches and leaves are deleted). The minimal code change is to move the GetBranch and GetLeaf to be after the elasticchain->GetEntry. However this is relatively ineffecient. The better way is to use SetBranchAddress also for the branch ‘b2’ by passing the address of a struct of the ‘proper’ structure. To get some hints on this structure either take a look at the code that produced/wrote the file or take a look at the result of MakeClass on one of the trees/files.

Cheers,
Philippe.

2 Likes

[quote=“pcanal”]Hi,

The issues stems from this pattern: TChain *elasticchain=new TChain("pdelastic"); ... TBranch *b2 = elasticchain->GetBranch("b2"); TLeaf *ballhitsleaf = b2->GetLeaf("ballhits"); ... for (Long64_t jentry=Firstentry; jentry<Lastentry; jentry++) { elasticchain->GetEntry(jentry); ballhits=ballhitsleaf->GetValue(); The address of the branch object and leaf object in a TChain do change whenever the TChain moves from one file to another. So at best this would use the value of the last entry of the first file from all the entries after the first file and at worse would crash (because the once the first file is done the branches and leaves are deleted). The minimal code change is to move the GetBranch and GetLeaf to be after the elasticchain->GetEntry. However this is relatively ineffecient. The better way is to use SetBranchAddress also for the branch ‘b2’ by passing the address of a struct of the ‘proper’ structure. To get some hints on this structure either take a look at the code that produced/wrote the file or take a look at the result of MakeClass on one of the trees/files.

Cheers,
Philippe.[/quote]
It works.
Thank you so much.