Save histogram in txt file


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.18.00
Platform: Ubuntu 18.04.5 LTS
Compiler: Not Provided


Good evening!

I’m run a code and save histogram generate in root file and txt, but I’m doing some wrong, because what appears in the txt is:

   TH1F *values__1 = new TH1F("values__1","Kinect Energy spectrum - Fotoes Totais",3500,0,0.0455);
   values__1->SetBinContent(317,1);
   values__1->SetBinContent(319,1);
   values__1->SetBinContent(320,1);
   values__1->SetBinContent(321,2);
   values__1->SetBinContent(323,2);
   values__1->SetBinContent(324,1);
   values__1->SetBinContent(325,2);
   values__1->SetBinContent(328,2);
   values__1->SetBinContent(329,3);
   values__1->SetBinContent(332,1);
   values__1->SetBinContent(333,2);
   values__1->SetBinContent(334,2);
   values__1->SetBinContent(335,1);
   values__1->SetBinContent(336,1);
   values__1->SetBinContent(337,3);
   values__1->SetBinContent(339,1);
   values__1->SetBinContent(340,8);
   values__1->SetBinContent(341,5);
   values__1->SetBinContent(342,3);
   values__1->SetBinContent(343,1);

But I expected that was values and what appears it is the canvas values, isn’t? what I am doing wrong?

I use ana->SaveAs(“example.txt”);

Best regards

Hi,

There is no plain text storage for histogram.
But you can try json format - doing SaveAs(“example.json”);
Probably it will fit your needs.

Regards,
Sergey

Thanks @linev! I see that in root5 there was some eample of macro to convert root in txt file. I think applied this but when I do some errors appears? Have you or anyone show me an example in root 6.18.00?

Best Regards!

If you have ROOT5 macro, it should also work with ROOT6.
You probably can just loop over histogram bins and create text file yourself.

@linev I use this code:

void dumpHistoToTxt()
{
TFile *f=new TFile("SPEC_TotalFotoes1.root"); // opens the root file
TH1F * tr=(TH1F *)f->Get("SPEC_TotalFotoes1");

float ener,value;

ofstream myfile;
myfile.open ("testeFotoes1.txt");
myfile << "ener value\n";

for (int idx=0; idx<tr->GetNbinsX();++idx) {
   ener = tr->GetBinCenter(idx);
   value = tr->GetBinContent(idx);
   cout << ener << " " << value << endl; //print to the screen
   myfile << ener << " " << value << "\n"; //write to file
}

myfile.close();
}

I use the histogram in root file.

Can you help in this? I place here the text file generated, but is wrong.

SPEC_TotalFotoes1.root (6.4 KB) SPEC_TotalFotoes1.txt (63.4 KB)

I find out in other question here, this line of command that provide something:

.> values.txt
values->Print("all");
.>

This give me:

 fSumw[0]=0, x=-6.5e-06
 fSumw[1]=0, x=6.5e-06
 fSumw[2]=0, x=1.95e-05
 fSumw[3]=0, x=3.25e-05
 fSumw[4]=0, x=4.55e-05
 fSumw[5]=0, x=5.85e-05
 fSumw[6]=0, x=7.15e-05
 fSumw[7]=0, x=8.45e-05
 fSumw[8]=0, x=9.75e-05
 fSumw[9]=0, x=0.0001105
 fSumw[10]=0, x=0.0001235
 fSumw[11]=0, x=0.0001365

But what I need is:
x y
energy count
0.001105 1000

Could I do it?

Thanks

best regards

TH1F *tr; f->GetObject("values", tr);

Thanks for the repllied @Wile_E_Coyote!

I substitued the

TH1F * tr=(TH1F *)f->Get("SPEC_TotalFotoes1");

to

TH1F *tr; f->GetObject("values", tr);

This give me:

0.0001105 0.0001105
0.0001235 0.0001235
0.0001365 0.0001365
0.0001495 0.0001495
0.0001625 0.0001625
0.0001755 0.0001755

But my Y axis is the count generated from the attached file, it is possible do it? In file I save the histogram a root file, but I need the data in a txt file.
specTi.C (13.7 KB) specTi.h (17.3 KB)

Best regards!

Sorry, I cannot identify place where you make printout.
But seems to be you just print GetBinCenter value twice

Thanks, @linev for the repllied. I notice that in the code that I use was place the BinCenter twice. When I fixed this, was correct now.

Thanks all for the help.

One doubt more: It’s possible to add a third column that will represent the normalize integral to 1?

Best Regards

You can use GetIntegral method to normalize your values:

double integral = tr->ComputeIntegral();
...
   ener = tr->GetBinCenter(idx);
   value = tr->GetBinContent(idx);
   value_normalized = value / integral;

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