Re: Save a histogram as a text file on disk

From: Ida Häggström <ida.haggstrom_at_radfys.umu.se>
Date: Tue, 27 Sep 2011 13:01:46 +0200


Hello Eduard and thanks for your help!
I've managed to do what I wanted with your tips... Here's my code snippet if anyone else is interested:



//Save histogram as text file
ofstream myFile("histogram.txt");
myFile << "BinNo" << "\t"<< "Start" << "\t"<< "End" << "\t"<< "No" << endl; for (Int_t i=1; i<=myHist->GetNbinsX();i++) {

    myFile << i << "\t" <<
(double)(myHist->GetBinCenter(i))-(double)(myHist->GetBinWidth(i)/2.) << "\t" <<
(double)(myHist->GetBinCenter(i))+(double)(myHist->GetBinWidth(i)/2.) << "\t" << (double)myHist->GetBinContent(i) << endl; }
myFile.close();



Cheers, Ida

On Tue, Sep 27, 2011 at 9:45 AM, Eduard Avetisyan <Eduard.Avetisyan_at_desy.de>wrote:

>
> Hi Ida,
>
> Try this:
> // Start at 1 (not 0) as bin 0 is the underflow, you probably don't need it
> for (Int_t i=1; i<myHist->GetNbinsX();i++) {
> cout << i << " " << myHist->GetBinContent(i) << endl;
> }
>
> If you need not the bin number but the value, replace 'i' with
> myHist->GetBinCenter(i).
>
> Cheers
> Eduard
>
> On Tue, 27 Sep 2011, Ida Häggström wrote:
>
> > Dear Rooters,
> >
> > I'm making a little script that stores data in a histogram. Now I want to
> > save this data as a text file (to open in another program, e.g. Matlab)
> with
> > bin in one column and number in a second column. I cannot really find any
> > examples of this in the users guide... Is there a good way to do this?
> Below
> > are some relevant pieces of my code. Thanks!
> > /Ida
> >
> > ----------------------
> > TH1F *myHist= new TH1F("myHist","Sensitivity",100,-200.,+200.);
> >
> > for (Int_t i=0; i<nentries;i++) {
> > //Long loop...
> > myHist->Fill(z);
> > }
> > ----------------------
> >
>
Received on Tue Sep 27 2011 - 13:01:53 CEST

This archive was generated by hypermail 2.2.0 : Tue Sep 27 2011 - 17:50:02 CEST