// // (C) Copyright Laurent Aphecetche (aphecetc@in2p3.fr) 1998-2002 // // This small program convert 1d and 2d histo from .root to .hbook files // This implies of course to adopt a scheme to go from TH? names // to HBOOK identifiers. // // Here's what we've choosen : // // if the histogram contains a numeric part, this part is taken as id ; // otherwise user is prompted for an id. // #include #include #include "cfortran/cfortran.h" #include "cfortran/hbook.h" #include "TFile.h" #include "TH1.h" #include "TH2.h" #include "TROOT.h" #include "TList.h" #include "TKey.h" #include "TSystem.h" #include "TProfile.h" #include "TClass.h" #define PAWC_SIZE 100000 #define NHISTOMAX 1000000 int pawc_[PAWC_SIZE] ; int Error ; void Usage(void) ; void book2(TH2F* h2) ; void book1(TH1F* h1) ; void bookp(TProfile* hp) ; void book(Int_t dim, TObject* obj) ; Int_t AskUser(char* title) ; Int_t GetId(TObject* obj, char* title) ; //________________________________________________________________________ void Usage(void) { printf("root2h filename.root [filename.hbook]\n") ; exit(1) ; } //________________________________________________________________________ void book(Int_t dim, TObject* obj) { if (dim==1) book1((TH1F*)obj) ; if (dim==2) book2((TH2F*)obj) ; if (dim==4) bookp((TProfile*)obj) ; } //________________________________________________________________________ Int_t AskUser(char* title) { Int_t id ; printf("Enter id for histogram %s\n",title) ; scanf("%d",&id) ; return id ; } //________________________________________________________________________ Int_t GetId(TObject* obj, char* name, char* title) { static Bool_t allocid[NHISTOMAX] ; static Bool_t first = kTRUE ; Int_t i,id ; Bool_t ok = kFALSE ; if (first) for (i=0;iGetTitle(),79) ; strncpy(name,obj->GetName(),79) ; i = 0 ; while (!isdigit(name[i]) && i=NHISTOMAX) { printf(" An id is greater than maximum allowed. No check performed\n") ; ok = kTRUE ; } if (id>0 && allocid[id]==0) { ok = kTRUE ; allocid[id] = kTRUE ; } else printf(" id should be > 0\n") ; } while (!ok) ; } else { id = atoi(name+i) ; if (id>=NHISTOMAX) printf(" An id is greater than maximum allowed. No check performed\n") ; if (id>0 && allocid[id]==0) { ok = kTRUE ; allocid[id] = kTRUE ; } else printf(" id should be > 0\n") ; } printf("ROOT histo %s (%s) will be id %d\n",name,title,id) ; return id ; } //________________________________________________________________________ void book2(TH2F* h2) { Int_t nx,ny,bx,by ; Float_t xmin,xmax,ymin,ymax ; Int_t id,i=0 ; Int_t nentries=0 ; Float_t* content ; char title[80] ; char name[80] ; nx = h2->GetXaxis()->GetNbins() ; xmin = h2->GetXaxis()->GetXmin() ; xmax = h2->GetXaxis()->GetXmax() ; ny = h2->GetYaxis()->GetNbins() ; ymin = h2->GetYaxis()->GetXmin() ; ymax = h2->GetYaxis()->GetXmax() ; content = new Float_t[nx*ny] ; id = GetId((TObject*)h2,name,title) ; HBOOK2(id,title,nx,xmin,xmax,ny,ymin,ymax,0.) ; for (by=1;by<=ny;by++) for (bx=1;bx<=nx;bx++) { content[nentries] = h2->GetCellContent(bx,by) ; nentries++ ; } HPAK(id,content) ; } //________________________________________________________________________ void bookp(TProfile* hp) { Int_t nx,ny,bx,by ; Float_t xmin,xmax,ymin,ymax ; Int_t id,i=0 ; Int_t nentries=0 ; Float_t* content ; char title[80] ; char name[80] ; char opt[80] ; nx = hp->GetXaxis()->GetNbins() ; xmin = hp->GetXaxis()->GetXmin() ; xmax = hp->GetXaxis()->GetXmax() ; ymin = hp->GetYaxis()->GetXmin() ; ymax = hp->GetYaxis()->GetXmax() ; strncpy(opt,hp->GetOption(),79) ; content = new Float_t[nx] ; id = GetId((TObject*)hp,name,title) ; HBPROF(id,title,nx,xmin,xmax,ymin,ymax,opt) ; for (bx=1;bx<=nx;bx++) { content[nentries] = hp->GetBinContent(bx) ; nentries++ ; } HPAK(id,content) ; } //________________________________________________________________________ void book1(TH1F* h1) { Int_t nx,bx ; Float_t xmin,xmax ; Int_t id,i=0 ; Int_t nentries=0 ; Float_t* content ; char title[80] ; char name[80] ; nx = h1->GetXaxis()->GetNbins() ; xmin = h1->GetXaxis()->GetXmin() ; xmax = h1->GetXaxis()->GetXmax() ; content = new Float_t[nx] ; id = GetId((TObject*)h1,name,title) ; HBOOK1(id,title,nx,xmin,xmax,0.) ; for (bx=1;bx<=nx;bx++) { content[nentries] = h1->GetBinContent(bx) ; nentries++ ; } HPAK(id,content) ; } //________________________________________________________________________ int main(int argc, char** argv) { Int_t i,istat ; Int_t len=1024 ; char hname[1024] ; // name of input histo char filein[1024] ; // .root in file char fileout[1024] ; // .hbook out file memset(filein,'\0',1024) ; // paranoia ? memset(fileout,'\0',1024) ; if (argc<2) Usage() ; strncpy(filein,argv[1],1023) ; if (argc>=2) strncpy(fileout,argv[2],1023) ; TFile* fin = new TFile(filein) ; if (!fin->IsOpen()) { printf(" Cannot open input file %s\n",filein) ; exit(1) ; } // we check that input file is not void TList* list = fin->GetListOfKeys() ; if (!list) { printf(" No keys found in file\n") ; exit(1) ; } TIter next(list) ; TKey* key ; TObject* obj ; Int_t dim ; HLIMIT(PAWC_SIZE) ; // go for the output file if (strcmp(filein,fileout)==0) { printf(" filein=fileout\n") ; exit(1) ; } char sroot2h[20]; char sn[2]; char space[2]; strcpy(sroot2h,"ROOT2H"); strcpy(sn,"N"); strcpy(space," "); HROPEN(1,sroot2h,fileout,sn,len,istat) ; while ( key = (TKey*)next() ) { obj = key->ReadObj() ; if (strcmp(obj->IsA()->GetName(),"TProfile")==0) dim = 4 ; else if (obj->InheritsFrom("TH2")) dim = 2 ; else if (obj->InheritsFrom("TH1")) dim = 1 ; else { dim = 0 ; printf(" Object %s is not 1D or 2D histogram : " "will not be converted\n",obj->GetName()) ; } book(dim,obj) ; } HROUT(0,istat,space) ; HREND(sroot2h) ; fin->Close() ; return 0; }