Home | News | Documentation | Download

Sort a tree using "time" variable

Dear,

I am trying to sort a tree using a leaf called “time” (Double_t). I am using this code:

void sort()
{
TFile *fin;
Int_t p=0,q=0;
fin = new TFile(Form("./output/output%i%i.root",p,q));
cout << "Opening file output" << p << q << ".root" << endl;
if (fin->IsZombie()) 
{
   cout << "Unable to open file ./output" << p << q << ".root" << endl;
   return;
}
TTree *OptData = (TTree*)gDirectory->Get("Hits");
OptData->SetBranchStatus("*",0);
OptData->SetBranchStatus("edep",1);
OptData->SetBranchStatus("time",1);
OptData->SetBranchStatus("posX",1);
OptData->SetBranchStatus("posY",1);
OptData->SetBranchStatus("pixelID",1);
OptData->SetBranchStatus("eventID",1);
OptData->SetBranchStatus("PDGEncoding",1);
OptData->SetMaxVirtualSize(2e+9);
OptData->LoadBaskets(2e+9);
cout << "Building index" << endl;
OptData->BuildIndex("0","time");
cout << "Get tree index" << endl;
TTreeIndex *index = (TTreeIndex*)OptData->GetTreeIndex();
//open new file to store the sorted Tree
TFile f2("./output/output_sorted.root","recreate");
//Create an empty clone of the original tree
cout << "Clone tree" << endl;
TTree *tsorted = (TTree*)OptData->CloneTree(0);
tsorted->SetAutoSave();
cout << "For loop" << endl;
int m=0;
for( Long64_t i = index->GetN() - 1; i >=0 ; --i )
{
   Long64_t local = OptData->LoadTree( index->GetIndex()[i] );
   OptData->GetEntry(local);
    tsorted->Fill();
}
tsorted->Write();
}

However, my output is still not sorted by “time”:

root [1] (TFile *) 0x55ffbd66d2d0
(TFile *) 0x55ffbd6611e0
************************************************************************************************
*    Row   * PDGEncodi * time.time * edep.edep * posX.posX * posY.posY * pixelID.p * eventID.e *
************************************************************************************************
*        0 *       -22 * 1.241e-06 * 2.718e-06 * -1.305589 * 0.9657539 *        -1 *      1174 *
*        1 *       -22 * 1.183e-06 * 2.451e-06 * -1.472175 * 0.7696531 *        -1 *      1174 *
*        2 *       -22 * 1.185e-06 * 2.510e-06 * -0.697627 * 0.0614384 *        -1 *      1174 *
*        3 *       -22 * 1.257e-06 * 3.053e-06 * 1.4349747 * -0.403005 *         0 *      1174 *
*        4 *       -22 * 1.180e-06 * 2.515e-06 * 0.1504872 * 0.5761914 *         0 *      1174 *
*        5 *       -22 * 1.185e-06 * 2.722e-06 * -0.867718 * -0.874481 *        -1 *      1174 *
*        6 *       -22 * 1.188e-06 * 2.908e-06 * -0.058795 * -0.498979 *         0 *      1174 *
*        7 *       -22 * 1.210e-06 * 2.832e-06 * -1.286729 * 1.4880576 *         0 *      1174 *
*        8 *       -22 * 1.180e-06 * 2.889e-06 * -1.077313 * 0.0854040 *         0 *      1174 *
*        9 *       -22 * 1.202e-06 * 2.346e-06 * 0.1655144 * -0.039745 *         0 *      1174 *
*       10 *       -22 * 1.218e-06 * 2.692e-06 * 0.1078747 * -0.314180 *        -1 *      1174 *
*       11 *       -22 * 1.183e-06 * 3.054e-06 * -0.071446 * -0.750333 *        -1 *      1174 *

What am I doing wrong?

Regards,
Daniel


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.24.06
Platform: Linux
Compiler: g++


Perhaps @pcanal can provide some hints here.

Cheers,
J.

Any help is appreciated here. :slight_smile:

Try: OptData->BuildIndex("0", "1e12 * time");

It worked. Thanks!

For the record, the index created by TTree are stored as a 64 bit integer (they were originally used for run number and event number). The time values in your TTree seems to be well below one and thus all the index values (in the original post) are zero.