CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch2/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   std::vector<std::pair<int, int> > evtRun;
00069   treeHandler.readTree(-1, fileName, &pairVector, -20, &evtRun, &genPairVector);
00070 
00071   if( (pairVector.size() != genPairVector.size()) && genInfo ) {
00072     std::cout << "Error: the size of pairVector and genPairVector is different" << std::endl;
00073   }
00074 
00075   ofstream outputFile;
00076   outputFile.open("TreeDump.txt");
00077 
00078   MuonPairVector::const_iterator it = pairVector.begin();
00079   MuonPairVector::const_iterator genIt = genPairVector.begin();
00080   std::vector<std::pair<int, int> >::iterator evtRunIt = evtRun.begin();
00081   for( ; it != pairVector.end(); ++it, ++genIt, ++evtRunIt ) {
00082     // Write the information to a txt file
00083     outputFile << it->first.pt()  << " " << it->first.eta()  << " " << it->first.phi()  << " "
00084                << it->second.pt() << " " << it->second.eta() << " " << it->second.phi() << " ";
00085     if( genInfo ) {
00086       outputFile << genIt->first.pt()  << " " << genIt->first.eta()  << " " << genIt->first.phi()  << " "
00087                  << genIt->second.pt() << " " << genIt->second.eta() << " " << genIt->second.phi() << " ";
00088     }
00089     outputFile << " " << evtRunIt->first << " " << evtRunIt->second;
00090     outputFile << std::endl;
00091   }
00092   
00093   // size_t namePos = fileName.find_last_of("/");
00094   // treeHandler.writeTree(("tree_"+fileName.substr(namePos+1, fileName.size())).c_str(), &pairVector);
00095 
00096   // close input and output files
00097   inFile->Close();
00098   outputFile.close();
00099 
00100   return 0;
00101 }