Utils.h
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 #pragma once
20 
21 #include <algorithm>
22 #include <string>
23 #include <vector>
24 
25 #include <momemta/Types.h>
26 
34 template <class T> std::vector<typename T::Scalar> toVector(const T& v) {
35  return {v.E(), v.Px(), v.Py(), v.Pz()};
36 }
37 
47 template <typename T>
48 std::vector<std::size_t> get_permutations(const std::vector<T>& from, const std::vector<T>& to) {
49 
50  std::vector<std::size_t> p(from.size());
51 
52  std::transform(from.begin(), from.end(), p.begin(), [&from, &to](const T& item) -> std::size_t {
53  return std::distance(to.begin(), std::find(to.begin(), to.end(), item));
54  });
55 
56  return p;
57 }
58 
67 template <typename T>
68 void apply_permutations(std::vector<T>& vec, std::vector<std::size_t> const& p) {
69  std::vector<T> sorted_vec(p.size());
70  std::transform(p.begin(), p.end(), sorted_vec.begin(), [&vec](int i) { return vec[i]; });
71 
72  vec = sorted_vec;
73 }
74 
83 LorentzVector getRandom4Vector(double maxE, double m=0);
84 
85 std::string demangle(const char* name);
86 
87 namespace cuba {
88 
89 inline unsigned int createFlagsBitset(char verbosity, bool subregion, bool retainStateFile,
90  unsigned int level, bool smoothing,
91  bool takeOnlyGridFromFile) {
92 
93  unsigned int flags = 0;
94 
95  static unsigned int opt_subregion = 0x04; // bit 2 (=4)
96  static unsigned int opt_smoothing = 0x08; // bit 3 (=8)
97  static unsigned int opt_retainStateFile = 0x10; // bit 4 (=16)
98  static unsigned int opt_takeOnlyGridFromFile = 0x20; // bit 5 (=32)
99 
100  level <<= 8; // bits 8-31
101  flags |= level | verbosity; // verbosity: bits 0-1
102  if (subregion)
103  flags |= opt_subregion;
104  if (!smoothing)
105  flags |= opt_smoothing; // careful true-false inverted
106  if (retainStateFile)
107  flags |= opt_retainStateFile;
108  if (takeOnlyGridFromFile)
109  flags |= opt_takeOnlyGridFromFile;
110 
111  return flags;
112 }
113 }
Definition: Utils.h:87