Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:15:32

0001 // -*- C++ -*-
0002 //
0003 // Package:    TestCompareDDDumpFiles
0004 // Class:      TestCompareDDDumpFiles
0005 //
0006 /**\class TestCompareDDDumpFiles TestCompareDDDumpFiles.cc test/TestCompareDDDumpFiles/src/TestCompareDDDumpFiles.cc
0007 
0008  Description: Compares two geoHistory dump files 
0009 
0010  Implementation:
0011      Read two files with a certain format and compare each line.  If lines are out of sync stop.
0012 **/
0013 //
0014 // Original Author:  Michael Case
0015 //         Created:  Thu Sep 10, 2009
0016 //
0017 //
0018 
0019 #include <fstream>
0020 #include <iostream>
0021 #include <sstream>
0022 #include <string>
0023 #include <vector>
0024 #include <cmath>
0025 #include <iomanip>
0026 
0027 #include "FWCore/Framework/interface/Frameworkfwd.h"
0028 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0029 #include "FWCore/Framework/interface/Event.h"
0030 #include "FWCore/Framework/interface/EventSetup.h"
0031 #include "FWCore/Framework/interface/MakerMacros.h"
0032 
0033 class TestCompareDDDumpFiles : public edm::one::EDAnalyzer<> {
0034 public:
0035   explicit TestCompareDDDumpFiles(const edm::ParameterSet&);
0036   ~TestCompareDDDumpFiles() override;
0037 
0038   void beginJob() override {}
0039   void analyze(edm::Event const&, edm::EventSetup const&) override;
0040   void endJob() override {}
0041 
0042 private:
0043   std::string fname1_;
0044   std::string fname2_;
0045   double tol_;
0046   std::ifstream f1_;
0047   std::ifstream f2_;
0048 };
0049 
0050 TestCompareDDDumpFiles::TestCompareDDDumpFiles(const edm::ParameterSet& ps)
0051     : fname1_(ps.getParameter<std::string>("dumpFile1")),
0052       fname2_(ps.getParameter<std::string>("dumpFile2")),
0053       tol_(ps.getUntrackedParameter<double>("tolerance", 0.000001)),
0054       f1_(fname1_.c_str(), std::ios::in),
0055       f2_(fname2_.c_str(), std::ios::in) {
0056   if (!f1_ || !f2_) {
0057     throw cms::Exception("MissingFileDDTest") << fname1_ << " and/or " << fname2_ << " do not exist.";
0058   }
0059 }
0060 
0061 TestCompareDDDumpFiles::~TestCompareDDDumpFiles() {
0062   f1_.close();
0063   f2_.close();
0064 }
0065 
0066 void TestCompareDDDumpFiles::analyze(const edm::Event&, const edm::EventSetup&) {
0067   std::string l1, l2, ts;
0068   std::vector<double> t1(3), t2(3), r1(9), r2(9);
0069   double diffv;
0070   while (!f1_.eof() && !f2_.eof()) {
0071     getline(f1_, l1, ',');
0072     getline(f2_, l2, ',');
0073     if (l1 != l2) {
0074       std::cout << "Lines don't match or are out of synchronization."
0075                 << "  The difference is much bigger than just the numbers,"
0076                 << " actual parts are missing or added.  This program "
0077                 << " does not handle this at this time... use diff first." << std::endl
0078                 << "[" << l1 << "]" << std::endl
0079                 << "[" << l2 << "]" << std::endl
0080                 << std::endl;
0081       break;
0082     }
0083     //     std::cout << "1================================" << std::endl;
0084     //     std::cout << "["<<l1 <<"]"<< std::endl
0085     //        << "["<<l2 <<"]"<< std::endl;
0086     //     std::cout << "================================" << std::endl;
0087 
0088     size_t i = 0;
0089     while (i < 3) {
0090       ts.clear();
0091       getline(f1_, ts, ',');
0092       //    std::cout << ts << std::endl;
0093       std::istringstream s1(ts);
0094       s1 >> t1[i++];
0095     }
0096 
0097     i = 0;
0098     while (i < 8) {
0099       ts.clear();
0100       getline(f1_, ts, ',');
0101       //    std::cout << ts << std::endl;
0102       std::istringstream s1(ts);
0103       s1 >> r1[i++];
0104     }
0105     ts.clear();
0106     getline(f1_, ts);
0107     std::istringstream s2(ts);
0108     s2 >> r1[8];
0109 
0110     i = 0;
0111     while (i < 3) {
0112       ts.clear();
0113       getline(f2_, ts, ',');
0114       //    std::cout << ts << std::endl;
0115       std::istringstream s1(ts);
0116       s1 >> t2[i++];
0117     }
0118 
0119     i = 0;
0120     while (i < 8) {
0121       ts.clear();
0122       getline(f2_, ts, ',');
0123       //    std::cout << ts << std::endl;
0124       std::istringstream s1(ts);
0125       s1 >> r2[i++];
0126     }
0127     ts.clear();
0128     getline(f2_, ts);
0129     std::istringstream s3(ts);
0130     s3 >> r2[8];
0131 
0132     //     std::cout << "2================================" << std::endl;
0133     //     std::cout << "["<<l1 <<"]"<< std::endl
0134     //        << "["<<l2 <<"]"<< std::endl;
0135     //     std::cout << "ts = " << ts << std::endl;
0136     //     std::cout << "================================" << std::endl;
0137     //      std::cout << "l1=" << l1 << std::endl;
0138     std::vector<bool> cerrorind;
0139     cerrorind.reserve(3);
0140     for (i = 0; i < 3; i++) {
0141       //       std::cout << std::setw(13)
0142       //        << std::setprecision(7) << std::fixed
0143       //        << "t1[" << i << "] = " << t1[i]
0144       //        << " t2[" << i << "] = " << t2[i] << std::endl;
0145       diffv = std::fabs(t1[i] - t2[i]);
0146       if (diffv > tol_)
0147         cerrorind[i] = true;
0148       else
0149         cerrorind[i] = false;
0150     }
0151     if (cerrorind[0] || cerrorind[1] || cerrorind[2])
0152       std::cout << l1;
0153     for (i = 0; i < 3; ++i) {
0154       diffv = std::fabs(t1[i] - t2[i]);
0155       if (cerrorind[i] && diffv > tol_) {
0156         std::cout << " coordinate ";
0157         if (i == 0)
0158           std::cout << "x ";
0159         else if (i == 1)
0160           std::cout << "y ";
0161         else if (i == 2)
0162           std::cout << "z ";
0163         std::cout << " is different by: " << std::setw(13) << std::setprecision(7) << std::fixed << diffv << " mm ";
0164       }
0165     }
0166     bool rerror(false);
0167     for (i = 0; i < 9; i++) {
0168       //       std::cout << "r1[" << i << "] = " << r1[i]
0169       //        << " r2[" << i << "] = " << r2[i] << std::endl;
0170       diffv = std::fabs(r1[i] - r2[i]);
0171       if (diffv > tol_)
0172         rerror = true;
0173     }
0174     if (rerror && !cerrorind[0] && !cerrorind[1] && !cerrorind[2]) {
0175       std::cout << l1 << " ";
0176     }
0177     if (rerror) {
0178       for (i = 0; i < 9; i++) {
0179         //  std::cout << "r1[" << i << "] = " << r1[i]
0180         //        << " r2[" << i << "] = " << r2[i] << std::endl;
0181         diffv = std::fabs(r1[i] - r2[i]);
0182         if (diffv > tol_) {
0183           //      std::cout << std::endl;
0184           std::cout << " index " << i << " of rotation matrix differs by " << std::setw(13) << std::setprecision(7)
0185                     << std::fixed << r1[i] - r2[i];
0186         }
0187       }
0188       std::cout << std::endl;
0189     } else if (cerrorind[0] || cerrorind[1] || cerrorind[2]) {
0190       std::cout << std::endl;
0191     }
0192     //     std::cout << "3================================" << std::endl;
0193     //     std::cout << "["<<l1 <<"]"<< std::endl
0194     //        << "["<<l2 <<"]"<< std::endl;
0195     //     std::cout << "================================" << std::endl;
0196     //       getline(f1_, l1, ',');
0197     //       getline(f2_, l2, ',');
0198     //     if ( f1_.peek() != 10 ) {
0199     //       std::cout << "not eol on f1_ doing an extra getline()" << std::endl;
0200     //       getline(f1_,ts);
0201     //       std::cout << "why this is left?" << ts << std::endl;
0202     //     }
0203     //     if ( f2_.peek() != 10 ) {
0204     //       std::cout << "not eol on f2_ doing an extra getline()" << std::endl;
0205     //       getline(f2_,ts);
0206     //       std::cout << "why this is left?" << ts << std::endl;
0207     //     }
0208   }
0209 }
0210 //define this as a plug-in
0211 DEFINE_FWK_MODULE(TestCompareDDDumpFiles);