ROBAST logo
/******************************************************************************
 * Copyright (C) 2006-, Akira Okumura                                         *
 * All rights reserved.                                                       *
 *****************************************************************************/

///////////////////////////////////////////////////////////////////////////////
//
// AGlassCatalog
//
// Glass catalog
//
///////////////////////////////////////////////////////////////////////////////

#include <fstream>

#include "TSystem.h"

#include "AGlassCatalog.h"
#include "AOpticsManager.h"

ClassImp(AGlassCatalog);

//_____________________________________________________________________________
AGlassCatalog::AGlassCatalog() {}

//_____________________________________________________________________________
AGlassCatalog::AGlassCatalog(const std::string& catalog_file) {
  std::ifstream fin;
  fin.open(gSystem->ExpandPathName(catalog_file.c_str()));

  if (!fin.is_open()) {
    Error("AGlassCatalog", "Cannot open %s", catalog_file.c_str());
    return;
  }

  // ASCII glass format catalog file (AGF)
  if (catalog_file.size() - catalog_file.find(".agf") != 4 &&
      catalog_file.size() - catalog_file.find(".AGF") != 4) {
    Error("AGlassCatalog", "Cannot read a non-ZEMAX file");
    return;
  }

  const std::size_t bufsize = 200;
  char buf[bufsize], glass_name[bufsize];
  std::string glass_name_s;
  Double_t nd;
  Int_t formula;
  std::shared_ptr<TGraph> graph;
  const std::size_t ncd = 8;
  Double_t cd[ncd];

  while (fin.good()) {
    fin.getline(buf, bufsize);
    if (strncmp(buf, "NM ", 3) == 0) {
      // found a glass name
      // e.g., "NM N-BK7 2 517642.251 1.5168 64.17 0 1"
      // where
      // glass name : N-BK7
      // dispersion formula : 2 (2 = Sellmeier)
      // product number? : 517642.251
      // Nd : 1.5168
      // Vd : 64.17
      // ignore thremal expantion: 0
      // exclude substitution: 1
      // meta material? : ?
      char product_number[bufsize];
      Double_t vd;
      if (sscanf(buf, "NM %s %d %s %lf %lf", glass_name, &formula,
                 product_number, &nd, &vd) != 5) {
        Warning("AGlassCatalog", "Bad format line found: %s", buf);
      }

      glass_name_s = std::string(glass_name);
      graph = std::make_shared<TGraph>();

      fIndexMap.insert(
          std::make_pair(glass_name_s, std::shared_ptr<ARefractiveIndex>(0)));
    } else if (strncmp(buf, "CD ", 3) == 0) {
      int ret = sscanf(buf, "CD %lf %lf %lf %lf %lf %lf %lf %lf", &cd[0],
                       &cd[1], &cd[2], &cd[3], &cd[4], &cd[5], &cd[6], &cd[7]);
      if (formula == 2 && ret >= 6) {
        auto it = fIndexMap.find(glass_name_s);
        if (it != fIndexMap.end()) {
          it->second = std::make_shared<ASellmeierFormula>(cd[0], cd[2], cd[4],
                                                           cd[1], cd[3], cd[5]);
          it->second->SetExtinctionCoefficient(graph);
        }
      }
    } else if (strncmp(buf, "IT ", 3) == 0) {
      Double_t wl, T, d;  // lambda (um), transmittance, thickness (mm)
      int ret = sscanf(buf, "IT %lf %lf %lf", &wl, &T, &d);
      if (ret == 3) {
        wl *= AOpticsManager::um();
        d *= AOpticsManager::mm();
        Double_t absl = -d / TMath::Log(T);
        Double_t k =
            ARefractiveIndex::AbsorptionLengthToExtinctionCoefficient(absl, wl);
        graph->SetPoint(graph->GetN(), wl, k);
      } else if (ret == 2) {
        // Some glass materials such as N-LASF9 has incomplete lines
        // Just ignore
      } else {
        Warning("AGlassCatalog", "Bad format line found: %s", buf);
      }
    }
  }
}

//_____________________________________________________________________________
AGlassCatalog::~AGlassCatalog() {}

