BfldMapRect2d Class Reference

#include <BfldMapRect2d.h>

Inheritance diagram for BfldMapRect2d:
BfldMap BfldRefCnt

List of all members.

Public Member Functions

 BfldMapRect2d ()
 BfldMapRect2d (BfldGrid::Grid_t grid, Int_t variant)
virtual ~BfldMapRect2d ()
Float_t GetX0 () const
Float_t GetY0 () const
Float_t GetDx () const
Float_t GetDy () const
Int_t GetNx () const
Int_t GetNy () const
Bool_t GetIsUVZ () const
const Int_t * GetQuadFlags () const

Protected Attributes

std::string fHeader
Float_t fX0
Float_t fDx
Int_t fNx
Float_t fY0
Float_t fDy
Int_t fNy
Bool_t fIsUVZ
Bool_t fQuadrant
Int_t fQuadFlags [4]
Int_t fFileForm

Private Member Functions

void Clear (const Option_t *="")

Detailed Description

Definition at line 25 of file BfldMapRect2d.h.


Constructor & Destructor Documentation

BfldMapRect2d::BfldMapRect2d (  ) 

Definition at line 47 of file BfldMapRect2d.cxx.

00048    : BfldMap()
00049 {
00050   this->Clear();
00051 }

BfldMapRect2d::BfldMapRect2d ( BfldGrid::Grid_t  grid,
Int_t  variant 
)

Definition at line 54 of file BfldMapRect2d.cxx.

References BfldMap::AddGenerator(), Clear(), Munits::cm, BfldMap::fDataSource, fDx, fDy, fFileForm, BfldMap::fFilledOkay, BfldMap::fGeneratedCoilCurrent, fHeader, fIsUVZ, fNx, fNy, fQuadFlags, fQuadrant, fX0, fY0, BfldMeshRect2d::GeneratorToIndices(), gSystem(), Msg::kDebug, Msg::kError, Msg::kFatal, Munits::kilogauss, Msg::kInfo, Msg::kSynopsis, Msg::kWarning, MAXMSG, MSG, BfldMeshRect2d::NearestGenerator(), and BfldMap::ResizeVectors().

