LHAPDF is hosted by Hepforge, IPPP Durham
LHAPDF  6.1.6
PDFIndex.h
1 // -*- C++ -*-
2 //
3 // This file is part of LHAPDF
4 // Copyright (C) 2012-2016 The LHAPDF collaboration (see AUTHORS for details)
5 //
6 #pragma once
7 #ifndef LHAPDF_PDFIndex_H
8 #define LHAPDF_PDFIndex_H
9 
10 #include "LHAPDF/Paths.h"
11 #include "LHAPDF/Utils.h"
12 #include "LHAPDF/Exceptions.h"
13 
14 namespace LHAPDF {
15 
16 
18 
19 
21  inline std::map<int, std::string>& getPDFIndex() {
22  static map<int, string> _lhaindex;
23  if (_lhaindex.empty()) { // The map needs to be populated first
24  string indexpath = findFile("pdfsets.index");
25  if (indexpath.empty()) throw ReadError("Could not find a pdfsets.index file");
26  try {
27  ifstream file(indexpath.c_str());
28  string line;
29  while (getline(file, line)) {
30  line = trim(line);
31  if (line.empty() || line.find("#") == 0) continue;
32  istringstream tokens(line);
33  int id; string setname;
34  tokens >> id;
35  tokens >> setname;
36  // cout << id << " -> " << _lhaindex[id] << endl;
37  _lhaindex[id] = setname;
38  }
39  } catch (const std::exception& ex) {
40  throw ReadError("Trouble when reading " + indexpath + ": " + ex.what());
41  }
42  }
43  return _lhaindex;
44  }
45 
46 
51  inline pair<std::string, int> lookupPDF(int lhaid) {
52  map<int, string>::iterator it = getPDFIndex().upper_bound(lhaid);
53  string rtnname = "";
54  int rtnmem = -1;
55  if (it != getPDFIndex().begin()) {
56  --it; // upper_bound (and lower_bound) return the entry *above* lhaid: we need to step back
57  rtnname = it->second; // name of the set that contains this ID
58  rtnmem = lhaid - it->first; // the member ID is the offset from the lookup ID
59  }
60  return make_pair(rtnname, rtnmem);
61  }
62 
63 
68  inline int lookupLHAPDFID(const std::string& setname, int nmem) {
69  // const map<int, string>& = getPDFIndex();
70  typedef pair<int, string> MapPair;
71  for (const MapPair& id_name : getPDFIndex()) {
72  if (id_name.second == setname) return id_name.first + nmem;
73  }
74  return -1; //< failure value
75  }
76 
77 
79  // inline int lookupLHAPDFID(const std::string& setname_nmem) {
80  // std::pair<std::string,int> idpair = decodePDFStr(setname_nmem);
81  // return lookupLHAPDFID(idpair.first, idpair.second);
82  // }
83 
85 
86 }
87 #endif
int lookupLHAPDFID(const std::string &setname, int nmem)
Definition: PDFIndex.h:68
Namespace for all LHAPDF functions and classes.
Definition: AlphaS.h:14
Error for file reading errors.
Definition: Exceptions.h:62
pair< std::string, int > lookupPDF(int lhaid)
Definition: PDFIndex.h:51
std::string trim(const std::string &s)
Strip leading and trailing spaces (not in-place)
Definition: Utils.h:130
std::string findFile(const std::string &target)
std::map< int, std::string > & getPDFIndex()
Get the singleton LHAPDF set ID -> PDF index map.
Definition: PDFIndex.h:21