//_____________________________________________________________________________
std::shared_ptr<ARefractiveIndex> AGlassCatalog::GetRefractiveIndex(
    const std::string& name) {
  auto it = fIndexMap.find(name);
  if (it == fIndexMap.end()) {
    return 0;
  } else {
    return it->second;
  }
}
 AGlassCatalog.cxx:1
 AGlassCatalog.cxx:2
 AGlassCatalog.cxx:3
 AGlassCatalog.cxx:4
 AGlassCatalog.cxx:5
 AGlassCatalog.cxx:6
 AGlassCatalog.cxx:7
 AGlassCatalog.cxx:8
 AGlassCatalog.cxx:9
 AGlassCatalog.cxx:10
 AGlassCatalog.cxx:11
 AGlassCatalog.cxx:12
 AGlassCatalog.cxx:13
 AGlassCatalog.cxx:14
 AGlassCatalog.cxx:15
 AGlassCatalog.cxx:16
 AGlassCatalog.cxx:17
 AGlassCatalog.cxx:18
 AGlassCatalog.cxx:19
 AGlassCatalog.cxx:20
 AGlassCatalog.cxx:21
 AGlassCatalog.cxx:22
 AGlassCatalog.cxx:23
 AGlassCatalog.cxx:24
 AGlassCatalog.cxx:25
 AGlassCatalog.cxx:26
 AGlassCatalog.cxx:27
 AGlassCatalog.cxx:28
 AGlassCatalog.cxx:29
 AGlassCatalog.cxx:30
 AGlassCatalog.cxx:31
 AGlassCatalog.cxx:32
 AGlassCatalog.cxx:33
 AGlassCatalog.cxx:34
 AGlassCatalog.cxx:35
 AGlassCatalog.cxx:36
 AGlassCatalog.cxx:37
 AGlassCatalog.cxx:38
 AGlassCatalog.cxx:39
 AGlassCatalog.cxx:40
 AGlassCatalog.cxx:41
 AGlassCatalog.cxx:42
 AGlassCatalog.cxx:43
 AGlassCatalog.cxx:44
 AGlassCatalog.cxx:45
 AGlassCatalog.cxx:46
 AGlassCatalog.cxx:47
 AGlassCatalog.cxx:48
 AGlassCatalog.cxx:49
 AGlassCatalog.cxx:50
 AGlassCatalog.cxx:51
 AGlassCatalog.cxx:52
 AGlassCatalog.cxx:53
 AGlassCatalog.cxx:54
 AGlassCatalog.cxx:55
 AGlassCatalog.cxx:56
 AGlassCatalog.cxx:57
 AGlassCatalog.cxx:58
 AGlassCatalog.cxx:59
 AGlassCatalog.cxx:60
 AGlassCatalog.cxx:61
 AGlassCatalog.cxx:62
 AGlassCatalog.cxx:63
 AGlassCatalog.cxx:64
 AGlassCatalog.cxx:65
 AGlassCatalog.cxx:66
 AGlassCatalog.cxx:67
 AGlassCatalog.cxx:68
 AGlassCatalog.cxx:69
 AGlassCatalog.cxx:70
 AGlassCatalog.cxx:71
 AGlassCatalog.cxx:72
 AGlassCatalog.cxx:73
 AGlassCatalog.cxx:74
 AGlassCatalog.cxx:75
 AGlassCatalog.cxx:76
 AGlassCatalog.cxx:77
 AGlassCatalog.cxx:78
 AGlassCatalog.cxx:79
 AGlassCatalog.cxx:80
 AGlassCatalog.cxx:81
 AGlassCatalog.cxx:82
 AGlassCatalog.cxx:83
 AGlassCatalog.cxx:84
 AGlassCatalog.cxx:85
 AGlassCatalog.cxx:86
 AGlassCatalog.cxx:87
 AGlassCatalog.cxx:88
 AGlassCatalog.cxx:89
 AGlassCatalog.cxx:90
 AGlassCatalog.cxx:91
 AGlassCatalog.cxx:92
 AGlassCatalog.cxx:93
 AGlassCatalog.cxx:94
 AGlassCatalog.cxx:95
 AGlassCatalog.cxx:96
 AGlassCatalog.cxx:97
 AGlassCatalog.cxx:98
 AGlassCatalog.cxx:99
 AGlassCatalog.cxx:100
 AGlassCatalog.cxx:101
 AGlassCatalog.cxx:102
 AGlassCatalog.cxx:103
 AGlassCatalog.cxx:104
 AGlassCatalog.cxx:105
 AGlassCatalog.cxx:106
 AGlassCatalog.cxx:107
 AGlassCatalog.cxx:108
 AGlassCatalog.cxx:109
 AGlassCatalog.cxx:110
 AGlassCatalog.cxx:111
 AGlassCatalog.cxx:112
 AGlassCatalog.cxx:113
 AGlassCatalog.cxx:114
 AGlassCatalog.cxx:115
 AGlassCatalog.cxx:116
 AGlassCatalog.cxx:117
 AGlassCatalog.cxx:118
 AGlassCatalog.cxx:119
 AGlassCatalog.cxx:120
 AGlassCatalog.cxx:121