00055    : BfldMap(grid,variant)
00056 {
00057 
00058    this->Clear();
00059 
00060    // read in a Map
00061 
00062    // strip off "-" sign if necessary
00063    Int_t varabs = TMath::Abs(variant);
00064 
00065    MSG("Bfld",Msg::kDebug) 
00066      << "BfldMapRect2d ctor map " << varabs << "." << endl;
00067 
00068    char cstrbuf[255];
00069 
00070    // format and expand file name
00071    const char* search_path = ".:$BMAPPATH";
00072    sprintf(cstrbuf,"bfld_%3d.dat",varabs);
00073    char* expanded = gSystem->Which(search_path,cstrbuf,kReadPermission);
00074    if (!expanded) {
00075       MSG("Bfld",Msg::kError) << "can not find input file: '" 
00076                               << cstrbuf <<"'"<< endl;
00077       return;
00078    }
00079    fDataSource = expanded;
00080 
00081    delete [] expanded;  // user responsible for deleting returned string
00082 
00083    // open the file for reading
00084    ifstream from;
00085    from.open(fDataSource.c_str(),ios::in);
00086    if (!from) {
00087       MSG("Bfld",Msg::kError) << "can not open input file: '" 
00088                               << fDataSource <<"'"<< endl;
00089       return;
00090    }
00091 
00092    // read in the header line
00093    from.getline(cstrbuf,sizeof(cstrbuf),'\n');
00094    fHeader = cstrbuf;
00095 
00096    MSG("Bfld",Msg::kInfo) 
00097      << "BfldMapRect2d read file: " << fDataSource << endl;
00098    MSG("Bfld",Msg::kInfo) 
00099      << "BfldMapRect2d: " << fHeader << endl;
00100 
00101    fIsUVZ = ( fHeader.find("UVZ") != string::npos );
00102    Double_t dirScale  = 1;
00103 
00104    // search the title string for a coil current
00105    // generally a token like 15kAturns, 40kAturns
00106    string::size_type loc_aturn = fHeader.find("Aturn");
00107    // alas sometimes the header has the value string "40k A-turns"
00108    if (loc_aturn == string::npos) loc_aturn = fHeader.find("A-turn");
00109 
00110    if (loc_aturn != string::npos) {
00111      // find last space before 'Aturn'
00112      string::size_type loc_space = fHeader.find_last_of(" ",loc_aturn); 
00113      std::string sval = fHeader.substr(loc_space,loc_aturn-loc_space);
00114      MSG("Bfld",Msg::kDebug) << " found Aturns: '" << sval << "'" << endl;
00115      string::size_type loc_k = sval.find("k");
00116      if (loc_k != string::npos) {
00117        sval.replace(loc_k,1," ");
00118        MSG("Bfld",Msg::kDebug) << " found 'k' new string '" << sval << "'" << endl;
00119      }
00120      std::istringstream inputString(sval);
00121      inputString >> fGeneratedCoilCurrent;
00122      if (loc_k != string::npos) fGeneratedCoilCurrent *= 1000;
00123 
00124      if ( fHeader.find("CurrentForward") == string::npos ) {
00125        MSG("Bfld",Msg::kInfo) 
00126          << "BfldMapRect2d: maps were created focusing mu+;"
00127          << " correct this with an extra -1" << endl;
00128        dirScale = -1;
00129      }   
00130 
00131      MSG("Bfld",Msg::kInfo) 
00132        << "BfldMapRect2d: GeneratedCoilCurrent=" 
00133        << setprecision(4) << (fGeneratedCoilCurrent/1000.)
00134        << "kA*turns based on header info."
00135        << resetiosflags(ios::floatfield)  // reset to "%g" format
00136        << endl;
00137    }
00138    else {
00139      MSG("Bfld",Msg::kError) 
00140        << "BfldMapRect2d: could not find coil current in input file header "
00141        << endl;
00142    }
00143 
00144    from >> fX0 >> fDx >> fNx >> fY0 >> fDy >> fNy;
00145    // lengths read in from map files are in cm, convert to base units
00146    fX0 *= Munits::cm;
00147    fDx *= Munits::cm;
00148    fY0 *= Munits::cm;
00149    fDy *= Munits::cm;
00150 
00151    MSG("Bfld",Msg::kInfo) 
00152      << "BfldMapRect2d:"
00153      << setprecision(6)
00154      << " x0 " << fX0 << " dx " << fDx << " nx " << fNx 
00155      << " y0 " << fY0 << " dy " << fDy << " ny " << fNy 
00156      << resetiosflags(ios::floatfield)  // reset to "%g" format
00157      << endl;
00158 
00159    // next 3 elements are irrelevant (z0,dz,nz)
00160    Float_t z0, dz;
00161    Int_t   nz;
00162    from >> z0 >> dz >> nz;
00163 
00164    // read in remainder of the line for any quadflags
00165    from.getline(cstrbuf,sizeof(cstrbuf),'\n');
00166    TString quads = cstrbuf;
00167 
00168    // replace "x" with " "
00169    quads = quads.ReplaceAll("x"," ");
00170    // quads should be of the form ' 00 00 00 00  FNAL2'
00171    MSG("Bfld",Msg::kDebug) << "quads string '" 
00172                            << quads << "'" << endl;
00173 
00174    fFileForm = 0;
00175    if (quads.Contains("FNAL"))  fFileForm = 1;
00176    if (quads.Contains("FNAL2")) fFileForm = 2;
00177 
00178    // remove any "FNAL" flag
00179    if (quads.Length() > 12) quads = quads.Remove(12);
00180 
00181    // scan the remaining part for the quad flags
00182    if (quads.Length() > 0) {
00183       sscanf(quads,"%3d%3d%3d%3d",
00184              fQuadFlags,fQuadFlags+1,fQuadFlags+2,fQuadFlags+3);
00185    } else {
00186       MSG("Bfld",Msg::kInfo) << "no quads info - use defaults" << endl;
00187       fQuadFlags[0] = 0;
00188       fQuadFlags[1] = 2;
00189       fQuadFlags[2] = 3;
00190       fQuadFlags[3] = 1;
00191    }
00192 
00193    // if x0=0,y0=0 assume map only has one quadrant 
00194    fQuadrant = ( 0 == fX0 && 0 == fY0 );
00195 
00196    MSG("Bfld",Msg::kSynopsis) 
00197      << "BfldMapRect2d: fileform " << fFileForm
00198      << "  quadflags (" 
00199      << fQuadFlags[0] << "," 
00200      << fQuadFlags[1] << "," 
00201      << fQuadFlags[2] << "," 
00202      << fQuadFlags[3] << ")  use them " 
00203      << (fQuadrant ? "'true'" : "'false'") 
00204      << endl;
00205 
00206    ResizeVectors(fNx*fNy);
00207 
00208    // build a temporary mesh so we can can use the functions
00209    // for finding the generator from x,y etc
00210    // but this isn't the one that will go into the loan pool
00211    BfldMeshRect2d tmpMesh(this); 
00212    
00213    Int_t   ii, indx;
00214    Float_t x,y,z;
00215    Float_t bx,by,bz,bmag;
00216 
00217    const Float_t GaussToKGauss = 0.001;
00218    const Float_t TeslaToKGauss = 10.;
00219 
00220    std::vector<bool> seen_gen(fNx*fNy);
00221 
00222    // start reading the file
00223    Int_t ilines=0;
00224    //original for (ilines=0; ilines < fNx*fNy; ilines++) {
00225    for (ilines=0; true ; ilines++) {
00226 
00227       switch (fFileForm) {
00228       case 0:
00229          from >> x >> y >> z >> bx >> by >> bz >> bmag;
00230          // LLNL maps were in gauss ... need kGauss
00231          bx   = bx   * GaussToKGauss;
00232          by   = by   * GaussToKGauss;
00233          bz   = bz   * GaussToKGauss;
00234          bmag = bmag * GaussToKGauss;
00235          break;
00236       case 1:
00237          from >> ii >> x >> y >> bx >> by;
00238          // FNAL (version 1) maps were in Tesla
00239          bx   = bx   * TeslaToKGauss;
00240          by   = by   * TeslaToKGauss;
00241          bz   = 0.0;
00242          // avoid unitialized values
00243          z    = 0.0;
00244          bmag = TMath::Sqrt(bx*bx+by*by+bz*bz);
00245          break;
00246       case 2:
00247          from >> x >> y >> z >> bx >> by >> bz >> bmag;
00248          // FNAL (version 2) maps were in Tesla
00249          bx   = bx   * TeslaToKGauss;
00250          by   = by   * TeslaToKGauss;
00251          bz   = bz   * TeslaToKGauss;
00252          bmag = bmag * TeslaToKGauss;
00253          break;
00254       default:
00255          MSG("Bfld",Msg::kFatal) << " bad FileForm " << fFileForm << endl;
00256          break;
00257       }
00258 
00259       if (!from.good()) {
00260         if (ilines != fNx*fNy) { //  read one too far ...
00261           MSG("Bfld",Msg::kInfo) 
00262             << "BfldMapRect2d: early EOF on " << endl 
00263             << "  "
00264             << fDataSource 
00265             << endl
00266             << "  saw " << ilines << " entries, expected " 
00267             << fNx*fNy << "; could be due to suppressed (0,0) points."
00268             << endl;
00269         }
00270         break;
00271       }
00272 
00273       // The above puts field values in kGauss
00274       // convert them to base units
00275       // Also make correction for reversed current direction when generated
00276       bx   *= Munits::kilogauss * dirScale;
00277       by   *= Munits::kilogauss * dirScale;
00278       bz   *= Munits::kilogauss * dirScale;
00279       bmag *= Munits::kilogauss;
00280       // positions read in are given in cm
00281       x    *= Munits::cm;
00282       y    *= Munits::cm;
00283       z    *= Munits::cm;
00284 
00285       // all the maps aren't in consistent order (ascending/descending x,y)
00286       // so we have to actually interpret the (x,y) values to determine 
00287       // the array index.
00288       // we'll put them in the array in a way that we can lookup 
00289       // based on fX0,fDx,fNx,fY0,fDy,fNy
00290 
00291       // when reading points, find the nearest generator
00292       // not the lower left one
00293       indx = tmpMesh.NearestGenerator(x,y);
00294 
00295       if ( seen_gen[indx] ) {
00296         MAXMSG("Bfld",Msg::kWarning,20)
00297           << "Duplicate entry for indx " << indx
00298           << " x= " << x << ", y= " << y << endl;
00299       }
00300       else {
00301         seen_gen[indx] = true;
00302       }
00303 
00304       // cross check! for bad data points
00305       Int_t jxx, jyy;
00306       tmpMesh.GeneratorToIndices(indx,jxx,jyy);
00307       Float_t xx,yy;
00308       xx = fX0 + jxx*fDx;
00309       yy = fY0 + jyy*fDy;
00310       const Float_t dxy_epsilon = 0.0005;
00311       static MsgFormat f9p6("f9.6"); 
00312       static MsgFormat f5p1("f5.1"); 
00313       if ( TMath::Abs(x-xx) > dxy_epsilon*fDx ||
00314            TMath::Abs(y-yy) > dxy_epsilon*fDy    ) {
00315         MAXMSG("Bfld",Msg::kWarning,20)
00316           << " [" << setw(5) << ilines << "] off grid? "
00317           << endl 
00318           <<   "  (x,y)=(" << f9p6(x)  << "," << f9p6(y)  << ") "
00319           << " expected (" << f9p6(xx) << "," << f9p6(yy) << ") "
00320           << endl
00321           << "   error is " << f5p1(100.*TMath::Abs(x-xx)/fDx) << "% of dx "
00322           <<           ", " << f5p1(100.*TMath::Abs(y-yy)/fDy) << "% of dy "
00323           << endl;
00324       }
00325 
00326       AddGenerator(indx,bx,by,bz);
00327 
00328 #ifdef BLAHBLAH
00329       // one *might* think of using [i] rather than [indx]
00330       if (indx != ilines &&  ilines < 2*fNx ) {
00331          MSG("Bfld",Msg::kWarning) 
00332            << " i " << i << " indx " << indx
00333            << " (" << x << "," << y << ")" << endl;
00334       }
00335 #endif
00336 
00337    }
00338 
00339    MSG("Bfld",Msg::kDebug) 
00340      << "BfldMapRect2d: read " << ilines << " entries from "
00341      << fDataSource << endl;
00342 
00343    fFilledOkay = kTRUE;
00344 }

