CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/MuonAnalysis/MomentumScaleCalibration/bin/ZntupleToTreeConverter.cc

Go to the documentation of this file.
00001 #include <memory>
00002 #include <string>
00003 #include <vector>
00004 #include <sstream>
00005 #include <fstream>
00006 #include <iostream>
00007 
00008 #include <TH1F.h>
00009 #include <TROOT.h>
00010 #include <TFile.h>
00011 #include <TSystem.h>
00012 
00013 #include "DataFormats/Common/interface/Handle.h"
00014 #include "DataFormats/FWLite/interface/Event.h"
00015 #include "DataFormats/PatCandidates/interface/Muon.h"
00016 #include "FWCore/FWLite/interface/AutoLibraryLoader.h"
00017 #include "DataFormats/FWLite/interface/Handle.h"
00018 #include "DataFormats/Common/interface/Handle.h"
00019 #include "PhysicsTools/FWLite/interface/TFileService.h"
00020 #include "MuonAnalysis/MomentumScaleCalibration/interface/RootTreeHandler.h"
00021 
00022 // Useful function to convert 4-vector coordinates
00023 // -----------------------------------------------
00024 lorentzVector fromPtEtaPhiToPxPyPz( const double* ptEtaPhiE )
00025 {
00026   double muMass = 0.105658;
00027   double px = ptEtaPhiE[0]*cos(ptEtaPhiE[2]);
00028   double py = ptEtaPhiE[0]*sin(ptEtaPhiE[2]);
00029   double tmp = 2*atan(exp(-ptEtaPhiE[1]));
00030   double pz = ptEtaPhiE[0]*cos(tmp)/sin(tmp);
00031   double E  = sqrt(px*px+py*py+pz*pz+muMass*muMass);
00032 
00033   // lorentzVector corrMu(px,py,pz,E);
00034   // To fix memory leaks, this is to be substituted with
00035   // std::auto_ptr<lorentzVector> corrMu(new lorentzVector(px, py, pz, E));
00036 
00037   return lorentzVector(px,py,pz,E);
00038 }
00039 
00040 int main(int argc, char* argv[]) 
00041 {
00042 
00043   if( argc != 2 ) {
00044     std::cout << "Please provide the name of the file with file: or rfio: as needed" << std::endl;
00045     exit(1);
00046   }
00047   std::string fileName(argv[1]);
00048   if( fileName.find("file:") != 0 && fileName.find("rfio:") != 0 ) {
00049     std::cout << "Please provide the name of the file with file: or rfio: as needed" << std::endl;
00050     exit(1);
00051   }
00052 
00053   // ----------------------------------------------------------------------
00054   // First Part:
00055   //
00056   //  * enable the AutoLibraryLoader 
00057   //  * book the histograms of interest 
00058   //  * open the input file
00059   // ----------------------------------------------------------------------
00060 
00061   // load framework libraries
00062   gSystem->Load( "libFWCoreFWLite" );
00063   AutoLibraryLoader::enable();
00064   
00065   // book a set of histograms
00066   fwlite::TFileService fs = fwlite::TFileService("analyzeBasics.root");
00067   TFileDirectory theDir = fs.mkdir("analyzeBasic");
00068   TH1F* muonPt_  = theDir.make<TH1F>("muonPt", "pt",    100,  0.,300.);
00069   TH1F* muonEta_ = theDir.make<TH1F>("muonEta","eta",   100, -3.,  3.);
00070   TH1F* muonPhi_ = theDir.make<TH1F>("muonPhi","phi",   100, -5.,  5.);  
00071   
00072   // open input file (can be located on castor)
00073   TFile* inFile = TFile::Open(fileName.c_str());
00074 
00075 //   TFile* inFile = TFile::Open("rfio:/castor/cern.ch/user/f/fabozzi/36XSkimData/run_139791-140159/NtupleLoose_139791-140159_v2.root");
00076 //   TFile* inFile = TFile::Open("rfio:/castor/cern.ch/user/f/fabozzi/36XSkimData/run_140160-140182/NtupleLoose_140160-140182.root");
00077 //   TFile* inFile = TFile::Open("rfio:/castor/cern.ch/user/f/fabozzi/36XSkimData/run_140183-140399/NtupleLoose_140183-140399.root");
00078 //   TFile* inFile = TFile::Open("rfio:/castor/cern.ch/user/d/degrutto/36XSkimData/run_140440-141961/NtupleLoose_140440-141961.root");
00079 //   TFile* inFile = TFile::Open("rfio:/castor/cern.ch/user/d/degrutto/36XSkimData/run_142035-142664/NtupleLoose_142035-142664.root");
00080 
00081 
00082   // ----------------------------------------------------------------------
00083   // Second Part: 
00084   //
00085   //  * loop the events in the input file 
00086   //  * receive the collections of interest via fwlite::Handle
00087   //  * fill the histograms
00088   //  * after the loop close the input file
00089   // ----------------------------------------------------------------------
00090 
00091   // Create the RootTreeHandler to save the events in the root tree
00092   RootTreeHandler treeHandler;
00093   // MuonPairVector pairVector;
00094   std::vector<MuonPair> pairVector;
00095 
00096   // loop the events
00097   unsigned int iEvent=0;
00098   fwlite::Event ev(inFile);
00099   for(ev.toBegin(); !ev.atEnd(); ++ev, ++iEvent){
00100     
00101     // simple event counter
00102     if(iEvent>0 && iEvent%100==0){
00103       std::cout << "  processing event: " << iEvent << std::endl;
00104     }
00105 
00106     // Handle to the muon collection
00107     fwlite::Handle<std::vector<float> > muon1pt;
00108     fwlite::Handle<std::vector<float> > muon1eta;
00109     fwlite::Handle<std::vector<float> > muon1phi;
00110     fwlite::Handle<std::vector<float> > muon2pt;
00111     fwlite::Handle<std::vector<float> > muon2eta;
00112     fwlite::Handle<std::vector<float> > muon2phi;
00113     muon1pt.getByLabel(ev, "goodZToMuMuEdmNtupleLoose", "zGoldenDau1Pt");
00114     muon1eta.getByLabel(ev, "goodZToMuMuEdmNtupleLoose", "zGoldenDau1Eta");
00115     muon1phi.getByLabel(ev, "goodZToMuMuEdmNtupleLoose", "zGoldenDau1Phi");
00116     muon2pt.getByLabel(ev, "goodZToMuMuEdmNtupleLoose", "zGoldenDau2Pt");
00117     muon2eta.getByLabel(ev, "goodZToMuMuEdmNtupleLoose", "zGoldenDau2Eta");
00118     muon2phi.getByLabel(ev, "goodZToMuMuEdmNtupleLoose", "zGoldenDau2Phi");
00119 
00120     if( !muon1pt.isValid() ) continue;
00121     if( !muon1eta.isValid() ) continue;
00122     if( !muon1phi.isValid() ) continue;
00123     if( !muon2pt.isValid() ) continue;
00124     if( !muon2eta.isValid() ) continue;
00125     if( !muon2phi.isValid() ) continue;
00126     // std::cout << "muon1pt = " << muon1pt->size() << std::endl;
00127 
00128     // loop muon collection and fill histograms
00129     if( muon1pt->size() != muon2pt->size() ) {
00130       std::cout << "Error: size of muon1 and muon2 is different. Skipping event" << std::endl;
00131       continue;
00132     }
00133     for(unsigned i=0; i<muon1pt->size(); ++i){
00134       muonPt_->Fill( (*muon1pt)[i] );
00135       muonEta_->Fill( (*muon1eta)[i] );
00136       muonPhi_->Fill( (*muon1phi)[i] );
00137       muonPt_->Fill( (*muon2pt)[i] );
00138       muonEta_->Fill( (*muon2eta)[i] );
00139       muonPhi_->Fill( (*muon2phi)[i] );
00140 
00141       double muon1[3] = {(*muon1pt)[i], (*muon1eta)[i], (*muon1phi)[i]};
00142       double muon2[3] = {(*muon2pt)[i], (*muon2eta)[i], (*muon2phi)[i]};
00143 
00144       // pairVector.push_back( std::make_pair( fromPtEtaPhiToPxPyPz(muon1), fromPtEtaPhiToPxPyPz(muon2) ) );
00145       pairVector.push_back( MuonPair(fromPtEtaPhiToPxPyPz(muon1), fromPtEtaPhiToPxPyPz(muon2), 0, 0 ) );
00146     }
00147   }
00148   size_t namePos = fileName.find_last_of("/");
00149   treeHandler.writeTree(("tree_"+fileName.substr(namePos+1, fileName.size())).c_str(), &pairVector);
00150 
00151   // close input file
00152   inFile->Close();
00153 
00154   // ----------------------------------------------------------------------
00155   // Third Part: 
00156   //
00157   //  * never forget to free the memory of objects you created
00158   // ----------------------------------------------------------------------
00159 
00160   // in this example there is nothing to do 
00161   
00162   // that's it!
00163   return 0;
00164 }