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 <TFile.h>
19 #include <TH1.h>
20 #include <Math/GenVector/LorentzVector.h>
21 #include <Math/GenVector/PtEtaPhiM4D.h>
22 #include <Math/Vector4Dfwd.h>
23 
24 using namespace ROOT::VecOps;
29 namespace hardware {
39  TFile *Open(std::string file, bool inTIMBER = true, const char *option = "READ");
49  TH1 *LoadHist(std::string filename, std::string histname, bool inTIMBER = true);
57  RVec<float> HadamardProduct(RVec<float> v1, RVec<float> v2);
68  RVec<float> HadamardProduct(RVec<float> v1, RVec<RVec<float>> v2, int v2subindex);
76  RVec<float> MultiHadamardProduct(RVec<float> v1, RVec<RVec<float>> Multiv2);
87  RVec<float> MultiHadamardProduct(RVec<float> v1, RVec<RVec<RVec<float>>> Multiv2, int v2subindex);
95  float DeltaPhi(float phi1,float phi2);
103  float DeltaR(ROOT::Math::PtEtaPhiMVector v1, ROOT::Math::PtEtaPhiMVector v2);
111  template<class T1, class T2>
112  float DeltaR(T1 in1, T2 in2) {
113  ROOT::Math::PtEtaPhiMVector v1(in1.pt, in1.eta, in1.phi, in1.mass);
114  ROOT::Math::PtEtaPhiMVector v2(in2.pt, in2.eta, in2.phi, in2.mass);
115  float deta = v1.Eta()-v2.Eta();
116  float dphi = DeltaPhi(v1.Phi(),v2.Phi());
117  return sqrt(deta*deta+dphi*dphi);
118  }
128  ROOT::Math::PtEtaPhiMVector TLvector(float pt,float eta,float phi,float m);
138  RVec<ROOT::Math::PtEtaPhiMVector> TLvector(RVec<float> pt,RVec<float> eta,RVec<float> phi,RVec<float> m);
145  template<class T>
146  ROOT::Math::PtEtaPhiMVector TLvector(T obj) {
147  ROOT::Math::PtEtaPhiMVector v (obj.pt, obj.eta, obj.phi, obj.mass);
148  return v;
149  }
156  template<class T>
157  RVec<ROOT::Math::PtEtaPhiMVector> TLvector(std::vector<T> objs) {
158  RVec<ROOT::Math::PtEtaPhiMVector> vs;
159  vs.reserve(objs.size());
160  for (size_t i = 0; i < objs.size(); i++) {
161  vs.emplace_back(objs[i].pt, objs[i].eta, objs[i].phi, objs[i].mass);
162  }
163  return vs;
164  }
165 
176  float TransverseMass(float MET_pt, float obj_pt, float MET_phi, float obj_phi);
186  double InvariantMass(RVec<ROOT::Math::PtEtaPhiMVector> vects);
193  template <class T>
194  RVec<RVec<T>> Transpose(RVec<RVec<T>> v) {
195  if (v.size() == 0) {
196  return RVec<RVec<T>> (0);
197  } else {
198  RVec<RVec<T>> out;
199  for (int i = 0; i < v[0].size(); i++) {
200  RVec<T> inner;
201  for (int j = 0; j < v.size(); j++) {
202  inner.push_back(v[j][i]);
203  }
204  out.push_back(inner);
205  }
206  return out;
207  }
208  }
209 }
210 
211 namespace Pythonic {
222  template <typename IntType>
223  std::vector<IntType> Range(IntType start, IntType stop, IntType step) {
224  if (step == IntType(0)) {
225  throw std::invalid_argument("step for range must be non-zero");
226  }
227 
228  std::vector<IntType> result;
229  IntType i = start;
230  while ((step > 0) ? (i < stop) : (i > stop)) {
231  result.push_back(i);
232  i += step;
233  }
234 
235  return result;
236  }
237 
247  template <typename IntType>
248  std::vector<IntType> Range(IntType start, IntType stop) {
249  return Range(start, stop, IntType(1));
250  }
251 
260  template <typename IntType>
261  std::vector<IntType> Range(IntType stop) {
262  return Range(IntType(0), stop, IntType(1));
263  }
264 
274  std::vector<std::string> Split(const std::string& str, char delim = ' ');
275 
276  // Personal
285  template<typename T>
286  int InList(T obj, std::vector<T> list) {
287  int out;
288  auto pos = std::find(std::begin(list), std::end(list), obj);
289  if (pos != std::end(list)){
290  out = pos - std::begin(list);
291  } else {out = -1;}
292  return out;
293  }
294 
303  bool InString(std::string sub, std::string main);
304 
312  template<typename T>
313  void Extend(std::vector<T> base, std::vector<T> extension) {
314  for (int i = 0; i < extension.size(); i++) {
315  base.push_back(extension.at(i));
316  }
317  }
318 
326  bool IsDir(char* dirname);
332  void Execute(std::string cmd);
333 }
334 
343 std::string ReadTarFile(std::string tarname, std::string internalFile);
344 
348 class TempDir {
349  private:
350  const boost::filesystem::path _path;
351  std::vector<std::string> _filesSaved;
352 
353  public:
358  TempDir();
363  ~TempDir();
372  std::string Write(std::string filename, std::string in);
378  std::string Hash();
379 
380 };
381 #endif
C++ namespace for common physics functions.
float DeltaR(ROOT::Math::PtEtaPhiMVector v1, ROOT::Math::PtEtaPhiMVector v2)
Calculate between two vectors.
Definition: common.cc:72
RVec< float > HadamardProduct(RVec< float > v1, RVec< float > v2)
Hadamard product of two vectors (v3[i] = v1[i]*v2[i])
Definition: common.cc:21
TH1 * LoadHist(std::string filename, std::string histname, bool inTIMBER=true)
Generically open a histogram from a file into memory (closing the file in the process).
Definition: common.cc:13
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:95
RVec< RVec< T > > Transpose(RVec< RVec< T >> v)
Transpose a vector so that output[j][i] = input[i][j].
Definition: common.h:194
TFile * Open(std::string file, bool inTIMBER=true, const char *option="READ")
Open a ROOT file inside the TIMBER path. Thsi function essentially just does the char + string format...
Definition: common.cc:5
RVec< float > MultiHadamardProduct(RVec< float > v1, RVec< RVec< float >> Multiv2)
Hadamard product of a base vector and a list of N more vectors (vout[i] = v1[i]*v2[i]*v3[i]...).
Definition: common.cc:39
float DeltaPhi(float phi1, float phi2)
Calculate the difference in .
Definition: common.cc:65
Definition: common.h:211
C++ class. Creates a temporary directory that is destroyed on delete.
Definition: common.h:348
ROOT::Math::PtEtaPhiMVector TLvector(float pt, float eta, float phi, float m)
Create a ROOT::Math::PtEtaPhiMVector.
Definition: common.cc:78
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:91