Accessing a root file while it is filled

Hello Rooters,

Imagine the following scenario: A program is creating a .root file and is filling it with histograms. Is it possible to view the histograms inside the .root file while the program is still running?

The following code does not work for me:

Program, that creates the rootfile:

void main()
{
     TFile rootfile = TFile::open("test.root","RECREATE");
     TH2D * twod = new TH2D("hpxpy","py vs px",40,-4,4,40,-4,4);
     twod->SetOption("colz");
     twod->Write();
     Double_t px,py;
     Int_t counter = 0;
     while(1)
     {
        gRandom->Rannor(px,py);
        twod->Fill(px,py);
        if (counter%300 == 0)
             rootfile->Flush();
        counter++
     }

}

The code above should create the rootfile “test.root”, which contains the histogram “hpxpy”. I want to open this file inside a root session and look at the histogram. Unfortunatly the histogram is empty and stays empty all the time. I was thinking that as soon as the function “rootfile->Flush()” is called the information in the memory is synchronized with test.root?
How do I have to rewrite the code above to archive what I want? Or is it not possible?

Thank you,
Lutz

On the producer run the following script:

[code]void lutz()
{
TFile *rootfile = TFile::Open(“test.root”,“RECREATE”);
TH2D * twod = new TH2D(“hpxpy”,“py vs px”,40,-4,4,40,-4,4);
twod->SetOption(“colz”);
Double_t px,py;
Int_t counter = 0;
while(1)
{
gRandom->Rannor(px,py);
twod->Fill(px,py);
if (counter%300 == 0) {
twod->Write(0,TObject::kWriteDelete);
rootfile->SaveSelf();
gSystem->Sleep(100);
}
counter++;
}

}
[/code]

In a different process, do
root > TFile *f = TFile::Open(“test.root”)
root > .x lutzr.C(f)
where lutzr.C is the file below

void lutzr(const TFile *f, const char *name="hpxpy") { f->ReadKeys(); delete f->FindObject(name); TH2 *hpxpy = (TH2*)f->Get(name); hpxpy->Draw(); }

Rene

Hello Rene,

thank you for your solution. Unfortunatly the script lutzr.c always gives the error Error: illegal pointer to class object hpxpy 0x0 636 C:\test\lutzr.c(5) *** Interpreter error recovered ***

I am using rootversion[code]


  •                                     *
    
  •    W E L C O M E  to  R O O T       *
    
  •                                     *
    
  • Version 5.12/00 10 July 2006 *
  •                                     *
    
  • You are welcome to visit our Web site *
  •      http://root.cern.ch            *
    
  •                                     *
    

Compiled on 11 July 2006 for win32.

CINT/ROOT C/C++ Interpreter version 5.16.13, June 8, 2006
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.[/code]

What Am I doing wrong?

Thank you,

Lutz

Are you running my scripts unchanged?
Are you running your view session as indicated?

root > TFile *f = TFile::Open("test.root") root > .x lutzr.C(f)

Rene

Hello Rene,

I found the problem. There was an error opening the file. Once this error was gone, and the file was opened correctly the lutzr.c Macro did work fine.

Thank you,

Lutz