BfldMapRect2d::~BfldMapRect2d (  )  [virtual]

Definition at line 347 of file BfldMapRect2d.cxx.

References BfldMap::fGrid, BfldMap::fVariant, Msg::kDebug, BfldGrid::kUndefined, and MSG.

00348 {
00349    // dtor deletes owned components
00350 
00351    MSG("Bfld",Msg::kDebug) << "~BfldMapRect2d variant " << fVariant << endl;
00352 
00353    fGrid    = BfldGrid::kUndefined;
00354    fVariant = -1;
00355 
00356 }


Member Function Documentation

void BfldMapRect2d::Clear ( const Option_t *  = ""  )  [private]

Definition at line 359 of file BfldMapRect2d.cxx.

References fDx, fDy, fFileForm, fNx, fNy, fQuadFlags, fQuadrant, fX0, and fY0.

Referenced by BfldMapRect2d().

00360 {
00361    // Clear to defined state.
00362 
00363    fX0           = 0.;
00364    fDx           = 0.;
00365    fNx           = 0;
00366    fY0           = 0.;
00367    fDy           = 0.;
00368    fNy           = 0;
00369    fQuadrant     = false;
00370    fQuadFlags[0] = 0;
00371    fQuadFlags[1] = 0;
00372    fQuadFlags[2] = 0;
00373    fQuadFlags[3] = 0;
00374    fFileForm     = 0;
00375 
00376 }

