IAP GITLAB

CRMCinterface.cc 4.17 KB
Newer Older
Ralf Ulrich's avatar
Ralf Ulrich committed
1
#include <CRMCinterface.h>
Colin Baus's avatar
Colin Baus committed
2 3 4 5 6 7 8
#include <CRMCconfig.h>
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
using namespace std;
Ralf Ulrich's avatar
Ralf Ulrich committed
9 10 11

CRMCdata gCRMC_data;

Colin Baus's avatar
Colin Baus committed
12
CRMCinterface::CRMCinterface() :
13 14 15 16
  crmc_generate(NULL),
  crmc_set(NULL),
  crmc_init(NULL),
  crmc_xsection(NULL),
17
  fLibrary(NULL)
Colin Baus's avatar
Colin Baus committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31
{
}

CRMCinterface::~CRMCinterface()
{
  if (fLibrary)
    {
      dlclose(fLibrary);
      fLibrary = NULL;
    }
}

bool CRMCinterface::init(int HEmodel)
{
32 33 34 35 36 37
#ifdef __CRMCSTATIC__
  crmc_generate = &crmc_f_;
  crmc_set = &crmc_set_f_;
  crmc_init = &crmc_init_f_;
  crmc_xsection = &crmc_xsection_f_;
#else
Colin Baus's avatar
Colin Baus committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  ostringstream libname;
  if (!fLibrary)
    {
      //rpath is being checked for non absolute paths
      libname << "lib";
      switch (HEmodel)
        {
        case 0: libname << "Epos"; break;
        case 1: libname << "Epos"; break;
        case 2: libname << "Qgsjet01"; break;
        case 3: libname << "Gheisha"; break;
        case 4: libname << "Pythia"; break;
        case 5: libname << "Hijing"; break;
        case 6: libname << "Sibyll"; break;
#ifdef __QGSJETII04__
        case 7: libname << "QgsjetII04"; break;
#endif
        case 8: libname << "Phojet"; break;
56 57
#ifdef __QGSJETII03__
        case 11: libname << "QgsjetII03"; break;
58
#endif
59 60 61
#ifdef __DPMJET06__
        case 12: libname << "Dpmjet06"; break;
#endif
Tanguy Pierog's avatar
Tanguy Pierog committed
62 63 64 65 66 67
#ifdef __DPMJET17__
        case 12: libname << "Dpmjet17"; break;
#endif
#ifdef __DPMJET19__
        case 12: libname << "Dpmjet19"; break;
#endif
Colin Baus's avatar
Colin Baus committed
68 69 70 71
        default: libname << "UnknownModel"; break;
        }
      libname << ".so";
      fLibrary = dlopen(libname.str().c_str(), RTLD_NOW);
72
      cout << "Opening: " << libname.str() << endl;
Colin Baus's avatar
Colin Baus committed
73 74 75 76 77 78 79 80 81 82 83
      if (!fLibrary )
        {
          ostringstream errMsg;
          errMsg << "\n cannot open shared library " << libname.str() << "\'\n\n"
                 << " Dynamic-link error:\n \"" << dlerror() << "\"\n";

          cerr << errMsg.str() << endl;
          exit(1);
        }
    }

84
  crmc_generate = (void(*)( const int&, const int&, int&, double&, int&, double&,
85
                      double&, double&, double&, double&, int&)) dlsym(fLibrary, "crmc_f_");
86
  if(crmc_generate == NULL)
Colin Baus's avatar
Colin Baus committed
87 88 89 90 91 92 93 94
    {
      ostringstream errMsg;
      errMsg << " dlsym error:\n \"" << dlerror() << "\"\n";

      cerr << errMsg.str() << endl;
      exit(1);
    }

95
  crmc_set = (void(*)( const int&, const int&, const double&, const double&,
Colin Baus's avatar
Colin Baus committed
96
                           const int&, const int&, const int&, const int&,
97
                           const int&, const char*)) dlsym(fLibrary, "crmc_set_f_");
98
  if(crmc_set == NULL)
99 100 101 102 103 104 105 106
    {
      ostringstream errMsg;
      errMsg << " dlsym error:\n \"" << dlerror() << "\"\n";

      cerr << errMsg.str() << endl;
      exit(1);
    }

107
  crmc_init = (void(*)(const char*,const int&)) dlsym(fLibrary, "crmc_init_f_");
108
  if(crmc_init == NULL)
Colin Baus's avatar
Colin Baus committed
109 110 111 112 113 114 115
    {
      ostringstream errMsg;
      errMsg << " dlsym error:\n \"" << dlerror() << "\"\n";

      cerr << errMsg.str() << endl;
      exit(1);
    }
116
  
117
  crmc_xsection = (void(*)( double&, double&, double&, double&, double&,
Colin Baus's avatar
Colin Baus committed
118
                              double&, double&, double&, double&)) dlsym(fLibrary, "crmc_xsection_f_");
119
  if(crmc_xsection == NULL)
Colin Baus's avatar
Colin Baus committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    {
      ostringstream errMsg;
      errMsg << " dlsym error:\n \"" << dlerror() << "\"\n";

      cerr << errMsg.str() << endl;
      exit(1);
    }

  //common blocks from library are not used. they come from DummyHepEvt library
  //grabbed in header with extern "C"
  // cevt_  = (cevt*)  dlsym(fLibrary, "cevt_");
  // if(cevt_ == NULL)
  //   {
  //     ostringstream errMsg;
  //     errMsg << " dlsym error:\n \"" << dlerror() << "\"\n";

  //     cerr << errMsg.str() << endl;
  //     exit(1);
  //   }
  // c2evt_ = (c2evt*) dlsym(fLibrary, "c2evt_");
  // if(c2evt_ == NULL)
  //   {
  //     ostringstream errMsg;
  //     errMsg << " dlsym error:\n \"" << dlerror() << "\"\n";

  //     cerr << errMsg.str() << endl;
  //     exit(1);
  //   }
  // hadr5_ = (hadr5*) dlsym(fLibrary, "hadr5_");
  // if(hadr5_ == NULL)
  //   {
  //     ostringstream errMsg;
  //     errMsg << " dlsym error:\n \"" << dlerror() << "\"\n";

  //     cerr << errMsg.str() << endl;
  //     exit(1);
  //   }

158
#endif
Colin Baus's avatar
Colin Baus committed
159 160
  return 1;
}