Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:22:36

0001 #include <cstdlib>
0002 
0003 #include <TH1F.h>
0004 #include <TROOT.h>
0005 #include <TFile.h>
0006 #include <TSystem.h>
0007 #include <sstream>
0008 #include <fstream>
0009 
0010 #include "FWCore/FWLite/interface/FWLiteEnabler.h"
0011 #include "MuonAnalysis/MomentumScaleCalibration/interface/RootTreeHandler.h"
0012 
0013 /**
0014  * Dumps the content of a tree to a local TreeDump.txt file. <br>
0015  * The txt file contains one pair per row and the values are: <br>
0016  * - for genInfo = 0 <br>
0017  * pt1 eta1 phi1 pt2 eta2 phi2 <br>
0018  * - for genInfo != 0 <br>
0019  * pt1 eta1 phi1 pt2 eta2 phi2 genPt1 genEta1 genPhi1 genPt2 genEta2 genPhi2.
0020  */
0021 
0022 // Useful function to convert 4-vector coordinates
0023 // -----------------------------------------------
0024 lorentzVector fromPtEtaPhiToPxPyPz(const double* ptEtaPhiE) {
0025   double muMass = 0.105658;
0026   double px = ptEtaPhiE[0] * cos(ptEtaPhiE[2]);
0027   double py = ptEtaPhiE[0] * sin(ptEtaPhiE[2]);
0028   double tmp = 2 * atan(exp(-ptEtaPhiE[1]));
0029   double pz = ptEtaPhiE[0] * cos(tmp) / sin(tmp);
0030   double E = sqrt(px * px + py * py + pz * pz + muMass * muMass);
0031 
0032   return lorentzVector(px, py, pz, E);
0033 }
0034 
0035 int main(int argc, char* argv[]) {
0036   if (argc != 3) {
0037     std::cout << "Please provide the name of the file (with file: or rfio: as needed) and if there is generator "
0038                  "information (0 is false)"
0039               << std::endl;
0040     exit(1);
0041   }
0042   std::string fileName(argv[1]);
0043   if (fileName.find("file:") != 0 && fileName.find("rfio:") != 0) {
0044     std::cout << "Please provide the name of the file with file: or rfio: as needed" << std::endl;
0045     exit(1);
0046   }
0047   std::stringstream ss;
0048   ss << argv[2];
0049   bool genInfo = false;
0050   ss >> genInfo;
0051   std::cout << "Dumping tree with genInfo = " << genInfo << std::endl;
0052 
0053   // load framework libraries
0054   gSystem->Load("libFWCoreFWLite");
0055   FWLiteEnabler::enable();
0056 
0057   // open input file (can be located on castor)
0058   TFile* inFile = TFile::Open(fileName.c_str());
0059 
0060   // MuonPairVector pairVector;
0061   MuonPairVector pairVector;
0062   MuonPairVector genPairVector;
0063 
0064   // Create the RootTreeHandler to save the events in the root tree
0065   RootTreeHandler treeHandler;
0066   // treeHandler.readTree(-1, fileName, &pairVector, &genPairVector);
0067   std::vector<std::pair<unsigned int, unsigned long long> > evtRun;
0068   treeHandler.readTree(-1, fileName, &pairVector, -20, &evtRun, &genPairVector);
0069 
0070   if ((pairVector.size() != genPairVector.size()) && genInfo) {
0071     std::cout << "Error: the size of pairVector and genPairVector is different" << std::endl;
0072   }
0073 
0074   std::ofstream outputFile;
0075   outputFile.open("TreeDump.txt");
0076 
0077   MuonPairVector::const_iterator it = pairVector.begin();
0078   MuonPairVector::const_iterator genIt = genPairVector.begin();
0079   std::vector<std::pair<unsigned int, unsigned long long> >::iterator evtRunIt = evtRun.begin();
0080   for (; it != pairVector.end(); ++it, ++genIt, ++evtRunIt) {
0081     // Write the information to a txt file
0082     outputFile << it->first.pt() << " " << it->first.eta() << " " << it->first.phi() << " " << it->second.pt() << " "
0083                << it->second.eta() << " " << it->second.phi() << " ";
0084     if (genInfo) {
0085       outputFile << genIt->first.pt() << " " << genIt->first.eta() << " " << genIt->first.phi() << " "
0086                  << genIt->second.pt() << " " << genIt->second.eta() << " " << genIt->second.phi() << " ";
0087     }
0088     outputFile << " " << evtRunIt->first << " " << evtRunIt->second;
0089     outputFile << std::endl;
0090   }
0091 
0092   // size_t namePos = fileName.find_last_of("/");
0093   // treeHandler.writeTree(("tree_"+fileName.substr(namePos+1, fileName.size())).c_str(), &pairVector);
0094 
0095   // close input and output files
0096   inFile->Close();
0097   outputFile.close();
0098 
0099   return 0;
0100 }