I read from root.cern.ch/root/html/TH1F.html, to write an TH1F to a file:
TFile f("histos.root"); //maybe is better to use the option "RECREATE" or "NEW"
TH1F histo(...)
...
histo->Write();
how the Write method know where save data?? More: the contrary of Write is Read, not Get!!! Get is the contrary of Set.
I do two very simple programs:
-----------------write.cpp------------------
#include "TROOT.h"
#include "TApplication.h"
#include "TCanvas.h"
#include "TH1F.h"
#include "TFile.h"
#include "TPaveLabel.h"
int main(int argc,char **argv)
{
TApplication theApp("App", &argc, argv);
TH1F histo("histo","my histogram",100,0,10);
histo.Fill(3);
histo.Fill(4);
histo.Fill(4);
TCanvas canvas("canvas","my canvas",0,0,800,600);
histo.Draw();
TFile f("file1.root","RECREATE");
histo.Write();
f.Close();
theApp.Run();
return 0;
}
-----------------read.cpp-------------------
#include "TROOT.h"
#include "TApplication.h"
#include "TCanvas.h"
#include "TH1F.h"
#include "TFile.h"
#include "TPaveLabel.h"
int main(int argc,char **argv)
{
TApplication theApp("App", &argc, argv);
TFile f("file1.root");
TH1F *histo = (TH1F*)f.Get("histo");
// f.Close();
TCanvas canvas("canvas","my canvas",0,0,800,600);
histo->Draw();
theApp.Run();
return 0;
}
Now… why if I close the file in read.cpp I get an error
*** Break *** segmentation violation
what’s the problem? 1) open file 2) get data from file and store it in a container (TH1F) 3) close file, the data will be indipendent from the file! but not!
more: why I have to initalize dynamically an histo in read.cpp? Why in your documentation you use always the dynamic iniatilization?? If it is not necessary it is evil! And more: the cast (TH1F*) is deprecated, use static_cast<TH1F*> that is c++ standard instead. And more… why the cast is necessary? I don’t understand your philosophy here and in a lots of other things. I need to do a long data analysis of spectrum and I lost one day to learn how save histogram in ROOT format, it is not possibile!