CMS 3D CMS Logo

TreeDump.cc
Go to the documentation of this file.
1 #include <cstdlib>
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  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  if (argc != 3) {
37  std::cout << "Please provide the name of the file (with file: or rfio: as needed) and if there is generator "
38  "information (0 is false)"
39  << std::endl;
40  exit(1);
41  }
42  std::string fileName(argv[1]);
43  if (fileName.find("file:") != 0 && fileName.find("rfio:") != 0) {
44  std::cout << "Please provide the name of the file with file: or rfio: as needed" << std::endl;
45  exit(1);
46  }
47  std::stringstream ss;
48  ss << argv[2];
49  bool genInfo = false;
50  ss >> genInfo;
51  std::cout << "Dumping tree with genInfo = " << genInfo << std::endl;
52 
53  // load framework libraries
54  gSystem->Load("libFWCoreFWLite");
56 
57  // open input file (can be located on castor)
58  TFile* inFile = TFile::Open(fileName.c_str());
59 
60  // MuonPairVector pairVector;
61  MuonPairVector pairVector;
62  MuonPairVector genPairVector;
63 
64  // Create the RootTreeHandler to save the events in the root tree
65  RootTreeHandler treeHandler;
66  // treeHandler.readTree(-1, fileName, &pairVector, &genPairVector);
67  std::vector<std::pair<unsigned int, unsigned long long> > evtRun;
68  treeHandler.readTree(-1, fileName, &pairVector, -20, &evtRun, &genPairVector);
69 
70  if ((pairVector.size() != genPairVector.size()) && genInfo) {
71  std::cout << "Error: the size of pairVector and genPairVector is different" << std::endl;
72  }
73 
74  std::ofstream outputFile;
75  outputFile.open("TreeDump.txt");
76 
77  MuonPairVector::const_iterator it = pairVector.begin();
78  MuonPairVector::const_iterator genIt = genPairVector.begin();
79  std::vector<std::pair<unsigned int, unsigned long long> >::iterator evtRunIt = evtRun.begin();
80  for (; it != pairVector.end(); ++it, ++genIt, ++evtRunIt) {
81  // Write the information to a txt file
82  outputFile << it->first.pt() << " " << it->first.eta() << " " << it->first.phi() << " " << it->second.pt() << " "
83  << it->second.eta() << " " << it->second.phi() << " ";
84  if (genInfo) {
85  outputFile << genIt->first.pt() << " " << genIt->first.eta() << " " << genIt->first.phi() << " "
86  << genIt->second.pt() << " " << genIt->second.eta() << " " << genIt->second.phi() << " ";
87  }
88  outputFile << " " << evtRunIt->first << " " << evtRunIt->second;
89  outputFile << std::endl;
90  }
91 
92  // size_t namePos = fileName.find_last_of("/");
93  // treeHandler.writeTree(("tree_"+fileName.substr(namePos+1, fileName.size())).c_str(), &pairVector);
94 
95  // close input and output files
96  inFile->Close();
97  outputFile.close();
98 
99  return 0;
100 }
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
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=nullptr)
static double constexpr muMass
Muon mass [GeV].
Definition: Constants.h:14
reco::Particle::LorentzVector lorentzVector
Definition: GenMuonPair.h:9
lorentzVector fromPtEtaPhiToPxPyPz(const double *ptEtaPhiE)
Definition: TreeDump.cc:24
int main(int argc, char *argv[])
Definition: TreeDump.cc:35
T sqrt(T t)
Definition: SSEVec.h:19
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
tmp
align.sh
Definition: createJobs.py:716
def exit(msg="")