test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TreeDump.cc
Go to the documentation of this file.
1 #include <stdlib.h>
2 
3 #include <TH1F.h>
4 #include <TROOT.h>
5 #include <TFile.h>
6 #include <TSystem.h>
7 #include <sstream>
8 #include <fstream>
9 
12 
22 // Useful function to convert 4-vector coordinates
23 // -----------------------------------------------
24 lorentzVector fromPtEtaPhiToPxPyPz( const double* ptEtaPhiE )
25 {
26  double muMass = 0.105658;
27  double px = ptEtaPhiE[0]*cos(ptEtaPhiE[2]);
28  double py = ptEtaPhiE[0]*sin(ptEtaPhiE[2]);
29  double tmp = 2*atan(exp(-ptEtaPhiE[1]));
30  double pz = ptEtaPhiE[0]*cos(tmp)/sin(tmp);
31  double E = sqrt(px*px+py*py+pz*pz+muMass*muMass);
32 
33  return lorentzVector(px,py,pz,E);
34 }
35 
36 int main(int argc, char* argv[])
37 {
38 
39  if( argc != 3 ) {
40  std::cout << "Please provide the name of the file (with file: or rfio: as needed) and if there is generator information (0 is false)" << std::endl;
41  exit(1);
42  }
43  std::string fileName(argv[1]);
44  if( fileName.find("file:") != 0 && fileName.find("rfio:") != 0 ) {
45  std::cout << "Please provide the name of the file with file: or rfio: as needed" << std::endl;
46  exit(1);
47  }
48  std::stringstream ss;
49  ss << argv[2];
50  bool genInfo = false;
51  ss >> genInfo;
52  std::cout << "Dumping tree with genInfo = " << genInfo << std::endl;
53 
54  // load framework libraries
55  gSystem->Load( "libFWCoreFWLite" );
57 
58  // open input file (can be located on castor)
59  TFile* inFile = TFile::Open(fileName.c_str());
60 
61  // MuonPairVector pairVector;
62  MuonPairVector pairVector;
63  MuonPairVector genPairVector;
64 
65  // Create the RootTreeHandler to save the events in the root tree
66  RootTreeHandler treeHandler;
67  // treeHandler.readTree(-1, fileName, &pairVector, &genPairVector);
68  std::vector<std::pair<unsigned int, unsigned long long> > evtRun;
69  treeHandler.readTree(-1, fileName, &pairVector, -20, &evtRun, &genPairVector);
70 
71  if( (pairVector.size() != genPairVector.size()) && genInfo ) {
72  std::cout << "Error: the size of pairVector and genPairVector is different" << std::endl;
73  }
74 
75  std::ofstream outputFile;
76  outputFile.open("TreeDump.txt");
77 
78  MuonPairVector::const_iterator it = pairVector.begin();
79  MuonPairVector::const_iterator genIt = genPairVector.begin();
80  std::vector<std::pair<unsigned int, unsigned long long> >::iterator evtRunIt = evtRun.begin();
81  for( ; it != pairVector.end(); ++it, ++genIt, ++evtRunIt ) {
82  // Write the information to a txt file
83  outputFile << it->first.pt() << " " << it->first.eta() << " " << it->first.phi() << " "
84  << it->second.pt() << " " << it->second.eta() << " " << it->second.phi() << " ";
85  if( genInfo ) {
86  outputFile << genIt->first.pt() << " " << genIt->first.eta() << " " << genIt->first.phi() << " "
87  << genIt->second.pt() << " " << genIt->second.eta() << " " << genIt->second.phi() << " ";
88  }
89  outputFile << " " << evtRunIt->first << " " << evtRunIt->second;
90  outputFile << std::endl;
91  }
92 
93  // size_t namePos = fileName.find_last_of("/");
94  // treeHandler.writeTree(("tree_"+fileName.substr(namePos+1, fileName.size())).c_str(), &pairVector);
95 
96  // close input and output files
97  inFile->Close();
98  outputFile.close();
99 
100  return 0;
101 }
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
reco::Particle::LorentzVector lorentzVector
Definition: GenMuonPair.h:9
lorentzVector fromPtEtaPhiToPxPyPz(const double *ptEtaPhiE)
Definition: TreeDump.cc:24
T sqrt(T t)
Definition: SSEVec.h:18
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
static void enable()
enable automatic library loading
std::vector< std::pair< lorentzVector, lorentzVector > > MuonPairVector
tuple argc
Definition: dir2webdir.py:38
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
tuple cout
Definition: gather_cfg.py:145
void readTree(const int maxEvents, const TString &fileName, MuonPairVector *savedPair, const int muonType, std::vector< std::pair< unsigned int, unsigned long long > > *evtRun, MuonPairVector *genPair=0)