Re: Converting histograms ROOT->PAW

From: Rene Brun (Rene.Brun@cern.ch)
Date: Wed Sep 15 1999 - 08:36:12 MEST


Hi Stephan,
I have written a small Root macro (topaw.C) to convert a Root file to
PAW.
In fact the macro converts the Root file into a PAW kumac.

The macro is in the attachment. See comments inside.
This macro could be extended to cover more cases, including ntuples.
I have no time to upgrade this macro for the time being.

Rene Brun

Stephan Hurling wrote:
> 
> Hello everybody,
> 
> I would like to use a program based on minuit, which would allow me to
> make a complex fit to my data. This fit cannot be reprogrammed in Root
> without spending a lot of time on this project. Unfortunately the
> interface of this programm works only with histograms in PAW format. So
> I'm searching for a way to convert just a single normal TH1F histogram
> into its PAW equivalent.
> 
> Can anybody discribe me, what I have to do, to put my TH1F histogram into
> PAW format? (Because I think it will be faster to go this way then
> rewriting the fit program in C++).
> 
> (I'm using Root from the beginning on and therefore I'm not very familiar
> with the HBOOK data format. In earlier letters of the Root Talk Digest,
> Rene said, one doesn't need to know the internals of the data formats to
> make this conversion. So it's no problem for me to develop code, if it is
> possible to make this running successfully within 2 days or so.)
> 
> Thanks and all the best,
> 
> Stephan
> 
> +-------------------------------------------------------------------------+
> |                                                                         |
> |                                                                         |
> |    Stephan Hurling                                                      |
> |                                                                         |
> |    DESY                                  Telephone: **49 40 8998 3461   |
> |    Group FH1, Build. 1b/343              FAX      : **49 40 8998 4385   |
> |    Notkestrasse 85                                                      |
> |                                                                         |
> |    D-22607 Hamburg (Germany)             e-mail :hurling@mail.desy.de   |
> |                                                                         |
> |                                                                         |
> +-------------------------------------------------------------------------+
>

FILE *fk;
static Int_t ID=0;
char aline[100];

