CMS 3D CMS Logo

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