Ntuple and array

Hello,
I have a question (may be naive) about ntuple and an array.
I would like to make something like:

Float_t E[3];
TNtuple *ntuple = new TNtuple(“ntuple”,“nutple”,“E[]…”);
for(Int_t j=0;j<4>Fill(E[j],…);

and for example:
ntuple->Draw(“E[1]”);

can I do this (does it makes sense) ?
by advance thank you
cheers
olivier dadoun

Hi,

Your code was mangled by the forum’s HTML processor. Please disable HTML in your post containing code.

The short answer to your questing seems to be: yes
However, you did not specify (or I did not understood for sure) your intent and hence I don’t know whether this would do what you expect.

Cheers,
Philippe

ok thank you, and I modify a little bit my question to be more understandable …

I have a question (may be naive) about ntuple and an array.
I would like to make something like:

Float_t E[][4];
TNtuple *ntuple = new TNtuple(“ntuple”,“nutple”,“E[][]…”);
for (Int_t j=0; j <= 3; j++)
{
PChain[j]->SetBranchAddress(“x”, &x);
for (Int_t i=0; i<PChain[j]->GetEntries(); i++)
{
PChain[j]->GetEntry(i);
E[][j]=x;
ntuple->Fill(E[][j],…);
}
}
and for example: ntuple->Draw(“E[1]:E[2]”);

My question is:
can I fill a ntuple by array variable ? (does it makes sense) ?
by advance thank you
cheers
olivier dadoun

Yes you can. Yes it makes sense. And Yes you could make the drawing call you wrote.

Now, instead of TNtuple, you should use TTree directly.Float_t E[4]; TTree *ntuple = new TTree("ntuple","nutple"); ntuple->Branch("E",&(E[0]),"E[4]/F"); for (Int_t j=0; j <= 3; j++) { PChain[j]->SetBranchAddress("x", &(E[j])); } for (Int_t i=0; i<PChain[j]->GetEntries(); i++) { for (Int_t j=0; j <= 3; j++) { PChain[j]->GetEntry(i); } ntuple->Fill(); }

Thank you Philippe.
I have modified a little bit the piece of code:

Float_t E[4];
TTree *ntuple = new TTree(“ntuple”,“nutple”);
ntuple->Branch(“E”,&(E[0]),“E[4]/F”);
for (Int_t j=0; j <= 3; j++)
{
PChain[j]->SetBranchAddress(“x”, &(E[j]));

 for (Int_t i=0; i<PChain[j]->GetEntries(); i++) 
{  
    PChain[j]->GetEntry(i);  
   ntuple->Fill(); 
} 

}
Are you ok with this ?

if yes I have two problems.
The first one appear when I try to draw the last array:
ntuple->Draw(“E[3]”)
give something wrong.
It is strange cause the results are good for E[0],E[1] and E[2].
The second problem appear when I quit ROOT
root [2] .q

*** Break *** segmentation violation
Root >

an idea ?
thank you
cheers
olivier

[quote]Are you ok with this ?[/quote]Not this is wrong.
When you do ntuple->Fill()it create a whole new entry with the current value of all variables. So in your case (assuming I read correctly and assuming your initialize E to 0). The contain for your ntuple whole entry E[0] E[1] E[2] E[3] 0 x_0_0 0 0 0 .... n x_0_n 0 0 0 n+1 x_0_n x_1_0 0 0 n+2 x_0_n x_1_1 0 0 ... 2*n x_0_n x_1_n 0 0 ...
where x_j_i is the content of x for the entry i of your pChain[j]

[quote]The second problem appear when I quit ROOT[/quote]It must be related to code you have not shown.

Cheers,
Philippe

Hello Philip ,

thanks a lot for your explanations.
Something that I don’t understand is

for (Int_t j=0; j <= 3; j++)
{
PChain[j]->SetBranchAddress(“x”, &(E[j]));
}
for (Int_t i=0; i<PChain[j]->GetEntries(); i++)

Correct me if I am wrong but PChain[j]->GetEntries() outside of the j loop have no meaning …
Below you will find my bad code …
(thank you for your patience :slight_smile: )

void NewreadTree(char *filename)
{
TChain *PChain[256];
Char_t ChainName[256];
Float_t xx[3];

TTree *ntuple2 = new TTree("ntuple2","ntuple2");

ntuple2->Branch(“xx”,&(xx[0]),“xx[3]/F”);

for (Int_t j=0; j <= 2 ; j++)
{
if(j==0)sprintf(ChainName,“ir_IR_SAMPLER0_SQL_1”);
if(j==1)sprintf(ChainName,“ir_IR_SAMPLER1_SQL_1”);
if(j==2)sprintf(ChainName,“ir_IR_SAMPLER2_SQL_1”);

PChain[j]=new TChain(ChainName,“chain”);

PChain[j]->Add(filename);
PChain[j]->SetBranchAddress(“x”,&(xx[j]));

for (Int_t  i=0; i<PChain[j]->GetEntries(); i++)   
{
  PChain[j]->GetEntry(i);  
  ntuple2->Fill();
}

}
}

[quote]Correct me if I am wrong but PChain[j]->GetEntries() outside of the j loop have no meaning … [/quote]Yes. That’s why in the code I sent you there j loop is there twice. Once to setup the addresses. Once to call all the GetEntry within the entry loop.

[quote]
Below you will find my bad code … [/quote]Still nothing obvious leading to crash … the only way to help further (about the crash) would be for you to sent a complete running example.

Cheers,
Philippe.

Hello,

In my case the values PChain[j]->GetEntries() is different for all j.
So the trick I found is :

Int_t max=0;
for (Int_t j=jstar; j <= jend ; j++)
{

max=TMath::Max(max,(Int_t)PChain[j]->GetEntries());
}
for (Int_t i=0; i<max; i++){
for (Int_t j=jstar; j <= jend; j++) PChain[j]->GetEntry(i);
ntuple2->Fill();
}
And it work :slight_smile:
The only think is that the mean and the rms value change a little bit.
Thank you
cheers
olivier

Yes you can. Yes it makes sense. And Yes you could make the drawing call you wrote.

Now, instead of TNtuple, you should use TTree directly.Float_t E[4]; TTree *ntuple = new TTree("ntuple","nutple"); ntuple->Branch("E",&(E[0]),"E[4]/F"); for (Int_t j=0; j <3>SetBranchAddress("x", &(E[j])); } for (Int_t i=0; i<PChain>GetEntries(); i++) { for (Int_t j=0; j <3>GetEntry(i); } ntuple->Fill(); } [/quote]

I run the programm ,but it is wrong in
PChain[j]->SetBranchAddress(“x”, &(E[j]));

please upload you complete programm,OK?
you use which root?

Hello ,

I use Root 5.11/02 under mac os X (10.4.7).
Below you wil find my complete program.
cheers
olivier

void NewreadTree(char *filename)
{
gStyle->SetOptTitle(0);
Float_t E,x;

Int_t jstar=0;
Int_t jend=3;
TChain *PChain[256];
Char_t ChainName[256];
Int_t Ntot=0;

Float_t xx[4];
TTree *ntuple2 = new TTree(“ntuple2”,“ntuple2”);
Int_t max=0;
ntuple2->Branch(“xx”,&(xx[0]),“xx[4]/F”);
for (Int_t j=jstar; j <= jend ; j++)
{
if(!filename)
{
cout << “Error : Syntax:” << endl;
cout << “root ‘NewreadTree.C(“YOUR ROOT FILE”)’” << endl;
exit;
}
else
if(j==0)sprintf(ChainName,“ir_IR_SAMPLER0_SQL_1”);
if(j==1)sprintf(ChainName,“ir_IR_SAMPLER1_SQL_1”);
if(j==2)sprintf(ChainName,“ir_IR_SAMPLER2_SQL_1”);
if(j==3)sprintf(ChainName,“ir_IR_SAMPLER3_SQL_1”);

PChain[j]=new TChain(ChainName,“chain”);
PChain[j]->Add(filename);
PChain[j]->SetBranchAddress(“x”,&(xx[j]));

max=TMath::Max(max,(Int_t)PChain[j]->GetEntries());
}
for (Int_t i=0; i<max; i++){
for (Int_t j=jstar; j <= jend; j++) PChain[j]->GetEntry(i);
ntuple2->Fill();
}
}

Thanks for help!
Could you upload you data(if too big please upload a little data) so that i can run the programm?