CMS 3D CMS Logo

ZntupleToTreeConverter.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 
8 #include <TH1F.h>
9 #include <TROOT.h>
10 #include <TFile.h>
11 #include <TSystem.h>
12 
21 
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  // lorentzVector corrMu(px,py,pz,E);
33  // To fix memory leaks, this is to be substituted with
34  // std::unique_ptr<lorentzVector> corrMu(new lorentzVector(px, py, pz, E));
35 
36  return lorentzVector(px, py, pz, E);
37 }
38 
39 int main(int argc, char* argv[]) {
40  if (argc != 2) {
41  std::cout << "Please provide the name of the file with file: or rfio: as needed" << std::endl;
42  exit(1);
43  }
45  if (fileName.find("file:") != 0 && fileName.find("rfio:") != 0) {
46  std::cout << "Please provide the name of the file with file: or rfio: as needed" << std::endl;
47  exit(1);
48  }
49 
50  // ----------------------------------------------------------------------
51  // First Part:
52  //
53  // * enable FWLite
54  // * book the histograms of interest
55  // * open the input file
56  // ----------------------------------------------------------------------
57 
58  // load framework libraries
59  gSystem->Load("libFWCoreFWLite");
61 
62  // book a set of histograms
63  fwlite::TFileService fs = fwlite::TFileService("analyzeBasics.root");
64  TFileDirectory theDir = fs.mkdir("analyzeBasic");
65  TH1F* muonPt_ = theDir.make<TH1F>("muonPt", "pt", 100, 0., 300.);
66  TH1F* muonEta_ = theDir.make<TH1F>("muonEta", "eta", 100, -3., 3.);
67  TH1F* muonPhi_ = theDir.make<TH1F>("muonPhi", "phi", 100, -5., 5.);
68 
69  // open input file (can be located on castor)
70  TFile* inFile = TFile::Open(fileName.c_str());
71 
72  // TFile* inFile = TFile::Open("rfio:/castor/cern.ch/user/f/fabozzi/36XSkimData/run_139791-140159/NtupleLoose_139791-140159_v2.root");
73  // TFile* inFile = TFile::Open("rfio:/castor/cern.ch/user/f/fabozzi/36XSkimData/run_140160-140182/NtupleLoose_140160-140182.root");
74  // TFile* inFile = TFile::Open("rfio:/castor/cern.ch/user/f/fabozzi/36XSkimData/run_140183-140399/NtupleLoose_140183-140399.root");
75  // TFile* inFile = TFile::Open("rfio:/castor/cern.ch/user/d/degrutto/36XSkimData/run_140440-141961/NtupleLoose_140440-141961.root");
76  // TFile* inFile = TFile::Open("rfio:/castor/cern.ch/user/d/degrutto/36XSkimData/run_142035-142664/NtupleLoose_142035-142664.root");
77 
78  // ----------------------------------------------------------------------
79  // Second Part:
80  //
81  // * loop the events in the input file
82  // * receive the collections of interest via fwlite::Handle
83  // * fill the histograms
84  // * after the loop close the input file
85  // ----------------------------------------------------------------------
86 
87  // Create the RootTreeHandler to save the events in the root tree
88  RootTreeHandler treeHandler;
89  // MuonPairVector pairVector;
90  std::vector<MuonPair> pairVector;
91 
92  // loop the events
93  unsigned int iEvent = 0;
94  fwlite::Event ev(inFile);
95  for (ev.toBegin(); !ev.atEnd(); ++ev, ++iEvent) {
96  // simple event counter
97  if (iEvent > 0 && iEvent % 100 == 0) {
98  std::cout << " processing event: " << iEvent << std::endl;
99  }
100 
101  // Handle to the muon collection
108  muon1pt.getByLabel(ev, "goodZToMuMuEdmNtupleLoose", "zGoldenDau1Pt");
109  muon1eta.getByLabel(ev, "goodZToMuMuEdmNtupleLoose", "zGoldenDau1Eta");
110  muon1phi.getByLabel(ev, "goodZToMuMuEdmNtupleLoose", "zGoldenDau1Phi");
111  muon2pt.getByLabel(ev, "goodZToMuMuEdmNtupleLoose", "zGoldenDau2Pt");
112  muon2eta.getByLabel(ev, "goodZToMuMuEdmNtupleLoose", "zGoldenDau2Eta");
113  muon2phi.getByLabel(ev, "goodZToMuMuEdmNtupleLoose", "zGoldenDau2Phi");
114 
115  if (!muon1pt.isValid())
116  continue;
117  if (!muon1eta.isValid())
118  continue;
119  if (!muon1phi.isValid())
120  continue;
121  if (!muon2pt.isValid())
122  continue;
123  if (!muon2eta.isValid())
124  continue;
125  if (!muon2phi.isValid())
126  continue;
127  // std::cout << "muon1pt = " << muon1pt->size() << std::endl;
128 
129  // loop muon collection and fill histograms
130  if (muon1pt->size() != muon2pt->size()) {
131  std::cout << "Error: size of muon1 and muon2 is different. Skipping event" << std::endl;
132  continue;
133  }
134  for (unsigned i = 0; i < muon1pt->size(); ++i) {
135  muonPt_->Fill((*muon1pt)[i]);
136  muonEta_->Fill((*muon1eta)[i]);
137  muonPhi_->Fill((*muon1phi)[i]);
138  muonPt_->Fill((*muon2pt)[i]);
139  muonEta_->Fill((*muon2eta)[i]);
140  muonPhi_->Fill((*muon2phi)[i]);
141 
142  double muon1[3] = {(*muon1pt)[i], (*muon1eta)[i], (*muon1phi)[i]};
143  double muon2[3] = {(*muon2pt)[i], (*muon2eta)[i], (*muon2phi)[i]};
144 
145  // pairVector.push_back( std::make_pair( fromPtEtaPhiToPxPyPz(muon1), fromPtEtaPhiToPxPyPz(muon2) ) );
146  pairVector.push_back(
147  MuonPair(fromPtEtaPhiToPxPyPz(muon1), fromPtEtaPhiToPxPyPz(muon2), MuScleFitEvent(0, 0, 0, 0, 0, 0)));
148  }
149  }
150  size_t namePos = fileName.find_last_of('/');
151  treeHandler.writeTree(("tree_" + fileName.substr(namePos + 1, fileName.size())).c_str(), &pairVector);
152 
153  // close input file
154  inFile->Close();
155 
156  // ----------------------------------------------------------------------
157  // Third Part:
158  //
159  // * never forget to free the memory of objects you created
160  // ----------------------------------------------------------------------
161 
162  // in this example there is nothing to do
163 
164  // that's it!
165  return 0;
166 }
int main(int argc, char *argv[])
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
static double constexpr muMass
Muon mass [GeV].
Definition: Constants.h:14
bool isValid() const
Definition: Handle.h:60
reco::Particle::LorentzVector lorentzVector
Definition: GenMuonPair.h:9
T * make(const Args &...args) const
make new ROOT object
int iEvent
Definition: GenABIO.cc:224
lorentzVector fromPtEtaPhiToPxPyPz(const double *ptEtaPhiE)
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
void getByLabel(const P &iP, const char *iModuleLabel, const char *iProductInstanceLabel=nullptr, const char *iProcessLabel=nullptr)
Definition: Handle.h:100
tmp
align.sh
Definition: createJobs.py:716
def exit(msg="")