CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 <stdlib.h>
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 {
25  double muMass = 0.105658;
26  double px = ptEtaPhiE[0]*cos(ptEtaPhiE[2]);
27  double py = ptEtaPhiE[0]*sin(ptEtaPhiE[2]);
28  double tmp = 2*atan(exp(-ptEtaPhiE[1]));
29  double pz = ptEtaPhiE[0]*cos(tmp)/sin(tmp);
30  double E = sqrt(px*px+py*py+pz*pz+muMass*muMass);
31 
32  return lorentzVector(px,py,pz,E);
33 }
34 
35 int main(int argc, char* argv[])
36 {
37 
38  if( argc != 3 ) {
39  std::cout << "Please provide the name of the file and if there is generator information (0 is false)" << std::endl;
40  exit(1);
41  }
42  std::string fileName(argv[1]);
43  std::stringstream ss;
44  ss << argv[2];
45  bool genInfo = false;
46  ss >> genInfo;
47  std::cout << "Reading tree dump with genInfo = " << genInfo << std::endl;
48 
49  // load framework libraries
50  gSystem->Load( "libFWCoreFWLite" );
52 
53  // MuonPairVector pairVector;
54  std::vector<MuonPair> pairVector;
55  std::vector<GenMuonPair> genPairVector;
56 
57  // Create the RootTreeHandler to save the events in the root tree
58  RootTreeHandler treeHandler;
59 
60  std::ifstream inputFile;
61  inputFile.open(fileName.c_str());
62 
64  double value[6];
65  double genValue[6];
66  // Read the information from a txt file
67  while( !inputFile.eof() ) {
68  getline(inputFile, line);
69  if( line != "" ) {
70  // std::cout << "line = " << line << std::endl;
71  std::stringstream ss(line);
72  for( int i=0; i<6; ++i ) {
73  ss >> value[i];
74  // std::cout << "value["<<i<<"] = " << value[i] << std::endl;
75  }
76  pairVector.push_back(MuonPair(fromPtEtaPhiToPxPyPz(value), fromPtEtaPhiToPxPyPz(&(value[3])), MuScleFitEvent(0,0,0,0,0,0)) );
77  if( genInfo ) {
78  for( int i=0; i<6; ++i ) {
79  ss >> genValue[i];
80  // std::cout << "genValue["<<i<<"] = " << genValue[i] << std::endl;
81  }
82  genPairVector.push_back(GenMuonPair(fromPtEtaPhiToPxPyPz(genValue), fromPtEtaPhiToPxPyPz(&(genValue[3])), 0));
83  }
84  }
85  }
86  inputFile.close();
87 
88  if( (pairVector.size() != genPairVector.size()) && genInfo ) {
89  std::cout << "Error: the size of pairVector and genPairVector is different" << std::endl;
90  }
91 
92  if( genInfo ) {
93  treeHandler.writeTree("TreeFromDump.root", &pairVector, 0, &genPairVector);
94  std::cout << "Filling tree with genInfo" << std::endl;
95  }
96  else {
97  treeHandler.writeTree("TreeFromDump.root", &pairVector);
98  std::cout << "Filling tree" << std::endl;
99  }
100  // close input file
101  inputFile.close();
102 
103  return 0;
104 }
int i
Definition: DBlmapReader.cc:9
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
reco::Particle::LorentzVector lorentzVector
Definition: GenMuonPair.h:9
int main(int argc, char **argv)
lorentzVector fromPtEtaPhiToPxPyPz(const double *ptEtaPhiE)
Definition: TreeDump.cc:24
T sqrt(T t)
Definition: SSEVec.h:48
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
void writeTree(const TString &fileName, const std::vector< MuonPair > *savedPair, const int muonType=0, const std::vector< GenMuonPair > *genPair=0, const bool saveAll=false)
tuple argc
Definition: dir2webdir.py:38
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
static void enable()
enable automatic library loading
tuple cout
Definition: gather_cfg.py:121