Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:31:28

0001 #include <memory>
0002 #include <string>
0003 #include <vector>
0004 #include <sstream>
0005 #include <fstream>
0006 #include <iostream>
0007 #include <cstdlib>
0008 
0009 #include <TH1F.h>
0010 #include <TROOT.h>
0011 #include <TFile.h>
0012 #include <TSystem.h>
0013 
0014 #include "FWCore/FWLite/interface/FWLiteEnabler.h"
0015 #include "MuonAnalysis/MomentumScaleCalibration/interface/RootTreeHandler.h"
0016 
0017 /**
0018  * Builds a tree from the dump in the TreeDump.txt file produced by the TreeDump macro.
0019  */
0020 
0021 // Useful function to convert 4-vector coordinates
0022 // -----------------------------------------------
0023 lorentzVector fromPtEtaPhiToPxPyPz(const double* ptEtaPhiE) {
0024   double muMass = 0.105658;
0025   double px = ptEtaPhiE[0] * cos(ptEtaPhiE[2]);
0026   double py = ptEtaPhiE[0] * sin(ptEtaPhiE[2]);
0027   double tmp = 2 * atan(exp(-ptEtaPhiE[1]));
0028   double pz = ptEtaPhiE[0] * cos(tmp) / sin(tmp);
0029   double E = sqrt(px * px + py * py + pz * pz + muMass * muMass);
0030 
0031   return lorentzVector(px, py, pz, E);
0032 }
0033 
0034 int main(int argc, char* argv[]) {
0035   if (argc != 3) {
0036     std::cout << "Please provide the name of the file and if there is generator information (0 is false)" << std::endl;
0037     exit(1);
0038   }
0039   std::string fileName(argv[1]);
0040   std::stringstream ss;
0041   ss << argv[2];
0042   bool genInfo = false;
0043   ss >> genInfo;
0044   std::cout << "Reading tree dump with genInfo = " << genInfo << std::endl;
0045 
0046   // load framework libraries
0047   gSystem->Load("libFWCoreFWLite");
0048   FWLiteEnabler::enable();
0049 
0050   // MuonPairVector pairVector;
0051   std::vector<MuonPair> pairVector;
0052   std::vector<GenMuonPair> genPairVector;
0053 
0054   // Create the RootTreeHandler to save the events in the root tree
0055   RootTreeHandler treeHandler;
0056 
0057   std::ifstream inputFile;
0058   inputFile.open(fileName.c_str());
0059 
0060   std::string line;
0061   double value[6];
0062   double genValue[6];
0063   // Read the information from a txt file
0064   while (!inputFile.eof()) {
0065     getline(inputFile, line);
0066     if (!line.empty()) {
0067       // std::cout << "line = " << line << std::endl;
0068       std::stringstream ss(line);
0069       for (int i = 0; i < 6; ++i) {
0070         ss >> value[i];
0071         // std::cout << "value["<<i<<"] = " << value[i] << std::endl;
0072       }
0073       pairVector.push_back(
0074           MuonPair(fromPtEtaPhiToPxPyPz(value), fromPtEtaPhiToPxPyPz(&(value[3])), MuScleFitEvent(0, 0, 0, 0, 0, 0)));
0075       if (genInfo) {
0076         for (int i = 0; i < 6; ++i) {
0077           ss >> genValue[i];
0078           // std::cout << "genValue["<<i<<"] = " << genValue[i] << std::endl;
0079         }
0080         genPairVector.push_back(GenMuonPair(fromPtEtaPhiToPxPyPz(genValue), fromPtEtaPhiToPxPyPz(&(genValue[3])), 0));
0081       }
0082     }
0083   }
0084   inputFile.close();
0085 
0086   if ((pairVector.size() != genPairVector.size()) && genInfo) {
0087     std::cout << "Error: the size of pairVector and genPairVector is different" << std::endl;
0088   }
0089 
0090   if (genInfo) {
0091     treeHandler.writeTree("TreeFromDump.root", &pairVector, 0, &genPairVector);
0092     std::cout << "Filling tree with genInfo" << std::endl;
0093   } else {
0094     treeHandler.writeTree("TreeFromDump.root", &pairVector);
0095     std::cout << "Filling tree" << std::endl;
0096   }
0097   // close input file
0098   inputFile.close();
0099 
0100   return 0;
0101 }