Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:03:11

0001 #include <cassert>
0002 #include <iostream>
0003 #include <fstream>
0004 #include <string>
0005 #include <cstring>
0006 
0007 #include "CondTools/Hcal/interface/CmdLine.h"
0008 
0009 #include "CondFormats/Serialization/interface/eos/portable_iarchive.hpp"
0010 #include "CondFormats/Serialization/interface/eos/portable_oarchive.hpp"
0011 
0012 #include <boost/archive/text_iarchive.hpp>
0013 #include <boost/archive/text_oarchive.hpp>
0014 
0015 #include "CondTools/Hcal/interface/make_HFPhase1PMTParams.h"
0016 
0017 using namespace std;
0018 using namespace cmdline;
0019 
0020 static void print_usage(const char *progname) {
0021   cout << "\nUsage: " << progname << " code outputfile\n\n"
0022        << "Argument \"code\" must be 0, 1, 2, or 3:\n\n"
0023        << "  0 -- write parameters for data (calls make_HFPhase1PMTParams_data)\n"
0024        << "  1 -- write parameters for MC (calls make_HFPhase1PMTParams_mc)\n"
0025        << "  2 -- write dummy (all pass) parameters (calls make_HFPhase1PMTParams_dummy)\n"
0026        << "  3 -- write parameters for testing (calls make_HFPhase1PMTParams_test)\n"
0027        << endl;
0028 }
0029 
0030 int main(int argc, char *argv[]) {
0031   CmdLine cmdline(argc, argv);
0032 
0033   if (argc == 1) {
0034     print_usage(cmdline.progname());
0035     return 0;
0036   }
0037 
0038   unsigned code;
0039   string outputfile;
0040 
0041   try {
0042     cmdline.optend();
0043     if (cmdline.argc() != 2)
0044       throw CmdLineError("wrong number of command line arguments");
0045     cmdline >> code >> outputfile;
0046   } catch (const CmdLineError &e) {
0047     std::cerr << "Error in " << cmdline.progname() << ": " << e.str() << std::endl;
0048     return 1;
0049   }
0050 
0051   // Make the object
0052   std::unique_ptr<HFPhase1PMTParams> p1;
0053   if (code == 0)
0054     p1 = make_HFPhase1PMTParams_data();
0055   else if (code == 1)
0056     p1 = make_HFPhase1PMTParams_mc();
0057   else if (code == 2)
0058     p1 = make_HFPhase1PMTParams_dummy();
0059   else if (code == 3)
0060     p1 = make_HFPhase1PMTParams_test();
0061   else {
0062     cerr << "Error: invalid code \"" << code << "\"." << endl;
0063     print_usage(cmdline.progname());
0064     return 1;
0065   }
0066 
0067   // Are we using a text file as output?
0068   bool usingTxt = false;
0069   std::ios_base::openmode mode = std::ios::binary;
0070   {
0071     const unsigned outlen = strlen(outputfile.c_str());
0072     if (outlen >= 4)
0073       usingTxt = strcmp(".txt", outputfile.c_str() + outlen - 4) == 0;
0074     if (usingTxt)
0075       mode = std::ios_base::openmode();
0076   }
0077 
0078   // Write the object out
0079   {
0080     std::ofstream of(outputfile, mode);
0081     if (!of.is_open()) {
0082       cerr << "Failed to open file " << outputfile << endl;
0083       return 1;
0084     }
0085     if (usingTxt) {
0086       boost::archive::text_oarchive ar(of);
0087       ar &*p1;
0088     } else {
0089       eos::portable_oarchive ar(of);
0090       ar &*p1;
0091     }
0092   }
0093 
0094   // Read it back in
0095   HFPhase1PMTParams p2;
0096   {
0097     std::ifstream is(outputfile, mode);
0098     if (usingTxt) {
0099       boost::archive::text_iarchive ar(is);
0100       ar &p2;
0101     } else {
0102       eos::portable_iarchive ar(is);
0103       ar &p2;
0104     }
0105   }
0106 
0107   // Make sure that they are the same
0108   assert(*p1 == p2);
0109 
0110   return 0;
0111 }