Float_t BfldMapRect2d::GetDx (  )  const [inline]

Definition at line 35 of file BfldMapRect2d.h.

References fDx.

Referenced by BfldMeshRect2d::CopyMapRect2d().

00035 { return fDx; }

Float_t BfldMapRect2d::GetDy (  )  const [inline]

Definition at line 36 of file BfldMapRect2d.h.

References fDy.

Referenced by BfldMeshRect2d::CopyMapRect2d().

00036 { return fDy; }

Bool_t BfldMapRect2d::GetIsUVZ (  )  const [inline]

Definition at line 40 of file BfldMapRect2d.h.

References fIsUVZ.

Referenced by BfldMeshRect2d::CopyMapRect2d().

00040 { return fIsUVZ; }

Int_t BfldMapRect2d::GetNx (  )  const [inline]

Definition at line 37 of file BfldMapRect2d.h.

References fNx.

Referenced by BfldMeshRect2d::CopyMapRect2d().

00037 { return fNx; }

Int_t BfldMapRect2d::GetNy (  )  const [inline]

Definition at line 38 of file BfldMapRect2d.h.

References fNy.

Referenced by BfldMeshRect2d::CopyMapRect2d().

00038 { return fNy; }

const Int_t* BfldMapRect2d::GetQuadFlags (  )  const [inline]

