Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:17:09

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 //
0027 //
0028 // Author: Elena Guardincerri (Elena.Guardincerri@ge.infn.it)
0029 //
0030 // History:
0031 // -----------
0032 // 28 Nov 2001 Elena Guardincerri     Created
0033 //
0034 // -------------------------------------------------------------------
0035 
0036 #include "XrayFluoDataSet.hh"
0037 #include <fstream>
0038 #include "G4VDataSetAlgorithm.hh"
0039 
0040 XrayFluoDataSet::XrayFluoDataSet(G4int /*Z*/,
0041              G4DataVector* points, 
0042              G4DataVector* values,
0043              const G4VDataSetAlgorithm* interpolation,
0044              G4double unitE, G4double unitData)
0045   :energies(points), data(values), algorithm(interpolation)
0046 {
0047   numberOfBins = energies->size();
0048   unit1 = unitE;
0049   unit2 = unitData;
0050 }
0051 
0052 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0053 
0054 XrayFluoDataSet:: XrayFluoDataSet(G4int /*Z*/, 
0055               const G4String& dataFile,
0056               const G4VDataSetAlgorithm* interpolation,
0057               G4double unitE, G4double unitData)
0058   : algorithm(interpolation)
0059 {
0060   energies = new G4DataVector;
0061   data = new G4DataVector;
0062   unit1 = unitE;
0063   unit2 = unitData;  
0064   LoadData(dataFile);
0065   numberOfBins = energies->size();
0066 }
0067 
0068 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0069 
0070 // Destructor
0071 XrayFluoDataSet::~XrayFluoDataSet()
0072 { 
0073   delete energies;
0074   delete data;
0075 }
0076 
0077 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0078 
0079 G4double XrayFluoDataSet::FindValue(G4double e, G4int) const
0080 {
0081   G4double value;
0082   G4double e0 = (*energies)[0];
0083   // Protections
0084   size_t bin = FindBinLocation(e);
0085   if (bin == numberOfBins)
0086     {
0087      
0088       value = (*data)[bin];
0089     }
0090   else if (e <= e0)
0091     {
0092     
0093       value = (*data)[0];
0094     }
0095   else
0096     {
0097       value = algorithm->Calculate(e,bin,*energies,*data);
0098     }
0099   
0100   return value;
0101 }
0102 
0103 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0104 
0105 G4int XrayFluoDataSet::FindBinLocation(G4double energy) const
0106 {
0107   // Protection against call outside allowed range
0108   G4double e0 = (*energies)[0];
0109   if (energy < e0)
0110     {
0111     
0112       energy = e0;
0113     }
0114 
0115   size_t lowerBound = 0;
0116   size_t upperBound = numberOfBins - 1;
0117   
0118   // Binary search
0119   while (lowerBound <= upperBound) 
0120     {
0121       size_t midBin = (lowerBound + upperBound)/2;
0122       if ( energy < (*energies)[midBin] ) upperBound = midBin-1;
0123       else lowerBound = midBin+1;
0124   }
0125  
0126   return upperBound;
0127 }
0128 
0129 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0130 
0131 G4bool XrayFluoDataSet::LoadData(const G4String& fileName)
0132 {
0133   // Build the complete string identifying the file with the data set
0134   G4String dirFile = "";
0135   
0136   char* path;
0137 
0138   path = std::getenv("XRAYDATA");
0139   if (!path)
0140     path = std::getenv("PWD");
0141 
0142   //G4cout << path << G4endl;
0143   //G4cout << fileName << G4endl;
0144 
0145 
0146   G4String pathString(path);
0147   pathString += "\0";
0148   dirFile = pathString + "/" + fileName + ".dat";
0149     
0150   std::ifstream file(dirFile);
0151   std::filebuf* lsdp = file.rdbuf();
0152   
0153   if (! (lsdp->is_open()) )
0154     {
0155       G4ExceptionDescription execp;
0156       execp << "XrayFluoDataSet - data file: " + dirFile + " not found"<<G4endl;
0157       G4Exception("XrayFluoDataSet::LoadData()","example-xray_fluorescence01",
0158       FatalException, execp);
0159     }
0160   G4double a = 0;
0161   G4int k = 1;
0162 
0163   do
0164     {
0165       file >> a;
0166       G4int nColumns = 2;
0167       // The file is organized into two columns:
0168       // 1st column is the energy
0169       // 2nd column is the corresponding value
0170       // The file terminates with the pattern: -1   -1
0171       //                                       -2   -2
0172       if (a == -1 || a == -2)
0173     {
0174      
0175     }
0176       else
0177     {
0178       if (k%nColumns != 0)
0179         {   
0180           G4double e = a * unit1;
0181           energies->push_back(e);
0182         
0183           k++;
0184 
0185         }
0186       else if (k%nColumns == 0)
0187         {
0188           G4double value = a * unit2;
0189           data->push_back(value);
0190          
0191           k = 1;
0192         }
0193     }
0194       
0195     } while (a != -2); // end of file
0196   
0197   file.close();
0198   return true;
0199 
0200 }
0201 
0202 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo....
0203 
0204 void XrayFluoDataSet::PrintData() const
0205 {
0206   size_t size = numberOfBins;
0207   for (size_t i=0; i<size; i++)
0208     {
0209       G4double e = (*energies)[i]  / unit1;
0210       G4double sigma = (*data)[i] / unit2 ;
0211       G4cout << "Point: "
0212          << e
0213          << " - Data value : "
0214          << sigma 
0215          << G4endl; 
0216     }
0217 }
0218 
0219 
0220