Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-07 04:35:45

0001 #include <iostream>
0002 #include <fstream>
0003 #include <cassert>
0004 #include <string>
0005 #include <cstring>
0006 
0007 #include "CondFormats/Serialization/interface/eos/portable_iarchive.hpp"
0008 #include "CondFormats/Serialization/interface/eos/portable_oarchive.hpp"
0009 
0010 #include "CondTools/Hcal/interface/make_HBHENegativeEFilter.h"
0011 
0012 #include <boost/archive/text_iarchive.hpp>
0013 #include <boost/archive/text_oarchive.hpp>
0014 
0015 using namespace std;
0016 
0017 static void print_usage(const char *progname) {
0018   cout << "\nUsage: " << progname << " which outputfile\n\n"
0019        << "Argument \"which\" must be 0 or 1:\n\n"
0020        << "  0 -- write Alexander Toropin's filter\n"
0021        << "  1 -- write a dummy (all pass) filter\n"
0022        << endl;
0023 }
0024 
0025 int main(int argc, char *argv[]) {
0026   // Parse input arguments
0027   if (argc != 3) {
0028     print_usage(argv[0]);
0029     return argc != 1;
0030   }
0031   const string which(argv[1]);
0032   const char *outputfile = argv[2];
0033 
0034   // Are we using a text file as output?
0035   bool usingTxt = false;
0036   std::ios_base::openmode mode = std::ios::binary;
0037   {
0038     const unsigned outlen = strlen(outputfile);
0039     if (outlen >= 4)
0040       usingTxt = strcmp(".txt", outputfile + outlen - 4) == 0;
0041     if (usingTxt)
0042       mode = std::ios_base::openmode();
0043   }
0044 
0045   // Make the filter
0046   std::unique_ptr<HBHENegativeEFilter> f1;
0047   if (which == "0")
0048     f1 = make_HBHENegativeEFilter();
0049   else if (which == "1")
0050     f1 = std::unique_ptr<HBHENegativeEFilter>(new HBHENegativeEFilter());
0051   else {
0052     cerr << "Error: invalid filter code \"" << which << "\"." << endl;
0053     print_usage(argv[0]);
0054     return 1;
0055   }
0056 
0057   // Write it out
0058   {
0059     std::ofstream of(outputfile, mode);
0060     if (!of.is_open()) {
0061       cerr << "Failed to open file " << outputfile << endl;
0062       return 1;
0063     }
0064     if (usingTxt) {
0065       boost::archive::text_oarchive ar(of);
0066       ar &*f1;
0067     } else {
0068       eos::portable_oarchive ar(of);
0069       ar &*f1;
0070     }
0071   }
0072 
0073   // Read it back in
0074   HBHENegativeEFilter f2;
0075   {
0076     std::ifstream is(outputfile, mode);
0077     if (usingTxt) {
0078       boost::archive::text_iarchive ar(is);
0079       ar & f2;
0080     } else {
0081       eos::portable_iarchive ar(is);
0082       ar & f2;
0083     }
0084   }
0085 
0086   // Make sure that they are the same
0087   assert(*f1 == f2);
0088 
0089   return 0;
0090 }