TTreeReader and TTreeReaderValue

I want to know the “TTreeReaderArray<Char_t> name(myReader_coin_30,“name_Si”);” size, but don’t know what method to use。Can anyone help me with this problem, I will be very grateful to him。

Hi @forever ,
and welcome to the ROOT forum. TTreeReaderArray has a GetSize method, see the docs (you have to expand the list of public methods inherited from TTreeReaderArrayBase).

Cheers,
Enrico

1 Like

I use the method that you said,but the terminal show the message “segmentation violation” The code show as below.
TTreeReaderArray<Double_t> N_Erg(myReader_coin_30,“N_Erg”);
cout << "N_Erg.GetSize(): "<<N_Erg.GetSize()<< endl;

A segmentation violation is usually due to a bug in the code. It could be anything really. ROOT should print a stacktrace that points to where the segfault happens, if so please paste it here, it might be enough to tell what’s going on.

If that does not help, please share a minimal self-contained reproducer that we can run and debug.
Cheers,
Enrico

#include "TFile.h"
#include "TH1F.h"
#include "TTreeReader.h"
#include "TTreeReaderValue.h"
#include "TTreeReaderArray.h"
#include "math.h"
# include <string>
#include "TH1D.h"
#include "TH2D.h"
#include "Riostream.h"
#include "TGraph.h"
#include "TGraphErrors.h"

void plot()
{
//TH2D* Coin_Si_CLLB = new TH2D("Coin_Si_CLLB","Coin_Si_CLLB_45",6400,0.,160.,800,0.,20.);
TCanvas* c = new TCanvas();
TPad *pad1 = new TPad("pad1","pad1",0.03,0.62,0.98,0.92,32);
TPad *pad2 = new TPad("pad2","pad1",0.03,0.02,0.98,0.60,33);
pad1 -> Draw();pad2 -> Draw();
TH1F* h1 = new TH1F("h1","N_Erg",1000,0.,500.);
auto myFile = TFile::Open("result.root");
if (!myFile || myFile->IsZombie()) {
return;
}
TTreeReader myReader_coin_30("tree5", myFile);
TTreeReaderArray<Double_t> N_Erg(myReader_coin_30,"N_Erg");
cout << "N_Erg.GetSize(): "<<N_Erg.GetSize()<< endl;
while (myReader_coin_30.Next())
{
h1 -> Fill(N_Erg[0]);
}
pad1 -> cd();
pad1 -> SetLogy();
h1 -> GetXaxis() -> SetRangeUser(0,500);
h1 -> Draw();
//------------------------
ifstream input;
input.open("./Erg.txt");
if(!input)
{
cout << "do not open the file!" << endl;
return;
}
double x[306];
double y[306];
double a =0;
for(int i = 0; i < 306; i++)
{
input >> x[i] >> y[i];
a = a + y[i]/40.8173;
}
TGraph *gr = new TGraph(306,x,y);
gr -> SetLineColor(kRed);
pad2 -> cd();
pad2 -> SetLogy();
gr -> GetXaxis() -> SetRangeUser(0,500);
gr -> Draw();
}

This is my code,I really do not know how to solve this problem,please help me! Thank you very much!

Hi,
we can’t run and debug the code without the input data.

Cheers,
Enrico

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.