Main Page | Class Hierarchy | Compound List | File List | Compound Members | File Members

hbook.C

Go to the documentation of this file.
00001 // ******************************************************************************
00002 // *
00003 // * This file defines wrapper functions for the various HBOOK routines
00004 // * which are too complicated to be inlined.
00005 // *
00006 // * The routines are ordered according to the
00007 // *  HBOOK reference manual, version 4.24, CERN library long writeup Y250
00008 // *
00009 // * This file also defines the PAWC common, of a size given by NHMEM, which is
00010 // * assumed to be defined as enum in nhmem.h
00011 // * The PAWC common is initialized in a way that ensures proper initialization
00012 // * (i.e. calling of hlimit) before it is ever used by a routine which is
00013 // * defined in a file that includes this header file
00014 // *
00015 // * Benno List
00016 // * CERN -EP/OPAL-
00017 // * CH-1211 Geneva 23
00018 // * Switzerland
00019 // * Benno.List@cern.ch
00020 // *
00021 // *
00022 // * Version 0.99, 3.11.98
00023 // * Version 0.991, 4.11.98
00024 // *
00025 // * Change V0.991, 4.11.98:
00026 // *   handling of string arrays and strings returned from FORTRAN completely new
00027 // *
00028 // ******************************************************************************
00029 #include "jbltools/kinfit/hbook.h" 
00030 #include "jbltools/kinfit/ftypes.h" 
00031 
00032 #include <string>
00033 #include <algorithm>
00034 
00035 using std::max;
00036 using std::min;
00037 
00038 // Global helper functions
00039 
00040 void charray_to_fortran (int n, 
00041                          int charraylen,
00042                          const FCharacter *charrayin,
00043                          FCharacter *& charrayout) {
00044 // take an array of strings and prepare it for transfer to a FORTRAN
00045 // subroutine, i.e. pad it with spaces                         
00046 // 
00047 // arguments:
00048 // n:          number of rows
00049 // charraylen: number of columns
00050 // charrayin:  input character array; may be, but need not be 0-terminated
00051 // charrayout: array of characters without trailing 0s, padded with spaces
00052   const FCharacter *cpin = charrayin;
00053   FCharacter *cpout = charrayout;
00054   FCharacter *cpend = charrayout + charraylen;
00055   FCharacter *cpendend = charrayout + n*charraylen;
00056   if (n > 0 && charraylen > 0) {
00057     do {
00058       while (cpout < cpend && *cpin != '\0') {
00059         *(cpout++) = *(cpin++);
00060       }
00061       while (cpout < cpend) {
00062         *(cpout++) = ' ';
00063         ++cpin;
00064       }
00065       cpend += charraylen;
00066     }
00067     while (cpout < cpendend);
00068   }
00069 }
00070 void charray_from_fortran (int n, 
00071                            int charraylen,
00072                            const FCharacter *charrayin,
00073                            FCharacter *& charrayout) {
00074 // take an array of strings and prepare it for transfer to a FORTRAN
00075 // subroutine, i.e. pad it with spaces                         
00076 // 
00077 // arguments:
00078 // n:          number of rows
00079 // charraylen: number of columns
00080 // charrayin:  input character array; may be, but need not be 0-terminated,
00081 //             padded with spaces
00082 // charrayout: array of character strings, 0-terminated, no trailing spaces
00083   if (n > 0 && charraylen > 0) {
00084     const FCharacter *cpin  = charrayin + n*charraylen - 1;
00085     FCharacter *cpout = charrayout + n*charraylen - 1;
00086     FCharacter *cpend = cpout - charraylen;
00087     do {
00088       do {
00089         *(cpout--) = '\0';
00090         --cpin;
00091       }
00092       while (cpout > cpend && (*cpin == ' ' || *cpin == '\0'));
00093       while (cpout > cpend) {
00094         *(cpout--) = *(cpin--);
00095       }
00096       cpend -= charraylen;
00097     }
00098     while (cpout > charrayout-1);
00099   }
00100 }
00101 void string_to_fortran (int n, 
00102                         unsigned int& charraylen,
00103                         const string sarrayin[],
00104                         FCharacter *& charrayout) {
00105 // take an array of strings and prepare it for transfer to a FORTRAN
00106 // subroutine, i.e. pad it with spaces
00107 //
00108 // *** NOTE: The output array is CREATED in this routine, and MUST be deleted
00109 // *** by the user afterwards with delete[]!                   
00110 // 
00111 // arguments:
00112 // n:          number of rows in sarray
00113 // charraylen: input: minimum number of columns,
00114 //             output: actual number of columns
00115 // sarrayin:   input string array
00116 // charrayout: array of characters without trailing 0s, padded with spaces
00117   charraylen  = max (charraylen, 1U);
00118   {                                          // avoid for-scope problem
00119     for (int i = 0; i < n; ++i) {
00120       charraylen  = max (charraylen, sarrayin[i].length());
00121     }
00122   }
00123   charrayout = new FCharacter [n*charraylen+1];
00124   charrayout[n*charraylen] = '\0';           // 0-terminate for debugging purposes
00125   FCharacter *cpout = charrayout;
00126   FCharacter *cpend = charrayout + charraylen;
00127   {                                          // avoid for-scope problem
00128     for (int i = 0; i < n; ++i) {
00129       sarrayin [i].copy(cpout, charraylen); // const_cast to avoid warnings
00130       {                                      // avoid for-scope problem
00131         for (FCharacter *cp=cpout + sarrayin [i].length(); cp < cpend; ++cp) {
00132           *cp = ' ';
00133         }
00134       }
00135       cpout = cpend;
00136       cpend += charraylen;
00137     }
00138   }
00139 }
00140 void string_to_fortran (unsigned int& charraylen,
00141                         const vector<string>& sarrayin,
00142                         FCharacter *& charrayout) {
00143 // take an vector of strings and prepare it for transfer to a FORTRAN
00144 // subroutine, i.e. pad it with spaces
00145 //
00146 // *** NOTE: The output array is CREATED in this routine, and MUST be deleted
00147 // *** by the user afterwards with delete[]!                   
00148 // 
00149 // arguments:
00150 // charraylen: input: minimum number of columns,
00151 //             output: actual number of columns
00152 // sarrayin:   input string vector
00153 // charrayout: array of characters without trailing 0s, padded with spaces
00154   int n = sarrayin.size();
00155   charraylen  = max (charraylen, 1U);
00156   {                                          // avoid for-scope problem
00157     for (int i = 0; i < n; ++i) {
00158       charraylen  = max (charraylen, sarrayin[i].length());
00159     }
00160   }
00161   charrayout = new FCharacter [n*charraylen+1];
00162   charrayout[n*charraylen] = '\0';           // 0-terminate for debugging purposes
00163   FCharacter *cpout = charrayout;
00164   FCharacter *cpend = charrayout + charraylen;
00165   {                                          // avoid for-scope problem
00166     for (int i = 0; i < n; ++i) {
00167       sarrayin [i].copy(cpout, charraylen); // const_cast to avoid warnings
00168       {                                      // avoid for-scope problem
00169         for (FCharacter *cp=cpout + sarrayin [i].length(); cp < cpend; ++cp) {
00170           *cp = ' ';
00171         }
00172       }
00173       cpout = cpend;
00174       cpend += charraylen;
00175     }
00176   }
00177 }
00178 void string_from_fortran (int n, 
00179                           unsigned int charraylen,
00180                           const FCharacter *charrayin,
00181                           string sarrayout[]) {
00182 // take an array of characters,padded with spaces and possibly 0-terminated,
00183 // and transfer it to an array of C++ strings
00184 // 
00185 // arguments:
00186 // n:          number of rows in charrayin, sarrayout
00187 // charraylen: input: number of columns in charrayin
00188 // charrayin:  input character array
00189 // sarrayout:  output string array
00190   FCharacter *buffer = new FCharacter [charraylen+1];
00191   buffer [charraylen] = '\0';
00192   {                                          // avoid for-scope problem
00193     for (int i = 0; i < n; ++i) {
00194       strncpy (buffer, charrayin + i*charraylen, charraylen);
00195       FCharacter *cp = buffer + charraylen - 1;
00196       while (cp > buffer - 1 && (*cp == ' ' || *cp == '\0')) {
00197         *(cp--) = '\0';
00198       }
00199       sarrayout [i] = buffer;
00200     }
00201   }
00202   delete[] buffer;
00203 }
00204 void string_from_fortran (unsigned int charraylen,
00205                           const FCharacter *charrayin,
00206                           string& sout) {
00207 // take an array of characters,padded with spaces and possibly 0-terminated,
00208 // and transfer it to a C++ string
00209 // 
00210 // arguments:
00211 // n:          number of rows in charrayin, sarrayout
00212 // charraylen: input: number of columns in charrayin
00213 // charrayin:  input character array
00214 // sout:       output string
00215   FCharacter *buffer = new FCharacter [charraylen+1];
00216   buffer [charraylen] = '\0';
00217   strncpy (buffer, charrayin, charraylen);
00218   FCharacter *cp = buffer + charraylen - 1;
00219   while (cp > buffer - 1 && (*cp == ' ' || *cp == '\0')) {
00220     *(cp--) = '\0';
00221   }
00222   sout = buffer;
00223   delete[] buffer;
00224 }
00225 
00226 // Definition of non-inline functions
00227 
00228 // =============================================================================             
00229 // Chapter 3: Ntuples
00230 // =============================================================================             
00231 
00232 // 3.2 Row-wise-ntuples (RWN)
00233 
00234 // 3.2.1 Booking a RWN
00235 
00236 void hbookn (FInteger id, 
00237              const string& chtitl, 
00238              FInteger nvar, 
00239              const string& chrzpa, 
00240              FInteger nwbuff, 
00241              const string chtags[]) {
00242   unsigned int chtagslen = 0;
00243   FCharacter *chtags_ = 0;
00244   string_to_fortran (nvar, chtagslen, chtags, chtags_); 
00245   hbookn_ (&id, chtitl.c_str(), &nvar, chrzpa.c_str(), &nwbuff, chtags_,
00246            chtitl.length(), chrzpa.length(), chtagslen);
00247   delete[] chtags_;
00248 }
00249 
00250 // 3.5 Get information about an ntuple
00251 
00252 void hgiven (FInteger id,
00253              string& chtitl,
00254              FInteger& nvar,
00255              string chtag[],
00256              FReal rlow[],
00257              FReal rhigh[]) {
00258   FCharacter chtitl_ [80];
00259   FCharacter *chtag_ = new FCharacter [nvar*10];
00260   hgiven_ (&id, chtitl_, &nvar, chtag_, rlow, rhigh, 80, 10);
00261   string_from_fortran (80, chtitl_, chtitl);
00262   string_from_fortran (nvar, 10, chtag_, chtag);
00263   delete[] chtag_;
00264 }  
00265 
00266 // 3.5.2 Retrieve the contents of a CWN into a common block
00267 
00268 FInteger hgntv  (FInteger id, 
00269                  const string chvar[],
00270                  FInteger nvar,
00271                  FInteger irow,
00272                  FInteger& ierr) {
00273   unsigned int chvarlen = 1;
00274   FCharacter *chvar_ = 0;
00275   string_to_fortran (nvar, chvarlen, chvar, chvar_); 
00276   hgntv_ (&id, chvar_, &nvar, &irow, &ierr, chvarlen);
00277   delete [] chvar_;
00278   return ierr;
00279 }                  
00280 
00281 
00282 // =============================================================================             
00283 // Chapter 4: Advanced features for booking and editing operations
00284 // =============================================================================             
00285 
00286 // 4.1 Overview of booking options
00287 
00288 // 4.1.8 Axis labels and histograms
00289 
00290 void hlabel (FInteger id,
00291              FInteger nlab,
00292              string clab[],
00293              const string& chopt) {
00294   unsigned int clablen = 20;
00295   FCharacter *clab_ = 0;
00296   string_to_fortran (nlab, clablen, clab, clab_); 
00297   hlabel_ (&id, &nlab, clab_, chopt.c_str(), clablen, chopt.length());
00298   string_from_fortran (nlab, clablen, clab_, clab); 
00299   delete[] clab_;
00300 } 
00301 
00302 // =============================================================================             
00303 // Chapter 5: Accessing Information
00304 // =============================================================================             
00305                 
00306  
00307 // 5.12 Histogram definition
00308 
00309 void hgive  (FInteger id,
00310              string& chtitl,
00311              FInteger& nx,
00312              FReal& xmi,
00313              FReal& xma,
00314              FInteger& ny,
00315              FReal& ymi,
00316              FReal& yma,
00317              FInteger& nwt,
00318              FInteger& loc) {
00319   FCharacter chtitl_ [80];
00320   hgive_ (&id, chtitl_, &nx, &xmi, &xma, &ny, &ymi, &yma, &nwt, &loc, 80);
00321   string_from_fortran (80, chtitl_, chtitl);
00322 }  
00323 
00324   
00325 // =============================================================================             
00326 // Chapter 7: Fitting, parametrization and smoothing
00327 // =============================================================================             
00328 
00329 // 7.1 Fitting
00330 
00331 // 7.1.4 Naming the parameters of a fit
00332 
00333 void hfinam (FInteger id, 
00334              const string chpnam[],
00335              FInteger npar) {
00336   unsigned int chpnamlen = 0;
00337   FCharacter *chpnam_ = 0;
00338   string_to_fortran (npar, chpnamlen, chpnam, chpnam_);
00339   hfinam_ (&id, chpnam_, &npar, chpnamlen);
00340   delete[] chpnam_;
00341 }
00342 void hfinam (FInteger id, 
00343              const vector<string>& chpnam,
00344              FInteger npar) {
00345   unsigned int chpnamlen = 0;
00346   FCharacter *chpnam_ = 0;
00347   if (static_cast<int>(chpnam.size()) < npar) npar = chpnam.size();
00348   string_to_fortran (chpnamlen, chpnam, chpnam_);
00349   hfinam_ (&id, chpnam_, &npar, chpnamlen);
00350   delete[] chpnam_;
00351 }
00352 void hgfit  (FInteger id,
00353              FInteger& nfpar,
00354              FInteger& npfits,
00355              FReal& fitchi,
00356              FReal fitpar [],
00357              FReal fitsig [],
00358              string fitnam[]) {
00359   FCharacter fitnam_ [25*8];
00360   hgfit_ (&id, &nfpar, &npfits, &fitchi, fitpar, fitsig, fitnam_, 8);
00361   string_from_fortran (min (25, nfpar), 8, fitnam_, fitnam);
00362 }
00363 
00364   
00365 // =============================================================================             
00366 // Chapter 8: Memory management and input/output routines
00367 // =============================================================================             
00368 
00369 // 8.3 Directories
00370 
00371 FInteger hlnext (FInteger& idh,
00372                  string& chtype, 
00373                  string& chtitl, 
00374                  const string& chopt) {
00375   FCharacter chtype_ [2];
00376   FCharacter chtitl_ [80];
00377   hlnext_ (&idh, chtype_, chtitl_, chopt.c_str(), 2, 80, chopt.length());
00378   string_from_fortran (2, chtype_, chtype);
00379   string_from_fortran (2, chtitl_, chtitl);
00380   return idh;
00381 }
00382 FInteger hrdir  (FInteger maxdir,
00383                  string chdir[],
00384                  FInteger &ndir) {
00385   FCharacter *chdir_ = new FCharacter [maxdir*255];               
00386   hrdir_ (&maxdir, chdir_, &ndir, 255);
00387   string_from_fortran (ndir, 255, chdir_, chdir);
00388   delete[] chdir_;
00389   return ndir;
00390 }
00391 FInteger hrdir  (FInteger maxdir,
00392                  string chdir[]) {
00393   FInteger ndir = 0;
00394   FCharacter *chdir_ = new FCharacter [maxdir*255];               
00395   hrdir_ (&maxdir, chdir_, &ndir, 255);
00396   string_from_fortran (ndir, 255, chdir_, chdir);
00397   delete[] chdir_;
00398   return ndir;
00399 }
00400 void hmerge (FInteger nfiles,
00401              const string chfin [],
00402              const string& chfout) {
00403   unsigned int chfinlen = 0;
00404   FCharacter *chfin_ = 0;
00405   string_to_fortran (nfiles, chfinlen, chfin, chfin_);
00406   hmerge_ (&nfiles, chfin_, chfout.c_str(), chfinlen, chfout.length());
00407   delete[] chfin_;
00408 }
00409 
00410 //==============================================================================
00411 // The PAWC common
00412 
00413 // Definition of the PAWC common
00414 // Designed in a way that ensures that HLIMIT is always called before 
00415 // the first compilation unit that uses hbook defines its variables
00416 //
00417 // For a discussion of this technique, see Stroustrup, 3rd ed, p. 640
00418 
00419 PAWC_Type pawc_;
00420 
00421 int PAWC_Type::Init::count = 0;
00422 
00423 // Interface to fitting common block
00424 
00425 HCFITDType hcfitd_;
00426 QUESTType quest_;

Generated on Fri Sep 14 17:38:20 2007 for Kinfit by doxygen 1.3.2