UPIDConverter.cxx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // UNI headers
  2. #include "UPIDConverter.h"
  3. // ROOT headers
  4. #include "TROOT.h"
  5. #include "TSystem.h"
  6. // C++ headers
  7. #include <fstream>
  8. ClassImp(UPIDConverter);
  9. UPIDConverter* UPIDConverter::fgInstance = NULL;
  10. //_________________
  11. UPIDConverter::UPIDConverter() : TNamed("PIDConverter", "Any-to-PDG particle-ID converter") {
  12. if (fgInstance) {
  13. Warning("UPIDConverter", "object already instantiated");
  14. }
  15. else {
  16. fgInstance = this;
  17. gROOT->GetListOfSpecials()->Add(this);
  18. }
  19. }
  20. //_________________
  21. Int_t UPIDConverter::GetPDGCode(const Int_t pid, const EConvention pidType) {
  22. /* Trivial case first */
  23. if (pidType == ePDG)
  24. return pid;
  25. ConversionTableMap::const_iterator mIt = fConversionTables.find(pidType);
  26. /* Either pidType has been set to an unknown value or the conversion table
  27. for specified convention hasn't been loaded yet. Assume the latter and
  28. try to do so now; if we still cannot find the table afterwards then
  29. something is indeed wrong. */
  30. if (mIt == fConversionTables.end()) {
  31. LoadConversionTable(pidType);
  32. mIt = fConversionTables.find(pidType);
  33. if (mIt == fConversionTables.end()) {
  34. Error("GetPDGCode",
  35. "PID conversion table not available for convention=%u",
  36. pidType);
  37. return 0;
  38. }
  39. }
  40. const std::map<Int_t, Int_t>& conversionTable = mIt->second;
  41. std::map<Int_t, Int_t>::const_iterator pidIt = conversionTable.find(pid);
  42. if (pidIt == conversionTable.end()) {
  43. Warning("GetPDGCode", "PDG code not found for convention=%u, pid=%d",
  44. pidType, pid);
  45. return 0;
  46. }
  47. return pidIt->second;
  48. }
  49. //_________________
  50. UPIDConverter* UPIDConverter::Instance() {
  51. return (fgInstance) ? fgInstance : new UPIDConverter();
  52. }
  53. //_________________
  54. void UPIDConverter::LoadConversionTable(const EConvention pidType) {
  55. std::string dataFileName = "/input/";
  56. switch (pidType) {
  57. case (ePluto):
  58. dataFileName += "pluto_pdg.dat";
  59. break;
  60. case (eUrQMD):
  61. dataFileName += "urqmd_pdg.dat";
  62. break;
  63. case (eWerner):
  64. // VENUS, NEXUS, EPOS
  65. dataFileName += "werner_pdg.dat";
  66. break;
  67. default:
  68. Error("LoadConversionTable",
  69. "I do not know where to find conversion table for convention=%u",
  70. pidType);
  71. return;
  72. }
  73. std::string unigenBase = gSystem->Getenv("UNIGEN");
  74. unigenBase += dataFileName;
  75. const char* fullFileName = unigenBase.c_str();
  76. std::ifstream fin;
  77. fin.open(fullFileName);
  78. if (! fin.good()) {
  79. Error("LoadConversionTable",
  80. "Failed to open conversion-table file %s", fullFileName);
  81. return;
  82. }
  83. std::map<Int_t, Int_t>& conversionTable = fConversionTables[pidType];
  84. Int_t localPid;
  85. Int_t pdgPid;
  86. while (1) {
  87. // FIXME: we might want to make this more robust against malformed input
  88. if (fin.eof())
  89. break;
  90. fin >> localPid >> pdgPid;
  91. conversionTable[localPid] = pdgPid;
  92. }
  93. fin.close();
  94. return;
  95. }