THbookFile.cxx

Go to the documentation of this file.
00001 // @(#)root/hbook:$Id: THbookFile.cxx 36246 2010-10-10 10:13:03Z brun $
00002 // Author: Rene Brun   18/02/2002
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2002, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 ////////////////////////////////////////////////////////////////////////
00013 //  This class is an interface to the Hbook objects in Hbook files
00014 //  Any Hbook object (1-D, 2-D, Profile, RWN or CWN can be read
00015 //  NB: a THbookFile can only be used in READ mode
00016 //      Use the utility in $ROOTSYS/bin/h2root to convert Hbook to Root
00017 //
00018 // Example of use:
00019 //  gSystem->Load("libHbook");
00020 //  THbookFile f("myfile.hbook");
00021 //  f.ls();
00022 //  TH1F *h1 = (TH1F*)f.Get(1);  //import histogram ID=1 in h1
00023 //  h1->Fit("gaus");
00024 //  THbookTree *T = (THbookTree*)f.Get(111); //import ntuple header
00025 //  T->Print();  //show the Hbook ntuple variables
00026 //  T->Draw("x","y<0"); // as in normal TTree::Draw
00027 //
00028 //  THbookFile can be browsed via TBrowser.
00029 //
00030 ////////////////////////////////////////////////////////////////////////
00031 
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <ctype.h>
00035 
00036 #include "TROOT.h"
00037 #include "THbookFile.h"
00038 #include "TH2.h"
00039 #include "THbookTree.h"
00040 #include "THbookBranch.h"
00041 #include "THbookKey.h"
00042 #include "TGraph.h"
00043 #include "TProfile.h"
00044 #include "TTreeFormula.h"
00045 #include "TLeafI.h"
00046 #include "TBrowser.h"
00047 #include "TSystem.h"
00048 #include "TMath.h"
00049 
00050 #define PAWC_SIZE 2000000
00051 
00052 //  Define the names of the Fortran common blocks for the different OSs
00053 
00054 #ifndef WIN32
00055 #  define pawc pawc_
00056 #  define quest quest_
00057 #  define hcbits hcbits_
00058 #  define hcbook hcbook_
00059 #  define rzcl rzcl_
00060 int pawc[PAWC_SIZE];
00061 int quest[100];
00062 int hcbits[37];
00063 int hcbook[51];
00064 int rzcl[11];
00065 #else
00066 #  define pawc   PAWC
00067 #  define quest  QUEST
00068 #  define hcbits HCBITS
00069 #  define hcbook HCBOOK
00070 #  define rzcl   RZCL
00071 extern "C" int pawc[PAWC_SIZE];
00072 extern "C" int quest[100];
00073 extern "C" int hcbits[37];
00074 extern "C" int hcbook[51];
00075 extern "C" int rzcl[11];
00076 #endif
00077 
00078 int *iq, *lq;
00079 float *q;
00080 char idname[128];
00081 int nentries;
00082 char chtitl[128];
00083 int ncx,ncy,nwt,idb;
00084 int lcont, lcid, lcdir, ltab;
00085 float xmin,xmax,ymin,ymax;
00086 const Int_t kNRH  = 6;
00087 const Int_t kMIN1 = 7;
00088 const Int_t kMAX1 = 8;
00089 
00090 static Int_t gLastEntry = -1;
00091 
00092 //  Define the names of the Fortran subroutine and functions for the different OSs
00093 
00094 #ifndef WIN32
00095 # define hlimit  hlimit_
00096 # define hldir   hldir_
00097 # define hropen  hropen_
00098 # define hrend   hrend_
00099 # define hrin    hrin_
00100 # define hnoent  hnoent_
00101 # define hgive   hgive_
00102 # define hgiven  hgiven_
00103 # define hgnpar  hgnpar_
00104 # define hgnf    hgnf_
00105 # define hgnt    hgnt_
00106 # define hgntf   hgntf_
00107 # define hgnt1   hgnt1_
00108 # define rzink   rzink_
00109 # define hdcofl  hdcofl_
00110 # define hmaxim  hmaxim_
00111 # define hminim  hminim_
00112 # define hdelet  hdelet_
00113 # define hntvar2 hntvar2_
00114 # define hntvar3 hntvar3_
00115 # define hbname  hbname_
00116 # define hbnamc  hbnamc_
00117 # define hbnam   hbnam_
00118 # define hi      hi_
00119 # define hie     hie_
00120 # define hif     hif_
00121 # define hij     hij_
00122 # define hix     hix_
00123 # define hijxy   hijxy_
00124 # define hije    hije_
00125 # define hcdir   hcdir_
00126 
00127 # define type_of_call
00128 # define DEFCHAR  const char*
00129 # define PASSCHAR(string) string
00130 #else
00131 # define hlimit  HLIMIT
00132 # define hldir   HLDIR
00133 # define hropen  HROPEN
00134 # define hrend   HREND
00135 # define hrin    HRIN
00136 # define hnoent  HNOENT
00137 # define hgive   HGIVE
00138 # define hgiven  HGIVEN
00139 # define hgnpar  HGNPAR
00140 # define hgnf    HGNF
00141 # define hgnt    HGNT
00142 # define hgntf   HGNTF
00143 # define hgnt1   HGNT1
00144 # define rzink   RZINK
00145 # define hdcofl  HDCOFL
00146 # define hmaxim  HMAXIM
00147 # define hminim  HMINIM
00148 # define hdelet  HDELET
00149 # define hntvar2 HNTVAR2
00150 # define hntvar3 HNTVAR3
00151 # define hbname  HBNAME
00152 # define hbnamc  HBNAMC
00153 # define hbnam   HBNAM
00154 # define hi      HI
00155 # define hie     HIE
00156 # define hif     HIF
00157 # define hij     HIJ
00158 # define hix     HIX
00159 # define hijxy   HIJXY
00160 # define hije    HIJE
00161 # define hcdir   HCDIR
00162 # define type_of_call  _stdcall
00163 # define DEFCHAR  const char*, const int
00164 # define PASSCHAR(string) string, strlen(string)
00165 #endif
00166 
00167 extern "C" void  type_of_call hlimit(const int&);
00168 #ifndef WIN32
00169 extern "C" void  type_of_call hropen(const int&,DEFCHAR,DEFCHAR,DEFCHAR,
00170                         const int&,const int&,const int,const int,const int);
00171 extern "C" void  type_of_call hrend(DEFCHAR,const int);
00172 #else
00173 extern "C" void  type_of_call hropen(const int&,DEFCHAR,DEFCHAR,DEFCHAR,
00174                         const int&,const int&);
00175 extern "C" void  type_of_call hrend(DEFCHAR);
00176 #endif
00177 
00178 extern "C" void  type_of_call hrin(const int&,const int&,const int&);
00179 extern "C" void  type_of_call hnoent(const int&,const int&);
00180 #ifndef WIN32
00181 extern "C" void  type_of_call hgive(const int&,DEFCHAR,const int&,const float&,const float&,
00182    const int&,const float&,const float&,const int&,const int&,const int);
00183 #else
00184 extern "C" void  type_of_call hgive(const int&,DEFCHAR,const int&,const float&,const float&,
00185    const int&,const float&,const float&,const int&,const int&);
00186 #endif
00187 
00188   //SUBROUTINE HGNT1(IDD,BLKNA1,VAR,IOFFST,NVAR,IDNEVT,IERROR)
00189 #ifndef WIN32
00190 extern "C" void  type_of_call hgiven(const int&,DEFCHAR,const int&,DEFCHAR,
00191    const float&,const float&,const int,const int);
00192 extern "C" void  type_of_call hgnt1(const int&,DEFCHAR,DEFCHAR,const int&,const int&,const int&,const int&,const int,const int);
00193 #else
00194 extern "C" void  type_of_call hgiven(const int&,DEFCHAR,const int&,DEFCHAR,
00195    const float&,const float&);
00196 extern "C" void  type_of_call hgnt1(const int&,DEFCHAR,DEFCHAR,const int&,const int&,const int&,const int&);
00197 #endif
00198 
00199 #ifndef WIN32
00200 extern "C" void  type_of_call hntvar2(const int&,const int&,DEFCHAR,DEFCHAR,DEFCHAR,int&,int&,int&,int&,int&,const int,const int, const int);
00201 extern "C" void  type_of_call hntvar3(const int&,const int&,DEFCHAR, const int);
00202 #else
00203 extern "C" void  type_of_call hntvar2(const int&,const int&,DEFCHAR,DEFCHAR,DEFCHAR,int&,int&,int&,int&,int&);
00204 extern "C" void  type_of_call hntvar3(const int&,const int&,DEFCHAR);
00205 #endif
00206 
00207 #ifndef WIN32
00208 extern "C" void  type_of_call hbnam(const int&,DEFCHAR,const int&,DEFCHAR,const int&,const int, const int);
00209 #else
00210 extern "C" void  type_of_call hbnam(const int&,DEFCHAR,const int&,DEFCHAR,const int&);
00211 #endif
00212 
00213 extern "C" void  type_of_call hgnpar(const int&,const char *,const int);
00214 extern "C" void  type_of_call hgnf(const int&,const int&,const float&,const int&);
00215 extern "C" void  type_of_call hgnt(const int&,const int&,const int&);
00216 extern "C" void  type_of_call hgntf(const int&,const int&,const int&);
00217 extern "C" void  type_of_call rzink(const int&,const int&,const char *,const int);
00218 extern "C" void  type_of_call hdcofl();
00219 extern "C" void  type_of_call hmaxim(const int&,const float&);
00220 extern "C" void  type_of_call hminim(const int&,const float&);
00221 extern "C" void  type_of_call hdelet(const int&);
00222 extern "C" float type_of_call hi(const int&,const int&);
00223 extern "C" float type_of_call hie(const int&,const int&);
00224 extern "C" float type_of_call hif(const int&,const int&);
00225 extern "C" float type_of_call hij(const int&,const int&,const int&);
00226 extern "C" void  type_of_call hix(const int&,const int&,const float&);
00227 extern "C" void  type_of_call hijxy(const int&,const int&,const int&,const float&,const float&);
00228 extern "C" float type_of_call hije(const int&,const int&,const int&);
00229 #ifndef WIN32
00230 extern "C" void  type_of_call hcdir(DEFCHAR,DEFCHAR ,const int,const int);
00231 extern "C" void  type_of_call hldir(DEFCHAR,DEFCHAR ,const int,const int);
00232 #else
00233 extern "C" void  type_of_call hcdir(DEFCHAR,DEFCHAR);
00234 extern "C" void  type_of_call hldir(DEFCHAR,DEFCHAR);
00235 #endif
00236 
00237 Bool_t THbookFile::fgPawInit = kFALSE;
00238 Int_t  *THbookFile::fgLuns   = 0;
00239 R__EXTERN TTree *gTree;
00240 
00241 ClassImp(THbookFile)
00242 
00243 //______________________________________________________________________________
00244 THbookFile::THbookFile() : TNamed(),fLun(0),fLrecl(0)
00245 {
00246    //the constructor
00247    fList = new TList();
00248    fKeys = new TList();
00249 }
00250 
00251 //_____________________________________________________________________________
00252 THbookFile::THbookFile(const char *fname, Int_t lrecl)
00253            :TNamed(fname,"")
00254 {
00255 //  Constructor for an HBook file object
00256 
00257    // Initialize the Hbook/Zebra store
00258    Int_t i;
00259    if (!fgPawInit) {
00260       fgPawInit = kTRUE;
00261       lq = &pawc[9];
00262       iq = &pawc[17];
00263       void *qq = iq;
00264       q = (float*)qq;
00265       int pawc_size = PAWC_SIZE;
00266       hlimit(pawc_size);
00267       fgLuns = new Int_t[10];
00268       for (i=0;i<10;i++) fgLuns[i] = 0;
00269    }
00270 
00271    //find a free logical unit (max 10)
00272    fLun = 0;
00273    for (i=0;i<10;i++) {
00274       if (fgLuns[i] == 0) {
00275          fLun = 10+i;
00276          fgLuns[i] = 1;
00277          break;
00278       }
00279    }
00280    if (fLun == 0) {
00281       Error("THbookFile","Too many HbookFiles\n");
00282       return;
00283    }
00284    char topdir[20];
00285    snprintf(topdir,19,"lun%d",fLun);
00286 
00287    Int_t ier;
00288 #ifndef WIN32
00289    hropen(fLun,PASSCHAR(topdir),PASSCHAR(fname),PASSCHAR("p"),lrecl,ier,strlen(topdir),strlen(fname),1);
00290 #else
00291    hropen(fLun,PASSCHAR(topdir),PASSCHAR(fname),PASSCHAR("p"),lrecl,ier);
00292 #endif
00293    fLrecl = lrecl;
00294    SetTitle(topdir);
00295    snprintf(topdir,19,"//lun%d",fLun);
00296    fCurDir = topdir;
00297 
00298    if (ier) printf (" Error on hropen was %d \n", ier);
00299    if (quest[0]) {
00300       printf("Error cannot open input file: %s\n",fname);
00301    }
00302    if (ier || quest[0]) {
00303       fgLuns[fLun-10]=0; 
00304       fLun  = 0;
00305       fList = 0;
00306       fKeys = 0;
00307       MakeZombie();
00308       return;
00309    }
00310 
00311    gROOT->GetListOfBrowsables()->Add(this,fname);
00312 
00313    fList = new TList();
00314    fKeys = new TList();
00315    for (Int_t key=1;key<1000000;key++) {
00316       int z0 = 0;
00317       rzink(key,z0,"S",1);
00318       if (quest[0]) break;
00319       if (quest[13] & 8) continue;
00320       Int_t id = quest[20];
00321       THbookKey *akey = new THbookKey(id,this);
00322       fKeys->Add(akey);
00323    }
00324 }
00325 
00326 //______________________________________________________________________________
00327 THbookFile::~THbookFile()
00328 {
00329    //destructor
00330    if (!fList) return;
00331    Close();
00332    delete fList;
00333    delete fKeys;
00334 }
00335 
00336 //______________________________________________________________________________
00337 void THbookFile::Browse(TBrowser *b)
00338 {
00339 // to be implemented
00340 
00341    if( b ) {
00342       b->Add(fList, "memory");
00343       b->Add(fKeys, "IDs on disk");
00344    }
00345    cd();
00346 }
00347 
00348 //______________________________________________________________________________
00349 Bool_t THbookFile::cd(const char *dirname)
00350 {
00351 // change directory to dirname
00352 
00353    Int_t nch = strlen(dirname);
00354    if (nch == 0) {
00355 #ifndef WIN32
00356       hcdir(PASSCHAR(fCurDir.Data()),PASSCHAR(" "),fCurDir.Length(),1);
00357 #else
00358       hcdir(PASSCHAR(fCurDir.Data()),PASSCHAR(" "));
00359 #endif
00360       return kTRUE;
00361    }
00362 
00363    char cdir[512];
00364    Int_t i;
00365    for (i=0;i<512;i++) cdir[i] = ' ';
00366    cdir[511] = 0;
00367 #ifndef WIN32
00368    hcdir(PASSCHAR(dirname),PASSCHAR(" "),nch,1);
00369    hcdir(PASSCHAR(cdir),PASSCHAR("R"),511,1);
00370 #else
00371    hcdir(PASSCHAR(dirname),PASSCHAR(" "));
00372    hcdir(PASSCHAR(cdir),PASSCHAR("R"));
00373 #endif
00374    for (i=510;i>=0;i--) {
00375       if (cdir[i] != ' ') break;
00376       cdir[i] = 0;
00377    }
00378    fCurDir = cdir;
00379    printf("fCurdir=%s\n",fCurDir.Data());
00380 
00381    return kTRUE;
00382 }
00383 
00384 //______________________________________________________________________________
00385 void THbookFile::Close(Option_t *)
00386 {
00387 // Close the Hbook file
00388 
00389    if(!IsOpen()) return;
00390    if (!fList) return;
00391    
00392    gROOT->GetListOfBrowsables()->Remove(this);
00393 
00394    cd();
00395 
00396    fList->Delete();
00397    fKeys->Delete();
00398    if (fgLuns) fgLuns[fLun-10] = 0;
00399    hdelet(0);
00400 #ifndef WIN32
00401    hrend(PASSCHAR(GetTitle()),strlen(GetTitle()));
00402 #else
00403    hrend(PASSCHAR(GetTitle()));
00404 #endif
00405 }
00406 
00407 //______________________________________________________________________________
00408 void THbookFile::DeleteID(Int_t id)
00409 {
00410    //remove id from file and memory
00411    hdelet(id);
00412 }
00413 
00414 //______________________________________________________________________________
00415 TObject *THbookFile::FindObject(const char *name) const
00416 {
00417 // return object with name in fList in memory
00418    return fList->FindObject(name);
00419 }
00420 
00421 //______________________________________________________________________________
00422 TObject *THbookFile::FindObject(const TObject *obj) const
00423 {
00424 // return object with pointer obj in fList in memory
00425    return fList->FindObject(obj);
00426 }
00427 
00428 //______________________________________________________________________________
00429 TObject *THbookFile::Get(Int_t idd)
00430 {
00431 // import Hbook object with identifier idd in memory
00432 
00433    Int_t id = 0;
00434    for (Int_t key=1;key<1000000;key++) {
00435       int z0 = 0;
00436       rzink(key,z0,"S",1);
00437       if (quest[0]) break;
00438       if (quest[13] & 8)  continue;
00439       id = quest[20];
00440       if (id == idd) break;
00441    }
00442    if (id == 0) return 0;
00443    if (id != idd) {
00444       printf("Error cannot find ID = %d\n",idd);
00445       return 0;
00446    }
00447 
00448    int i999 = 999;
00449    // must delete any previous object with the same ID !!
00450    lcdir = hcbook[6];
00451    ltab  = hcbook[9];
00452    for (Int_t i=1;i<=iq[lcdir+kNRH];i++) {
00453       if (iq[ltab+i] == id) {
00454          printf("WARNING, previous ID=%d is replaced\n",id);
00455          hdelet(id);
00456          break;
00457       }
00458    }
00459    hrin(id,i999,0);
00460    if (quest[0]) {
00461       printf("Error cannot read ID = %d\n",id);
00462       return 0;
00463    }
00464    hdcofl();
00465    lcid  = hcbook[10];
00466    lcont = lq[lcid-1];
00467    TObject *obj = 0;
00468    if (hcbits[3]) {
00469       if (iq[lcid-2] == 2) obj = ConvertRWN(id);
00470       else                 obj = ConvertCWN(id);
00471       //hdelet(id); //cannot be deleted here since used in GetEntry
00472       if (obj) {
00473          fList->Add(obj);
00474          ((THbookTree *)obj)->SetTitle(GetName());
00475       }
00476       return obj;
00477    }
00478    if (hcbits[0] && hcbits[7]) {
00479       obj = ConvertProfile(id);
00480       hdelet(id);
00481       if (obj) fList->Add(obj);
00482       return obj;
00483    }
00484    if (hcbits[0]) {
00485       obj = Convert1D(id);
00486       hdelet(id);
00487       if (obj) fList->Add(obj);
00488       return obj;
00489    }
00490    if (hcbits[1] || hcbits[2]) {
00491       obj = Convert2D(id);
00492       hdelet(id);
00493       if (obj) fList->Add(obj);
00494       return obj;
00495    }
00496    return obj;
00497 }
00498 
00499 
00500 //______________________________________________________________________________
00501 Int_t THbookFile::GetEntry(Int_t entry, Int_t id, Int_t atype, Float_t *x)
00502 {
00503 // Read in memory all columns of entry number of ntuple id from the Hbook file
00504 
00505    Int_t ier = 0;
00506    if (atype == 0) {
00507       hgnf(id,entry+1,x[0],ier);
00508    } else {
00509       hgnt(id,entry+1,ier);
00510    }
00511    return 0;
00512 }
00513 
00514 //______________________________________________________________________________
00515 Int_t THbookFile::GetEntryBranch(Int_t entry, Int_t id)
00516 {
00517 // Read in memory only the branch bname
00518 
00519    if (entry == gLastEntry) return 0;
00520    gLastEntry = entry;
00521    Int_t ier = 0;
00522    //uses the fast read method using the Hbook tables computed in InitLeaves
00523    hgntf(id,entry+1,ier);
00524    //old alternative slow method
00525 //#ifndef WIN32
00526 //   hgnt1(id,PASSCHAR(blockname),PASSCHAR(branchname),0,-1,entry+1,ier,strlen(blockname),strlen(branchname));
00527 //#else
00528 //   hgnt1(id,PASSCHAR(blockname),PASSCHAR(branchname),0,-1,entry+1,ier);
00529 //#endif
00530    return 0;
00531 }
00532 
00533 
00534 //______________________________________________________________________________
00535 void THbookFile::InitLeaves(Int_t id, Int_t var, TTreeFormula *formula)
00536 {
00537 // This function is called from the first entry in TTreePlayer::InitLoop
00538 // It analyzes the list of variables involved in the current query
00539 // and pre-process the internal Hbook tables to speed-up the search
00540 // at the next entries.
00541 
00542    if (!formula) return;
00543    Int_t ncodes = formula->GetNcodes();
00544    for (Int_t i=1;i<=ncodes;i++) {
00545       TLeaf *leaf = formula->GetLeaf(i-1);
00546       if (!leaf) continue;
00547       if (var == 5) {
00548          //leafcount may be null in case of a fix size array
00549          if (leaf->GetLeafCount()) leaf = leaf->GetLeafCount();
00550       }
00551       Int_t last = 0;
00552       if (var == 1 && i == ncodes) last = 1;
00553 #ifndef WIN32
00554       hntvar3(id,last,PASSCHAR(leaf->GetName()),strlen(leaf->GetName()));
00555 #else
00556       hntvar3(id,last,PASSCHAR(leaf->GetName()));
00557 #endif
00558    }
00559 }
00560 
00561 //______________________________________________________________________________
00562 Bool_t THbookFile::IsOpen() const
00563 {
00564    // Returns kTRUE in case file is open and kFALSE if file is not open.
00565 
00566    return fLun == 0 ? kFALSE : kTRUE;
00567 }
00568 
00569 
00570 //______________________________________________________________________________
00571 void THbookFile::SetBranchAddress(Int_t id, const char *bname, void *add)
00572 {
00573    //Set branch address
00574    Int_t *iadd = (Int_t*)add;
00575    Int_t &aadd = *iadd;
00576 #ifndef WIN32
00577    hbnam(id,PASSCHAR(bname),aadd,PASSCHAR("$SET"),0,strlen(bname),4);
00578 #else
00579    hbnam(id,PASSCHAR(bname),aadd,PASSCHAR("$SET"),0);
00580 #endif
00581 }
00582 
00583 //______________________________________________________________________________
00584 TFile *THbookFile::Convert2root(const char *rootname, Int_t /*lrecl*/,
00585                                 Option_t *option)
00586 {
00587 // Convert this Hbook file to a Root file with name rootname.
00588 // if rootname="', rootname = hbook file name with .root instead of .hbook
00589 // By default, the Root file is connected and returned
00590 // option:
00591 //       - "NO" do not connect the Root file
00592 //       - "C"  do not compress file (default is to compress)
00593 //       - "L"  do not convert names to lower case (default is to convert)
00594 
00595    TString opt = option;
00596    opt.ToLower();
00597 
00598    Int_t nch = strlen(rootname);
00599    char *rfile=0;
00600    if (nch) {
00601       rfile = new char[nch+1];
00602       strlcpy(rfile,rootname,nch+1);
00603    } else {
00604       nch = strlen(GetName());
00605       rfile = new char[nch+1];
00606       strlcpy(rfile,GetName(),nch+1);
00607       char *dot = strrchr(rfile,'.');
00608       if (dot) strcpy(dot+1,"root");
00609       else     strlcat(rfile,".root",nch+1);
00610    }
00611 
00612    nch = 2*nch+50;
00613    char *cmd = new char[nch+1];
00614    snprintf(cmd,nch,"h2root %s %s",GetName(),rfile);
00615    if (opt.Contains("c")) strlcat (cmd," 0",nch+1);
00616    if (opt.Contains("l")) strlcat (cmd," 0",nch+1);
00617 
00618    gSystem->Exec(cmd);
00619    
00620    delete [] cmd;
00621    if (opt.Contains("no")) {delete [] rfile; return 0;}
00622    TFile *f = new TFile(rfile);
00623    delete [] rfile;
00624    if (f->IsZombie()) {delete f; f = 0;}
00625    return f;
00626 }
00627 
00628 
00629 //______________________________________________________________________________
00630 TObject *THbookFile::ConvertCWN(Int_t id)
00631 {
00632 // Convert the Column-Wise-Ntuple id to a Root Tree
00633 
00634    const int nchar=9;
00635    int nvar;
00636    int i,j;
00637    int nsub,itype,isize,ielem;
00638    char *chtag_out;
00639    float rmin[1000], rmax[1000];
00640 
00641    if (id > 0) snprintf(idname,127,"h%d",id);
00642    else        snprintf(idname,127,"h_%d",-id);
00643    hnoent(id,nentries);
00644    //printf(" Converting CWN with ID= %d, nentries = %d\n",id,nentries);
00645    nvar=0;
00646 #ifndef WIN32
00647    hgiven(id,chtitl,nvar,PASSCHAR(""),rmin[0],rmax[0],80,0);
00648 #else
00649    hgiven(id,chtitl,80,nvar,PASSCHAR(""),rmin[0],rmax[0]);
00650 #endif
00651    chtag_out = new char[nvar*nchar+1];
00652    Int_t *charflag = new Int_t[nvar];
00653    Int_t *lenchar  = new Int_t[nvar];
00654    Int_t *boolflag = new Int_t[nvar];
00655    Int_t *lenbool  = new Int_t[nvar];
00656    UChar_t *boolarr = new UChar_t[10000];
00657 
00658    chtag_out[nvar*nchar]=0;
00659    for (i=0;i<80;i++)chtitl[i]=0;
00660 #ifndef WIN32
00661    hgiven(id,chtitl,nvar,chtag_out,rmin[0],rmax[0],80,nchar);
00662 #else
00663    hgiven(id,chtitl,80,nvar,chtag_out,nchar,rmin[0],rmax[0]);
00664 #endif
00665 
00666    Int_t bufpos = 0;
00667    Int_t isachar = 0;
00668    Int_t isabool = 0;
00669    char fullname[64];
00670    char name[32];
00671    char block[32];
00672    char oldblock[32];
00673    strlcpy(oldblock,"OLDBLOCK",32); 
00674    Int_t oldischar = -1;
00675    for (i=80;i>0;i--) {if (chtitl[i] == ' ') chtitl[i] = 0; }
00676    THbookTree *tree = new THbookTree(idname,id);
00677    tree->SetHbookFile(this);
00678    tree->SetType(1);
00679 
00680    char *bigbuf = tree->MakeX(500000);
00681 
00682    gTree = tree;
00683 #ifndef WIN32
00684    hbnam(id,PASSCHAR(" "),bigbuf[0],PASSCHAR("$CLEAR"),0,1,6);
00685 #else
00686    hbnam(id,PASSCHAR(" "),bigbuf[0],PASSCHAR("$CLEAR"),0);
00687 #endif
00688 
00689    UInt_t varNumber = 0;
00690    Int_t golower  = 1;
00691    Int_t nbits = 0;
00692    for(i=0; i<nvar;i++) {
00693       memset(name,' ',sizeof(name));
00694       name[sizeof(name)-1] = 0;
00695       memset(block,' ',sizeof(block));
00696       block[sizeof(block)-1] = 0;
00697       memset(fullname,' ',sizeof(fullname));
00698       fullname[sizeof(fullname)-1]=0;
00699 #ifndef WIN32
00700       hntvar2(id,i+1,PASSCHAR(name),PASSCHAR(fullname),PASSCHAR(block),nsub,itype,isize,nbits,ielem,32,64,32);
00701 #else
00702       hntvar2(id,i+1,PASSCHAR(name),PASSCHAR(fullname),PASSCHAR(block),nsub,itype,isize,nbits,ielem);
00703 #endif
00704       TString hbookName = name;
00705 
00706       for (j=30;j>0;j--) {
00707          if(golower) name[j] = tolower(name[j]);
00708          if (name[j] == ' ') name[j] = 0;
00709       }
00710       if (golower == 2) name[0] = tolower(name[0]);
00711 
00712       for (j=62;j>0;j--) {
00713          if(golower && fullname[j-1] != '[') fullname[j] = tolower(fullname[j]);
00714          // convert also character after [, if golower == 2
00715          if (golower == 2) fullname[j] = tolower(fullname[j]);
00716          if (fullname[j] == ' ') fullname[j] = 0;
00717       }
00718       // convert also first character, if golower == 2
00719       if (golower == 2) fullname[0] = tolower(fullname[0]);
00720       for (j=30;j>0;j--) {
00721          if (block[j] == ' ') block[j] = 0;
00722          else break;
00723       }
00724       if (itype == 1 && isize == 4) strlcat(fullname,"/F",64);
00725       if (itype == 1 && isize == 8) strlcat(fullname,"/D",64);
00726       if (itype == 2) strlcat(fullname,"/I",64);
00727       if (itype == 3) strlcat(fullname,"/i",64);
00728 //     if (itype == 4) strlcat(fullname,"/i",64);
00729       if (itype == 4) strlcat(fullname,"/b",64);
00730       if (itype == 5) strlcat(fullname,"/C",64);
00731 //printf("Creating branch:%s, block:%s, fullname:%s, nsub=%d, itype=%d, isize=%d, ielem=%d, bufpos=%d\n",name,block,fullname,nsub,itype,isize,ielem,bufpos);
00732       Int_t ischar;
00733       if (itype == 5) ischar = 1;
00734       else            ischar = 0;
00735 
00736       if (ischar != oldischar || strcmp(oldblock,block) != 0) {
00737          varNumber = 0;
00738          strlcpy(oldblock,block,32); 
00739          oldischar = ischar;
00740          Long_t add= (Long_t)&bigbuf[bufpos];
00741          Int_t lblock   = strlen(block);
00742 #ifndef WIN32
00743          hbnam(id,PASSCHAR(block),add,PASSCHAR("$SET"),ischar,lblock,4);
00744 #else
00745          hbnam(id,PASSCHAR(block),add,PASSCHAR("$SET"),ischar);
00746 #endif 
00747 
00748       }
00749 
00750       Int_t bufsize = 8000;
00751       THbookBranch *branch = new THbookBranch(tree,name,(void*)&bigbuf[bufpos],fullname,bufsize);
00752       tree->GetListOfBranches()->Add(branch);
00753       branch->SetBlockName(block);
00754       branch->SetUniqueID(varNumber);
00755       varNumber++;
00756 
00757       //NB: the information about isachar should be saved in the branch
00758       // to be done
00759       boolflag[i] = -10;
00760       charflag[i] = 0;
00761       if (itype == 4) {isabool++; boolflag[i] = bufpos; lenbool[i] = ielem;}
00762       bufpos += isize*ielem;
00763       if (ischar) {isachar++; charflag[i] = bufpos-1; lenchar[i] = isize*ielem;}
00764       TObjArray *ll= branch->GetListOfLeaves();
00765       TLeaf *leaf = (TLeaf*)ll->UncheckedAt(0);
00766       if (!leaf) continue;
00767       TLeafI *leafcount = (TLeafI*)leaf->GetLeafCount();
00768       if (leafcount) {
00769          if (leafcount->GetMaximum() <= 0) leafcount->SetMaximum(ielem);
00770       }
00771    }
00772    tree->SetEntries(nentries);
00773    delete [] charflag;
00774    delete [] lenchar;
00775    delete [] boolflag;
00776    delete [] lenbool;
00777    delete [] boolarr;
00778    delete [] chtag_out;
00779 
00780    return tree;
00781 }
00782 
00783 //______________________________________________________________________________
00784 TObject *THbookFile::ConvertRWN(Int_t id)
00785 {
00786 // Convert the Row-Wise-Ntuple id to a Root Tree
00787 
00788    const int nchar=9;
00789    int nvar;
00790    int i,j;
00791    char *chtag_out;
00792    float rmin[1000], rmax[1000];
00793 
00794    if (id > 0) snprintf(idname,127,"h%d",id);
00795    else        snprintf(idname,127,"h_%d",-id);
00796    hnoent(id,nentries);
00797    //printf(" Converting RWN with ID= %d, nentries = %d\n",id,nentries);
00798    nvar=0;
00799 #ifndef WIN32
00800    hgiven(id,chtitl,nvar,PASSCHAR(""),rmin[0],rmax[0],80,0);
00801 #else
00802    hgiven(id,chtitl,80,nvar,PASSCHAR(""),rmin[0],rmax[0]);
00803 #endif
00804 
00805    chtag_out = new char[nvar*nchar+1];
00806 
00807    Int_t golower  = 1;
00808    chtag_out[nvar*nchar]=0;
00809    for (i=0;i<80;i++)chtitl[i]=0;
00810 #ifndef WIN32
00811    hgiven(id,chtitl,nvar,chtag_out,rmin[0],rmax[0],80,nchar);
00812 #else
00813    hgiven(id,chtitl,80,nvar,chtag_out,nchar,rmin[0],rmax[0]);
00814 #endif
00815    hgnpar(id,"?",1);
00816    char *name = chtag_out;
00817    for (i=80;i>0;i--) {if (chtitl[i] == ' ') chtitl[i] = 0; }
00818    THbookTree *tree = new THbookTree(idname,id);
00819    tree->SetHbookFile(this);
00820    tree->SetType(0);
00821    gTree = tree;
00822    Float_t *x = (Float_t*)tree->MakeX(nvar*4);
00823 
00824    Int_t first,last;
00825    for(i=0; i<nvar;i++) {
00826       name[nchar-1] = 0;
00827       first = last = 0;
00828       TString hbookName = name;
00829       // suppress trailing blanks
00830       for (j=nchar-2;j>0;j--) {
00831          if(golower) name[j] = tolower(name[j]);
00832          if (name[j] == ' ' && last == 0) name[j] = 0;
00833          else last = j;
00834       }
00835       if (golower == 2) name[0] = tolower(name[0]);
00836 
00837       // suppress heading blanks
00838       for (j=0;j<nchar;j++) {
00839          if (name[j] != ' ') break;
00840          first = j+1;
00841       }
00842       Int_t bufsize = 8000;
00843       //tree->Branch(&name[first],&x[i],&name[first],bufsize);
00844       THbookBranch *branch = new THbookBranch(tree,&name[first],&x[4*i],&name[first],bufsize);
00845       branch->SetAddress(&x[i]);
00846       branch->SetBlockName(hbookName.Data());
00847       tree->GetListOfBranches()->Add(branch);
00848       name += nchar;
00849    }
00850    tree->SetEntries(nentries);
00851    return tree;
00852 }
00853 
00854 //______________________________________________________________________________
00855 TObject *THbookFile::ConvertProfile(Int_t id)
00856 {
00857 // Convert an Hbook profile histogram into a Root TProfile
00858 //
00859 // the following structure is used in Hbook
00860 //    lcid points to the profile in array iq
00861 //    lcont = lq(lcid-1)
00862 //    lw    = lq(lcont)
00863 //    ln    = lq(lw)
00864 //      if option S jbyt(iq(lw),1,2) = 1
00865 //      if option I jbyt(iq(lw),1,2) = 2
00866 
00867    if (id > 0) snprintf(idname,127,"h%d",id);
00868    else        snprintf(idname,127,"h_%d",-id);
00869    hnoent(id,nentries);
00870    Int_t lw = lq[lcont];
00871    Int_t ln = lq[lw];
00872 #ifndef WIN32
00873    hgive(id,chtitl,ncx,xmin,xmax,ncy,ymin,ymax,nwt,idb,80);
00874 #else
00875    hgive(id,chtitl,80,ncx,xmin,xmax,ncy,ymin,ymax,nwt,idb);
00876 #endif
00877    Float_t offsetx = 0.5*(xmax-xmin)/ncx;
00878    chtitl[4*nwt] = 0;
00879    const char *option= " ";
00880    if (iq[lw] == 1) option = "S";
00881    if (iq[lw] == 2) option = "I";
00882    TProfile *p = new TProfile(idname,chtitl,ncx,xmin,xmax,ymin,ymax,option);
00883 
00884    const Int_t kCON1 = 9;
00885    Int_t i;
00886    Float_t x;
00887    Float_t y = 0.5*(ymin+ymax);
00888    for (i=1;i<=ncx;i++) {
00889       Int_t n = Int_t(q[ln+i]);
00890       hix(id,i,x);
00891       for (Int_t j=0;j<n;j++) {
00892          p->Fill(x+offsetx,y);
00893       }
00894       Float_t content = q[lcont+kCON1+i];
00895       Float_t error   = TMath::Sqrt(q[lw+i]);
00896       p->SetBinContent(i,content);
00897       p->SetBinError(i,error);
00898    }
00899    p->SetEntries(nentries);
00900    return p;
00901 }
00902 
00903 //______________________________________________________________________________
00904 TObject *THbookFile::Convert1D(Int_t id)
00905 {
00906 // Convert an Hbook 1-d histogram into a Root TH1F
00907 
00908    if (id > 0) snprintf(idname,127,"h%d",id);
00909    else        snprintf(idname,127,"h_%d",-id);
00910    hnoent(id,nentries);
00911 #ifndef WIN32
00912    hgive(id,chtitl,ncx,xmin,xmax,ncy,ymin,ymax,nwt,idb,80);
00913 #else
00914    hgive(id,chtitl,80,ncx,xmin,xmax,ncy,ymin,ymax,nwt,idb);
00915 #endif
00916    chtitl[4*nwt] = 0;
00917    TH1F *h1;
00918    Int_t i;
00919    if (hcbits[5]) {
00920       Int_t lbins = lq[lcid-2];
00921       Double_t *xbins = new Double_t[ncx+1];
00922       for (i=0;i<=ncx;i++) xbins[i] = q[lbins+i+1];
00923       h1 = new TH1F(idname,chtitl,ncx,xbins);
00924       delete [] xbins;
00925    } else {
00926       h1 = new TH1F(idname,chtitl,ncx,xmin,xmax);
00927    }
00928    if (hcbits[8]) h1->Sumw2();
00929    TGraph *gr = 0;
00930    if (hcbits[11]) {
00931       gr = new TGraph(ncx);
00932       h1->GetListOfFunctions()->Add(gr);
00933    }
00934 
00935    Float_t x;
00936    for (i=0;i<=ncx+1;i++) {
00937       x = h1->GetBinCenter(i);
00938       h1->Fill(x,hi(id,i));
00939       if (hcbits[8]) h1->SetBinError(i,hie(id,i));
00940       if (gr && i>0 && i<=ncx) gr->SetPoint(i,x,hif(id,i));
00941    }
00942    Float_t yymin, yymax;
00943    if (hcbits[19]) {
00944       yymax = q[lcid+kMAX1];
00945       h1->SetMaximum(yymax);
00946    }
00947    if (hcbits[20]) {
00948       yymin = q[lcid+kMIN1];
00949       h1->SetMinimum(yymin);
00950    }
00951    h1->SetEntries(nentries);
00952    return h1;
00953 }
00954 
00955 //______________________________________________________________________________
00956 TObject *THbookFile::Convert2D(Int_t id)
00957 {
00958 // Convert an Hbook 2-d histogram into a Root TH2F
00959 
00960    if (id > 0) snprintf(idname,127,"h%d",id);
00961    else        snprintf(idname,127,"h_%d",-id);
00962    hnoent(id,nentries);
00963 #ifndef WIN32
00964    hgive(id,chtitl,ncx,xmin,xmax,ncy,ymin,ymax,nwt,idb,80);
00965 #else
00966    hgive(id,chtitl,80,ncx,xmin,xmax,ncy,ymin,ymax,nwt,idb);
00967 #endif
00968    chtitl[4*nwt] = 0;
00969    TH2F *h2 = new TH2F(idname,chtitl,ncx,xmin,xmax,ncy,ymin,ymax);
00970    Float_t offsetx = 0.5*(xmax-xmin)/ncx;
00971    Float_t offsety = 0.5*(ymax-ymin)/ncy;
00972    Int_t lw = lq[lcont];
00973    if (lw) h2->Sumw2();
00974 
00975    Float_t x,y;
00976    for (Int_t j=0;j<=ncy+1;j++) {
00977       for (Int_t i=0;i<=ncx+1;i++) {
00978          hijxy(id,i,j,x,y);
00979          h2->Fill(x+offsetx,y+offsety,hij(id,i,j));
00980          if (lw) {
00981             Double_t err2 = hije(id,i,j);
00982             h2->SetCellError(i,j,err2);
00983          }
00984       }
00985    }
00986    h2->SetEntries(nentries);
00987    return h2;
00988 }
00989 
00990 //______________________________________________________________________________
00991 void THbookFile::ls(const char *path) const
00992 {
00993 // List contents of Hbook directory
00994 
00995    Int_t nch = strlen(path);
00996    if (nch == 0) {
00997 #ifndef WIN32
00998       hldir(PASSCHAR(fCurDir.Data()),PASSCHAR("T"),fCurDir.Length(),1);
00999 #else
01000       hldir(PASSCHAR(fCurDir.Data()),PASSCHAR("T"));
01001 #endif
01002       return;
01003    }
01004 
01005 #ifndef WIN32
01006    hldir(PASSCHAR(path),PASSCHAR("T"),strlen(path),1);
01007 #else
01008    hldir(PASSCHAR(path),PASSCHAR("T"));
01009 #endif
01010 }

Generated on Tue Jul 5 14:22:46 2011 for ROOT_528-00b_version by  doxygen 1.5.1