void topaw(const char *rootFile, const char *ckumac="")
{
   // convert histograms in rootFile into a PAW macro ckumac
   // only histograms are converted
   // if ckumac is not specified assume rootFile.kumac
   // hbookFile name is rootFile with .hbook instead of .root
   // author Rene Brun
   //
   // Example
   // Root > .x topaw.C("hsimple.root")
   // will convert the Root file hsimple.root into a PAW kumac hsimple.kumac
   // To create the Hbook/Paw file, run the following PAW session
   // Paw > exec hsimple
         
   TFile f(rootFile);
   if (f.IsZombie()) {
      printf("Error invalid Rootfile name: %s\n",rootFile);
      return;
   }
   // build Hbook file name
   char hbookFile[100];
   strcpy(hbookFile,rootFile);
   char *dot = strstr(hbookFile,".");
   if (dot) strcpy(dot+1,"hbook");
   else     strcat(hbookFile,".hbook");
   // build kumac file name
   char kumac[100];
   if (strlen(ckumac) > 0) {
      strcpy(kumac,ckumac);
   } else {
      Int_t nch = strlen(rootFile);
      strcpy(kumac,rootFile);
      dot = strstr(kumac,".");
      if (dot) strcpy(dot+1,"kumac");
      else     strcat(kumac,".kumac");
   }
   fk = fopen(kumac,"w");
   if (!fk) {
      printf("Cannot open macro file:%s\n",kumac);
      return;
   }
   printf("Converting Root file: %s to PAW macro: %s\n",rootFile,kumac);
   fprintf(fk,"****************************************************\n");
   fprintf(fk,"* kumac generated by Root macro topaw.C\n");
   fprintf(fk,"* converting Root file: %s\n",f.GetName());
   fprintf(fk,"****************************************************\n");
   fprintf(fk,"*\n");
   fprintf(fk,"* Opening output Hbook file\n");
   fprintf(fk,"histo/file 22  %s ! N\n",hbookFile);
         
   // loop on all keys in the file
   TIter next(f.GetListOfKeys());
   TKey *key;
   TH1 *h;
   const char *className;
   while ((key = (TKey*)next())) {
      className = key->GetClassName();
      TClass *cl = gROOT->GetClass(className);
      if (!cl) continue;
      if (!cl->InheritsFrom("TH1")) continue;  
      h = (TH1*)key->ReadObj();
      ID++;

      printf("Converting ID= %d, h=%s, title=%s\n",ID,h->GetName(),h->GetTitle());
      fprintf(fk,"*\n");
      fprintf(fk,"*-------------------------------------\n");

      if (h->InheritsFrom("TProfile")) {
         ConvertProfile((TProfile*)h);
         delete h;
         continue;
      }               
      if (h->InheritsFrom("TH2")) {
         Convert2D(h);
         delete h;
         continue;
      }         
      Convert1D(h);
      delete h;
   }
   fprintf(fk,"close 22\n");
   fclose(fk);
}
void Convert1D(TH1 *h)
{
// Convert a Root 1-d histogram (bin contents and errors)
   TAxis *xaxis = h->GetXaxis();
   Int_t nbinsx = xaxis->GetNbins();
   fprintf(fk,"1d %d '%s' %d %g %g\n",ID,h->GetTitle(),
             nbinsx,xaxis->GetXmin(),xaxis->GetXmax());
   sprintf(aline,"Vector/Create bins(%d) R ",nbinsx);
   // convert bin contents
   Int_t bin,k;
   for (bin=1;bin<=nbinsx;bin++) {
      k = strlen(aline);
      sprintf(aline+k," %g",h->GetBinContent(bin));
      if (k > 65) {
         fprintf(fk,"%s",aline);
         if (bin < nbinsx) fprintf(fk," _\n");
         aline[0] = 0;
      }
   }
   k = strlen(aline);
   if (k > 0) fprintf(fk,"%s\n",aline);
   fprintf(fk,"Put/Content %d bins\n",ID);
   fprintf(fk,"Vector/Delete bins\n");
   // convert bin errors (if any)
   if (h->GetSumw2N()) {
      for (bin=1;bin<=nbinsx;bin++) {
         k = strlen(aline);
         sprintf(aline+k," %g",h->GetBinError(bin));
         if (k > 65) {
            fprintf(fk,"%s",aline);
            if (bin < nbinsx) fprintf(fk," _\n");
            aline[0] = 0;
         }
      }
      k = strlen(aline);
      if (k > 0) fprintf(fk,"%s\n",aline);
      fprintf(fk,"Put/Errors %d errors\n",ID);
      fprintf(fk,"Vector/Delete errors\n");
   }
   fprintf(fk,"hrout %d\n",ID);
}

void Convert2D(TH1 *h)
{
// convert a Root 2-d histogram (bins only)
   TAxis *xaxis = h->GetXaxis();
   TAxis *yaxis = h->GetYaxis();
   Int_t nbinsx = xaxis->GetNbins();
   Int_t nbinsy = yaxis->GetNbins();
   fprintf(fk,"2d %d '%s' %d %g %g %d %g %g\n",ID,h->GetTitle(),
          nbinsx,xaxis->GetXmin(),xaxis->GetXmax(),
          nbinsy,yaxis->GetXmin(),yaxis->GetXmax());
   sprintf(aline,"Vector/Create bins(%d) R ",nbinsx*nbinsy);
   Int_t binx,biny,k;
   for (biny=1;biny<=nbinsy;biny++) {
      for (binx=1;binx<=nbinsx;binx++) {
         k = strlen(aline);
         sprintf(aline+k," %g",h->GetCellContent(binx,biny));
         if (k > 65) {
            fprintf(fk,"%s",aline);
            if (bin < nbinsx) fprintf(fk," _\n");
            aline[0] = 0;
         }
      }
   }
   k = strlen(aline);
   if (k > 0) fprintf(fk,"%s\n",aline);
   fprintf(fk,"Put/Content %d bins\n",ID);
   fprintf(fk,"Vector/Delete bins\n");
   fprintf(fk,"hrout %d\n",ID);
}

void ConvertProfile(TProfile *h)
{
// Convert a Root Profile histogram (to be implemented)
   TAxis *xaxis = h->GetXaxis();
   Int_t nbinsx = xaxis->GetNbins();
   fprintf(fk,"Profile %d '%s' %d %g %g ! !\n",ID,h->GetTitle(),
          nbinsx,xaxis->GetXmin(),xaxis->GetXmax());
   fprintf(fk,"hrout %d\n",ID);
}



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:43:39 MET