Pyroot - read TH1F histograms from mmap file

being new to pyroot, I may still miss some concepts:
My problem is - how doI read a mmap file with TH1F histograms in it? They are created approximately like this:

TH1F *harray[7*8];
mmapfd3 = TMapFile::Create( "mmap.histo"  ,"RECREATE" , 16*1024000 ,  "TH1F_16");
harray[0]=new TH1F( "hist0" ,"vme_histogram",8192,0,8192);
mmapfd3->Add( harray[0] );  
mmapfd3->Update(  );

I can read it approximately from C++ like this

  TMapFile* mfile =TMapFile::Create("mmap.histo");
  TMapRec *mr = mfile->GetFirst();
  while (mfile->OrgAddress(mr)) {
    TString classn=mr->GetClassName();
    if ( strcmp(classn.Data(),"TH1F")==0){
 	   TString name=mr->GetName();
	   TH1F *h  =0;
	    h=(TH1F*)mfile->Get(name.Data(), h );
           TH1F *hc=(TH1F*)gDirectory->Get( name.Data()  );
   mr   = mr->GetNext();

Can I read it from pyroot? That would really eased up my situation. I tried something, I can read mmap file in python, but I am lost with PyROOT:


import ROOT
import mmap
import contextlib
import time
import os

#---- i can work with mmap in pure python
if os.path.isfile( mmapfile ):
    with open(mmapfile, "r+b") as f:
        mm1 = mmap.mmap(f.fileno(), 0)# memory-map the file,size 0= whole
        print("i...  MMAP HISTO OPENED")
# --- but i rather    think I must use ROOT objects....
mm=ROOT.TMapFile.Create( mmapfile ) #???

here I am stuck. I managed to receive an error:

Use rootn.exe or link application against "-lNew"```
When I want to open mmap from root, I must run `rootn.exe` by the way... 
I hope that there is some way... thanks

I am sorry, just now I have found TMapFile in pyROOT where the error message should have got removed… I am trying - because I am on Ubuntu …
Not really a change with LD_PRELOAD=$ROOTSYS/lib/; python3 however, TMapFile object appears…

from ROOT import TFile, TH1F, TH1D, TMapFile
f = TMapFile.Create( mmapfile );
print(mr.GetNext() )

and the corresponding errror is similar

Error in <TMapFile::TMapFile>: no memory mapped file capability available
Use rootn.exe or link application against "-lNew"
<ROOT.TMapFile object ("mmap.histo") at 0x2d22530>
<ROOT.TMapRec object at 0x(nil)>

Rather than

LD_PRELOAD=$ROOTSYS/lib/; python3

You meant either:

LD_PRELOAD=$ROOTSYS/lib/ python3

or (unlikely)

export LD_PRELOAD=$ROOTSYS/lib/; python3

but with the later now all commands will preload


1 Like

I don’t understand why libNew needs to be (pre)loaded explicitly in the first place (it doesn’t for me), but if it is needed, why not simply:

import ROOT

at the top of the script?

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