[d2n-analysis-talk] ROOT question: writing data from one file to another

Brad Sawatzky brads at jlab.org
Mon Mar 1 18:06:40 EST 2010


Hi Diana,

My naive approach would be to:
  - Create output root file
      TFile *hfile_test = new TFile("test.root","new"); 
  - Open main root file
      TFile *hfile = new TFile("runXXXX.root");
  - generate my histos from the runXXX.root
      TH1F *h10 = new TH1F("h10","foobar",nbins,xlow,xhi); 
      T->Project("h10", "BB.foo[0]",....);
      h10->Draw();
  - cd() to the test root file and save them:
    hfile_test.cd();
      h10->Write();
  - cd() back to the main root file and continue work
      hfile.cd();
      ...

  - Note that you will probably have to close (or flush) hfile_test to
    get consistent results (although a clean quit of the analyzer/root
    should do a close for you).
      hfile_test->Close();

  - If you reopen test.root you may have to do this to access the saved
    histogram(s):
      TFile f("test.root");
      TH1F *h = (TH1F*)f.Get("h10");
      h->Draw();
    I really don't know though...  Maybe it will 'autoload' the histos
    for you so you can access them without the extra step.  TBrowser
    might help here too...

-- Brad

On Mon, 01 Mar 2010, Diana Parno wrote:

> Hello all,
> 
> I have a fairly straightforward ROOT question that I thought I'd bring the 
> experts here.
> 
> Let's say I'm in a situation where I've got some data structures in a
> ROOT file (test1.root) -- trees, histograms, whatever. I want to
> manipulate this data, maybe make a new histogram or two, and write a
> couple of histograms to a different ROOT file (test2.root). What's the
> best way to do this?
> 
> In my experience, as soon as I open test2.root, test1.root and all its
> data structures go out of scope. If I have a histogram drawn on a pad,
> then it stays in scope long enough to write it to the second file, but
> this is somewhat unwieldy, easy to mess up, and hard to do if I want
> to manipulate lots of files via a script. There's got to be a better
> way to do this! Does anyone know the secret?
> 
> Thanks,
> Diana
-- 
Brad Sawatzky, PhD <brads at jlab.org>  -<>-  Jefferson Lab / Hall C / C111
  Ph: 757-269-5947 -<>- Pager: 757-584-5947 -<>- Fax: 757-269-7848
The most exciting phrase to hear in science, the one that heralds new
  discoveries, is not "Eureka!" but "That's funny..."   -- Isaac Asimov


More information about the d2n-analysis-talk mailing list