Fateme
July 13, 2020, 12:25pm
1
Hi guys, I have created a TTree to write some double number(their name is Fdata) in it and then read them. when I define Fdata as integer then the program print the integer part correctly, but if I define it as double parameter the program just print zero. How can I print these double number correctly?
The code is :
{
TFile* MyFile = new TFile("Badom.root");
TTree* TREE = (TTree*)MyFile->Get("MY TREE");
int Fdata = 0;
TREE->SetBranchAddress("FDATA", &Fdata);
}
Please read tips for efficient and successful posting and posting code
ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided
Your code doesn’t do anything. Could you post the full code?
Fateme
July 13, 2020, 5:46pm
3
That was just a part of the code.I have created ROOTFile already and named it Badom.root. Double numbers wrote in root file and I read them here.
{
double FinalData[NREvents][NrNSamplesPerEvent];
TFile* MyFile = new TFile("Badom.root");
TTree* TREE = (TTree*)MyFile->Get("MY TREE");
int Fdata = 0;
TREE->SetBranchAddress("FDATA", &Fdata);
cout << "Analyzing file ... " << endl << '\n';
Long64_t NEntries = TREE->GetEntries();
Long64_t EVENT_it = 0, SAMPLE_it = 0;
cout << "NrEntries: " << NEntries << endl;
for (Long64_t j = 0; j < NEntries; j++) {
Index++;
TREE->GetEntry(j);
FinalData[EVENT_it][SAMPLE_it] = Fdata;
SAMPLE_it += 1;
cout <<"Event is : "<<'\t'<< EVENT_it <<'\t'<<"sample is : "<<'\t'<< SAMPLE_it <<'\t'<<"data is:"<<'\t'<< Fdata << endl;
}
If you wrote double values, why do you want to read integers then?
Fateme
July 13, 2020, 9:44pm
5
No, I wanna to read them as double number, but when I define Fdate as a double parameter , the code just print zero, I mean the code print wrong number. But when I define Fdata as integer, the code print the integer part correctly. The Fdata parameter can be any type? If it can has double type so what is wrong? why I cannot print double number correctly?
What is the output of TREE->Print()
?
Fateme
July 14, 2020, 10:43am
7
Double numbers that stored in root file.I print them at cout line.
I was expecting a screenshot (copied/pasted), something like this:
root [1] t1->Print()
******************************************************************************
*Tree :t1 : a simple Tree with simple variables *
*Entries : 10000 : Total = 243611 bytes File Size = 180826 *
* : : Tree compression factor = 1.34 *
******************************************************************************
*Br 0 :px : px/F *
*Entries : 10000 : Total Size= 40615 bytes File Size = 37333 *
*Baskets : 2 : Basket Size= 32000 bytes Compression= 1.08 *
*............................................................................*
*Br 1 :py : py/F *
*Entries : 10000 : Total Size= 40615 bytes File Size = 37310 *
*Baskets : 2 : Basket Size= 32000 bytes Compression= 1.08 *
*............................................................................*
*Br 2 :pz : pz/F *
*Entries : 10000 : Total Size= 40615 bytes File Size = 36663 *
*Baskets : 2 : Basket Size= 32000 bytes Compression= 1.09 *
*............................................................................*
*Br 3 :random : random/D *
*Entries : 10000 : Total Size= 80722 bytes File Size = 54555 *
*Baskets : 3 : Basket Size= 32000 bytes Compression= 1.47 *
*............................................................................*
*Br 4 :ev : ev/I *
*Entries : 10000 : Total Size= 40615 bytes File Size = 14155 *
*Baskets : 2 : Basket Size= 32000 bytes Compression= 2.84 *
*............................................................................*
root [2]
Anyway, can you post your root file?
Fateme
July 14, 2020, 1:30pm
9
This is the root file:
Badom.root (792.7 KB)
I don’t see anything in your file:
root [0] TFile *f = TFile::Open("Badom.root")
(TFile *) 0x2e755c0
root [1] .ls
TFile** Badom.root EXAMPLE
TFile* Badom.root EXAMPLE
root [2] f->ls()
TFile** Badom.root EXAMPLE
TFile* Badom.root EXAMPLE
root [3]
Fateme
July 14, 2020, 3:15pm
11
Test this again please :
Badom.root (206.0 KB)
Your FDATA
variable is an integer (Fdata/I
):
root [4] t->Print()
******************************************************************************
*Tree :MY TREE : Tree to save guassian data *
*Entries : 499800 : Total = 2005877 bytes File Size = 151811 *
* : : Tree compression factor = 13.29 *
******************************************************************************
*Br 0 :FDATA : Fdata/I *
*Entries : 499800 : Total Size= 2005490 bytes File Size = 150803 *
*Baskets : 63 : Basket Size= 32000 bytes Compression= 13.29 *
*............................................................................*
root [5]
Fateme
July 14, 2020, 3:25pm
13
So what should I write instead Fdata/I ?
When you create the branch, create it with /F
, like “Fdata/F”. If you read the TTree documentation , or look at the tree tutorials , you will see how to properly create a tree with branches in the format you want. I would also suggest to start with the ROOT Primer
moneta
July 15, 2020, 8:43am
16
How it is defined the variable in the TTree ?
What is the result of TTree->Print()
?
Thanks @moneta , the issue is already solved…