Definition at line 42 of file BfldMapRect2d.h.

References fQuadFlags.

Referenced by BfldMeshRect2d::CopyMapRect2d().

00042 { return fQuadFlags; }

Float_t BfldMapRect2d::GetX0 (  )  const [inline]

Definition at line 33 of file BfldMapRect2d.h.

References fX0.

Referenced by BfldMeshRect2d::CopyMapRect2d().

00033 { return fX0; }

Float_t BfldMapRect2d::GetY0 (  )  const [inline]

Definition at line 34 of file BfldMapRect2d.h.

References fY0.

Referenced by BfldMeshRect2d::CopyMapRect2d().

00034 { return fY0; }


Member Data Documentation

Float_t BfldMapRect2d::fDx [protected]

Definition at line 49 of file BfldMapRect2d.h.

Referenced by BfldMapRect2d(), Clear(), and GetDx().

Float_t BfldMapRect2d::fDy [protected]

Definition at line 53 of file BfldMapRect2d.h.

Referenced by BfldMapRect2d(), Clear(), and GetDy().

Int_t BfldMapRect2d::fFileForm [protected]

Definition at line 60 of file BfldMapRect2d.h.

Referenced by BfldMapRect2d(), and Clear().

std::string BfldMapRect2d::fHeader [protected]

Definition at line 46 of file BfldMapRect2d.h.

Referenced by BfldMapRect2d().

Bool_t BfldMapRect2d::fIsUVZ [protected]

Definition at line 56 of file BfldMapRect2d.h.

Referenced by BfldMapRect2d(), and GetIsUVZ().

Int_t BfldMapRect2d::fNx [protected]

Definition at line 50 of file BfldMapRect2d.h.

Referenced by BfldMapRect2d(), Clear(), and GetNx().

Int_t BfldMapRect2d::fNy [protected]

Definition at line 54 of file BfldMapRect2d.h.

Referenced by BfldMapRect2d(), Clear(), and GetNy().

Int_t BfldMapRect2d::fQuadFlags[4] [protected]

Definition at line 59 of file BfldMapRect2d.h.

Referenced by BfldMapRect2d(), Clear(), and GetQuadFlags().

Bool_t BfldMapRect2d::fQuadrant [protected]

Definition at line 58 of file BfldMapRect2d.h.

Referenced by BfldMapRect2d(), and Clear().

Float_t BfldMapRect2d::fX0 [protected]

Definition at line 48 of file BfldMapRect2d.h.

Referenced by BfldMapRect2d(), Clear(), and GetX0().

Float_t BfldMapRect2d::fY0 [protected]

Definition at line 52 of file BfldMapRect2d.h.

Referenced by BfldMapRect2d(), Clear(), and GetY0().


The documentation for this class was generated from the following files:

Generated on 22 Nov 2017 for loon by  doxygen 1.6.1