Utils.cc
1 /*
2  * MoMEMta: a modular implementation of the Matrix Element Method
3  * Copyright (C) 2016 Universite catholique de Louvain (UCL), Belgium
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <random>
20 
21 #include <momemta/Utils.h>
22 #include <momemta/Math.h>
23 
24 LorentzVector getRandom4Vector(double maxE, double m/*=0*/) {
25  static std::mt19937_64 generator;
26  std::uniform_real_distribution<double> dist(-maxE, maxE);
27 
28  while (true) {
29  const double px = dist(generator);
30  const double py = dist(generator);
31  const double pz = dist(generator);
32  const double E = std::sqrt(SQ(m) + SQ(px) + SQ(py) + SQ(pz));
33  LorentzVector p4(px, py, pz, E);
34  if (E < maxE && p4.M() >= 0) {
35  return p4;
36  }
37  }
38 }
39 
40 #ifdef __GNUG__
41 #include <cstdlib>
42 #include <cxxabi.h>
43 #include <memory>
44 
45 std::string demangle(const char* name) {
46 
47  int status = -4; // some arbitrary value to eliminate the compiler warning
48 
49  std::unique_ptr<char, void (*)(void*)> res{abi::__cxa_demangle(name, NULL, NULL, &status),
50  std::free};
51 
52  return (status == 0) ? res.get() : name;
53 }
54 
55 #else
56 
57 // does nothing if not g++
58 std::string demangle(const char* name) { return name; }
59 
60 #endif
Mathematical functions.
#define SQ(x)
Compute .
Definition: Math.h:25