How to read a tree and save its entries

Hi all,
I want to read a tree included an array named BSE with size of 17 and then I want to sort it. For this purpose I am using the program which the following is the part of it:

for(int i=Firstentry ; i<Nentries ; i++)
    {
      fprintf(stdout,"\nBSE=\t\t");
      elasticchain->Draw("sortEvent(BSE)","","",1,i);
      sort(ADCshort, ADCshort + 17);
      for (int j=0; j<17; j++) fprintf(stdout,"%d\t",ADCshort[j]);
    }

where the sortEvent() function is:

int sortEvent(double BSE)
{
 
  int k=0;
  double ADCshort[k]=BSE;
  fprintf(stdout,"%d\t",ADCshort[k]);
  k++;
  return 0;
}

But elements of ADCshort array after sorting are 0.
How to call ADCshort array in main program and use it?
Please help me.

Try:

const int BSE_length = 17;
Double_t BSE[BSE_length];
elasticchain->SetBranchAddress("BSE", BSE);
for (Long64_t jentry = Firstentry; jentry < Nentries; jentry++) {
  Long64_t ientry = elasticchain->LoadTree(jentry);
  if (ientry < 0) break;
  elasticchain->GetEntry(jentry);
  std::sort(BSE, BSE + BSE_length);
  fprintf(stdout, "\nBSE=\t");
  for (int j = 0; j < BSE_length; j++) fprintf(stdout, "\t%g", BSE[j]);
}
elasticchain->ResetBranchAddresses();

Thank you very much.
I encountered with the following error:
Error in TTree::SetBranchStatus: unknown branch -> BSE
BSE= 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0

Try:
elasticchain->Print();

The result is :

******************************************************************************
*Chain   :pdelastic : /media/Transcend/BINA/Ntuples/ElasticLmds/ntupleNov2015_peds.root *
******************************************************************************
******************************************************************************
*Tree    :pdelastic : BINA p-d Elastic                                       *
*Entries : 16394855 : Total =     16527110622 bytes  File  Size = 1765626647 *
*        :          : Tree compression factor =   9.36                       *
******************************************************************************
*Br    0 :b2        : ID/D:WLE:WLtdc:WLdet:WRE:WRtdc:WRdet:WDdE:WDtdc:WDdet: *
*         | WUdE:WUtdc:WUdet:MWPCX:MWPCY:MWPCU:MWPCtheta:MWPCphi:T1:T2:T3:T4:*
*         | LiveTime:FC:Pol:pureT1:pureT2:pureT3:pureT4:Time:10kHz:10kHzDT:  *
*         | TotFera:TotFeraDT:BeamCurrent:T2DT:T2DTDsc:EFW:wallhits:         *
*         | EventType:ballhits                                               *
*Entries :16394855 : Total  Size= 5377854220 bytes  File Size  = 1291152319 *
*Baskets :     3595 : Basket Size=    5795840 bytes  Compression=   4.17     *
*............................................................................*
*Br    1 :b3        : Bnr[17]/D                                              *
*Entries :16394855 : Total  Size= 2229851169 bytes  File Size  = 85100293 *
*Baskets :     1602 : Basket Size=    4496384 bytes  Compression=  26.20     *
*............................................................................*
*Br    2 :b4        : BLE[17]/D                                              *
*Entries :16394855 : Total  Size= 2229851169 bytes  File Size  = 112606781 *
*Baskets :     1602 : Basket Size=    4496384 bytes  Compression=  19.80     *
*............................................................................*
*Br    3 :b5        : BSE[17]/D                                              *
*Entries :16394855 : Total  Size= 2229851169 bytes  File Size  = 111594475 *
*Baskets :     1602 : Basket Size=    4496384 bytes  Compression=  19.98     *
*............................................................................*
*Br    4 :b6        : Btheta[17]/D                                           *
*Entries :16394855 : Total  Size= 2229851178 bytes  File Size  = 81919292 *
*Baskets :     1602 : Basket Size=    4496384 bytes  Compression=  27.22     *
*............................................................................*
*Br    5 :b7        : Bphi[17]/D                                             *
*Entries :16394855 : Total  Size= 2229851172 bytes  File Size  = 83141787 *
*Baskets :     1602 : Basket Size=    4496384 bytes  Compression=  26.82     *
*............................................................................*

Try: elasticchain->SetBranchAddress("b5", BSE); // BSE[17]/D

Many Thanks for your help. That problem is solved,
but could you tell me how to use “ballhits” leaf in branch b2?

I think, the best for you would be to try an “analysis skeleton”. See, for example, links in: How are multiple TTree->Draw()s done?

I could not find useful things in this link: How are multiple TTree->Draw()s done?

Try the simplest:
elasticchain->MakeClass();
and then see the newly generated “pdelastic.h” and “pdelastic.C” files (if the “elasticchain” tree name is “pdelastic”).

OK, I understood.
Thanks :slight_smile: