CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/MuonAnalysis/MomentumScaleCalibration/bin/TreeDump.cc

Go to the documentation of this file.
00001 #include <stdlib.h>
00002 
00003 #include <TH1F.h>
00004 #include <TROOT.h>
00005 #include <TFile.h>
00006 #include <TSystem.h>
00007 #include <sstream>
00008 #include <fstream>
00009 
00010 #include "FWCore/FWLite/interface/AutoLibraryLoader.h"
00011 #include "MuonAnalysis/MomentumScaleCalibration/interface/RootTreeHandler.h"
00012 
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   return lorentzVector(px,py,pz,E);
00034 }
00035 
00036 int main(int argc, char* argv[]) 
00037 {
00038 
00039   if( argc != 3 ) {
00040     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;
00041     exit(1);
00042   }
00043   std::string fileName(argv[1]);
00044   if( fileName.find("file:") != 0 && fileName.find("rfio:") != 0 ) {
00045     std::cout << "Please provide the name of the file with file: or rfio: as needed" << std::endl;
00046     exit(1);
00047   }
00048   std::stringstream ss;
00049   ss << argv[2];
00050   bool genInfo = false;
00051   ss >> genInfo;
00052   std::cout << "Dumping tree with genInfo = " << genInfo << std::endl;
00053 
00054   // load framework libraries
00055   gSystem->Load( "libFWCoreFWLite" );
00056   AutoLibraryLoader::enable();
00057   
00058   // open input file (can be located on castor)
00059   TFile* inFile = TFile::Open(fileName.c_str());
00060 
00061   // MuonPairVector pairVector;
00062   MuonPairVector pairVector;
00063   MuonPairVector genPairVector;
00064 
00065   // Create the RootTreeHandler to save the events in the root tree
00066   RootTreeHandler treeHandler;
00067   // treeHandler.readTree(-1, fileName, &pairVector, &genPairVector);
00068   treeHandler.readTree(-1, fileName, &pairVector, -20, &genPairVector);
00069 
00070   if( (pairVector.size() != genPairVector.size()) && genInfo ) {
00071     std::cout << "Error: the size of pairVector and genPairVector is different" << std::endl;
00072   }
00073 
00074   ofstream outputFile;
00075   outputFile.open("TreeDump.txt");
00076 
00077   MuonPairVector::const_iterator it = pairVector.begin();
00078   MuonPairVector::const_iterator genIt = genPairVector.begin();
00079   for( ; it != pairVector.end(); ++it, ++genIt ) {
00080     // Write the information to a txt file
00081     outputFile << it->first.pt()  << " " << it->first.eta()  << " " << it->first.phi()  << " "
00082                << it->second.pt() << " " << it->second.eta() << " " << it->second.phi() << " ";
00083     if( genInfo ) {
00084       outputFile << genIt->first.pt()  << " " << genIt->first.eta()  << " " << genIt->first.phi()  << " "
00085                  << genIt->second.pt() << " " << genIt->second.eta() << " " << genIt->second.phi() << " ";
00086     }
00087     outputFile << std::endl;
00088   }
00089   
00090   // size_t namePos = fileName.find_last_of("/");
00091   // treeHandler.writeTree(("tree_"+fileName.substr(namePos+1, fileName.size())).c_str(), &pairVector);
00092 
00093   // close input and output files
00094   inFile->Close();
00095   outputFile.close();
00096 
00097   return 0;
00098 }