CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/MuonAnalysis/MomentumScaleCalibration/bin/TreeFromDump.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 #include <stdlib.h>
00008 
00009 #include <TH1F.h>
00010 #include <TROOT.h>
00011 #include <TFile.h>
00012 #include <TSystem.h>
00013 
00014 #include "FWCore/FWLite/interface/AutoLibraryLoader.h"
00015 #include "MuonAnalysis/MomentumScaleCalibration/interface/RootTreeHandler.h"
00016 
00021 // Useful function to convert 4-vector coordinates
00022 // -----------------------------------------------
00023 lorentzVector fromPtEtaPhiToPxPyPz( const double* ptEtaPhiE )
00024 {
00025   double muMass = 0.105658;
00026   double px = ptEtaPhiE[0]*cos(ptEtaPhiE[2]);
00027   double py = ptEtaPhiE[0]*sin(ptEtaPhiE[2]);
00028   double tmp = 2*atan(exp(-ptEtaPhiE[1]));
00029   double pz = ptEtaPhiE[0]*cos(tmp)/sin(tmp);
00030   double E  = sqrt(px*px+py*py+pz*pz+muMass*muMass);
00031 
00032   return lorentzVector(px,py,pz,E);
00033 }
00034 
00035 int main(int argc, char* argv[]) 
00036 {
00037 
00038   if( argc != 3 ) {
00039     std::cout << "Please provide the name of the file and if there is generator information (0 is false)" << std::endl;
00040     exit(1);
00041   }
00042   std::string fileName(argv[1]);
00043   std::stringstream ss;
00044   ss << argv[2];
00045   bool genInfo = false;
00046   ss >> genInfo;
00047   std::cout << "Reading tree dump with genInfo = " << genInfo << std::endl;
00048 
00049   // load framework libraries
00050   gSystem->Load( "libFWCoreFWLite" );
00051   AutoLibraryLoader::enable();
00052   
00053   // MuonPairVector pairVector;
00054   std::vector<MuonPair> pairVector;
00055   std::vector<GenMuonPair> genPairVector;
00056 
00057   // Create the RootTreeHandler to save the events in the root tree
00058   RootTreeHandler treeHandler;
00059 
00060   ifstream inputFile;
00061   inputFile.open(fileName.c_str());
00062 
00063   std::string line;
00064   double value[6];
00065   double genValue[6];
00066   // Read the information from a txt file
00067   while( !inputFile.eof() ) {
00068     getline(inputFile, line);
00069     if( line != "" ) {
00070       // std::cout << "line = " << line << std::endl;
00071       std::stringstream ss(line);
00072       for( int i=0; i<6; ++i ) {
00073         ss >> value[i];
00074         // std::cout << "value["<<i<<"] = " << value[i] << std::endl;
00075       }
00076       pairVector.push_back(MuonPair(fromPtEtaPhiToPxPyPz(value), fromPtEtaPhiToPxPyPz(&(value[3])), 0, 0));
00077       if( genInfo ) {
00078         for( int i=0; i<6; ++i ) {
00079           ss >> genValue[i];
00080           // std::cout << "genValue["<<i<<"] = " << genValue[i] << std::endl;
00081         }
00082         genPairVector.push_back(GenMuonPair(fromPtEtaPhiToPxPyPz(genValue), fromPtEtaPhiToPxPyPz(&(genValue[3])), 0));
00083       }
00084     }
00085   }
00086   inputFile.close();
00087   
00088   if( (pairVector.size() != genPairVector.size()) && genInfo ) {
00089     std::cout << "Error: the size of pairVector and genPairVector is different" << std::endl;
00090   }
00091 
00092   if( genInfo ) {
00093     treeHandler.writeTree("TreeFromDump.root", &pairVector, 0, &genPairVector);
00094     std::cout << "Filling tree with genInfo" << std::endl;
00095   }
00096   else {
00097     treeHandler.writeTree("TreeFromDump.root", &pairVector);
00098     std::cout << "Filling tree" << std::endl;
00099   }
00100   // close input file
00101   inputFile.close();
00102 
00103   return 0;
00104 }