CMS 3D CMS Logo

TreeFromDump.cc
Go to the documentation of this file.
1 #include <memory>
2 #include <string>
3 #include <vector>
4 #include <sstream>
5 #include <fstream>
6 #include <iostream>
7 #include <cstdlib>
8 
9 #include <TH1F.h>
10 #include <TROOT.h>
11 #include <TFile.h>
12 #include <TSystem.h>
13 
16 
21 // Useful function to convert 4-vector coordinates
22 // -----------------------------------------------
23 lorentzVector fromPtEtaPhiToPxPyPz(const double* ptEtaPhiE) {
24  double muMass = 0.105658;
25  double px = ptEtaPhiE[0] * cos(ptEtaPhiE[2]);
26  double py = ptEtaPhiE[0] * sin(ptEtaPhiE[2]);
27  double tmp = 2 * atan(exp(-ptEtaPhiE[1]));
28  double pz = ptEtaPhiE[0] * cos(tmp) / sin(tmp);
29  double E = sqrt(px * px + py * py + pz * pz + muMass * muMass);
30 
31  return lorentzVector(px, py, pz, E);
32 }
33 
34 int main(int argc, char* argv[]) {
35  if (argc != 3) {
36  std::cout << "Please provide the name of the file and if there is generator information (0 is false)" << std::endl;
37  exit(1);
38  }
40  std::stringstream ss;
41  ss << argv[2];
42  bool genInfo = false;
43  ss >> genInfo;
44  std::cout << "Reading tree dump with genInfo = " << genInfo << std::endl;
45 
46  // load framework libraries
47  gSystem->Load("libFWCoreFWLite");
49 
50  // MuonPairVector pairVector;
51  std::vector<MuonPair> pairVector;
52  std::vector<GenMuonPair> genPairVector;
53 
54  // Create the RootTreeHandler to save the events in the root tree
55  RootTreeHandler treeHandler;
56 
57  std::ifstream inputFile;
58  inputFile.open(fileName.c_str());
59 
61  double value[6];
62  double genValue[6];
63  // Read the information from a txt file
64  while (!inputFile.eof()) {
65  getline(inputFile, line);
66  if (!line.empty()) {
67  // std::cout << "line = " << line << std::endl;
68  std::stringstream ss(line);
69  for (int i = 0; i < 6; ++i) {
70  ss >> value[i];
71  // std::cout << "value["<<i<<"] = " << value[i] << std::endl;
72  }
73  pairVector.push_back(
75  if (genInfo) {
76  for (int i = 0; i < 6; ++i) {
77  ss >> genValue[i];
78  // std::cout << "genValue["<<i<<"] = " << genValue[i] << std::endl;
79  }
80  genPairVector.push_back(GenMuonPair(fromPtEtaPhiToPxPyPz(genValue), fromPtEtaPhiToPxPyPz(&(genValue[3])), 0));
81  }
82  }
83  }
84  inputFile.close();
85 
86  if ((pairVector.size() != genPairVector.size()) && genInfo) {
87  std::cout << "Error: the size of pairVector and genPairVector is different" << std::endl;
88  }
89 
90  if (genInfo) {
91  treeHandler.writeTree("TreeFromDump.root", &pairVector, 0, &genPairVector);
92  std::cout << "Filling tree with genInfo" << std::endl;
93  } else {
94  treeHandler.writeTree("TreeFromDump.root", &pairVector);
95  std::cout << "Filling tree" << std::endl;
96  }
97  // close input file
98  inputFile.close();
99 
100  return 0;
101 }
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
static double constexpr muMass
Muon mass [GeV].
Definition: Constants.h:14
reco::Particle::LorentzVector lorentzVector
Definition: GenMuonPair.h:9
void writeTree(const TString &fileName, const std::vector< MuonPair > *savedPair, const int muonType=0, const std::vector< GenMuonPair > *genPair=nullptr, const bool saveAll=false)
T sqrt(T t)
Definition: SSEVec.h:23
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
static void enable()
enable automatic library loading
Definition: value.py:1
int main(int argc, char *argv[])
Definition: TreeFromDump.cc:34
lorentzVector fromPtEtaPhiToPxPyPz(const double *ptEtaPhiE)
Definition: TreeFromDump.cc:23
tmp
align.sh
Definition: createJobs.py:716
def exit(msg="")