TIMBER  beta
Tree Interface for Making Binned Events with RDataFrame
common.h
1 #ifndef _TIMBER_COMMON
2 #define _TIMBER_COMMON
3 
4 #include <fstream>
5 #include <string>
6 #include <iostream>
7 #include <sstream>
8 #include <algorithm>
9 #include <iterator>
10 #include <vector>
11 #include <stdexcept>
12 #include <boost/filesystem.hpp>
13 
14 #include <cmath>
15 #include <cstdlib>
16 #include <ROOT/RVec.hxx>
17 #include <TMath.h>
18 #include <Math/GenVector/LorentzVector.h>
19 #include <Math/GenVector/PtEtaPhiM4D.h>
20 #include <Math/Vector4Dfwd.h>
21 
22 using namespace ROOT::VecOps;
27 namespace hardware {
35  RVec<float> HadamardProduct(RVec<float> v1, RVec<float> v2);
46  RVec<float> HadamardProduct(RVec<float> v1, RVec<RVec<float>> v2, int v2subindex);
57  RVec<float> MultiHadamardProduct(RVec<float> v1, RVec<RVec<RVec<float>>> Multiv2, int v2subindex);
65  float DeltaPhi(float phi1,float phi2);
73  float DeltaR(ROOT::Math::PtEtaPhiMVector v1, ROOT::Math::PtEtaPhiMVector v2);
81  template<class T1, class T2>
82  float DeltaR(T1 in1, T2 in2) {
83  ROOT::Math::PtEtaPhiMVector v1(in1.pt, in1.eta, in1.phi, in1.mass);
84  ROOT::Math::PtEtaPhiMVector v2(in2.pt, in2.eta, in2.phi, in2.mass);
85  float deta = v1.Eta()-v2.Eta();
86  float dphi = DeltaPhi(v1.Phi(),v2.Phi());
87  return sqrt(deta*deta+dphi*dphi);
88  }
98  ROOT::Math::PtEtaPhiMVector TLvector(float pt,float eta,float phi,float m);
108  RVec<ROOT::Math::PtEtaPhiMVector> TLvector(RVec<float> pt,RVec<float> eta,RVec<float> phi,RVec<float> m);
115  template<class T>
116  ROOT::Math::PtEtaPhiMVector TLvector(T obj) {
117  ROOT::Math::PtEtaPhiMVector v (obj.pt, obj.eta, obj.phi, obj.mass);
118  return v;
119  }
126  template<class T>
127  RVec<ROOT::Math::PtEtaPhiMVector> TLvector(std::vector<T> objs) {
128  RVec<ROOT::Math::PtEtaPhiMVector> vs;
129  vs.reserve(objs.size());
130  for (size_t i = 0; i < objs.size(); i++) {
131  vs.emplace_back(objs[i].pt, objs[i].eta, objs[i].phi, objs[i].mass);
132  }
133  return vs;
134  }
135 
146  float TransverseMass(float MET_pt, float obj_pt, float MET_phi, float obj_phi);
156  double InvariantMass(RVec<ROOT::Math::PtEtaPhiMVector> vects);
163  template <class T>
164  RVec<RVec<T>> Transpose(RVec<RVec<T>> v) {
165  if (v.size() == 0) {
166  return RVec<RVec<T>> (0);
167  } else {
168  RVec<RVec<T>> out;
169  for (int i = 0; i < v[0].size(); i++) {
170  RVec<T> inner;
171  for (int j = 0; j < v.size(); j++) {
172  inner.push_back(v[j][i]);
173  }
174  out.push_back(inner);
175  }
176  return out;
177  }
178  }
179 }
180 
181 namespace Pythonic {
192  template <typename IntType>
193  std::vector<IntType> Range(IntType start, IntType stop, IntType step) {
194  if (step == IntType(0)) {
195  throw std::invalid_argument("step for range must be non-zero");
196  }
197 
198  std::vector<IntType> result;
199  IntType i = start;
200  while ((step > 0) ? (i < stop) : (i > stop)) {
201  result.push_back(i);
202  i += step;
203  }
204 
205  return result;
206  }
207 
217  template <typename IntType>
218  std::vector<IntType> Range(IntType start, IntType stop) {
219  return Range(start, stop, IntType(1));
220  }
221 
230  template <typename IntType>
231  std::vector<IntType> Range(IntType stop) {
232  return Range(IntType(0), stop, IntType(1));
233  }
234 
244  std::vector<std::string> Split(const std::string& str, char delim = ' ');
245 
246  // Personal
255  template<typename T>
256  bool InList(T obj, std::vector<T> list) {
257  bool out;
258  auto pos = std::find(std::begin(list), std::end(list), obj);
259  if (pos != std::end(list)){
260  out = true;
261  } else {out = false;}
262  return out;
263  }
264 
273  bool InString(std::string sub, std::string main);
274 
282  template<typename T>
283  void Extend(std::vector<T> base, std::vector<T> extension) {
284  for (int i = 0; i < extension.size(); i++) {
285  base.push_back(extension.at(i));
286  }
287  }
288 
296  bool IsDir(char* dirname);
302  void Execute(std::string cmd);
303 }
304 
313 std::string ReadTarFile(std::string tarname, std::string internalFile);
314 
318 class TempDir {
319  private:
320  const boost::filesystem::path _path;
321  std::vector<std::string> _filesSaved;
322 
323  public:
328  TempDir();
333  ~TempDir();
342  std::string Write(std::string filename, std::string in);
348  std::string Hash();
349 
350 };
351 #endif
C++ namespace for common physics functions.
float DeltaR(ROOT::Math::PtEtaPhiMVector v1, ROOT::Math::PtEtaPhiMVector v2)
Calculate between two vectors.
Definition: common.cc:43
RVec< float > HadamardProduct(RVec< float > v1, RVec< float > v2)
Hadamard product of two vectors (v3[i] = v1[i]*v2[i])
Definition: common.cc:5
double InvariantMass(RVec< ROOT::Math::PtEtaPhiMVector > vects)
Calculates the invariant mass of a vector of Lorentz vectors (ROOT::Math::PtEtaPhiMVector). Note that this is an alternative to ROOT::VecOps::InvariantMasses() which does not need the intermediate Lorentz vector.
Definition: common.cc:66
RVec< RVec< T > > Transpose(RVec< RVec< T >> v)
Transpose a vector so that output[j][i] = input[i][j].
Definition: common.h:164
float DeltaPhi(float phi1, float phi2)
Calculate the difference in .
Definition: common.cc:36
Definition: common.h:181
C++ class. Creates a temporary directory that is destroyed on delete.
Definition: common.h:318
RVec< float > MultiHadamardProduct(RVec< float > v1, RVec< RVec< RVec< float >>> Multiv2, int v2subindex)
Hadamard product of a base vector and a list of N more vectors (vout[i] = v1[i]*v2[i][v2subindex]*v3[...
Definition: common.cc:23
ROOT::Math::PtEtaPhiMVector TLvector(float pt, float eta, float phi, float m)
Create a ROOT::Math::PtEtaPhiMVector.
Definition: common.cc:49
float TransverseMass(float MET_pt, float obj_pt, float MET_phi, float obj_phi)
Calculate the transverse mass from MET and and an object&#39;s and .
Definition: common.cc:62