Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:44:56

0001 // The following program dumps contents of an object catalog stored
0002 // in a "geners" binary metafile to the standard output
0003 
0004 #include "Alignment/Geners/interface/IOException.hh"
0005 #include <fstream>
0006 #include <iostream>
0007 #include <map>
0008 
0009 #include "Alignment/Geners/interface/CPP11_auto_ptr.hh"
0010 #include "Alignment/Geners/interface/CStringStream.hh"
0011 #include "Alignment/Geners/interface/CatalogIO.hh"
0012 #include "Alignment/Geners/interface/ContiguousCatalog.hh"
0013 
0014 #include "CmdLine.hh"
0015 
0016 using namespace gs;
0017 using namespace std;
0018 
0019 static void print_usage(const char *progname) {
0020   cout << "\nUsage: " << progname << " [-a] [-c] [-f] [-i] [-s] filename\n\n"
0021        << "This program prints the contents of \"geners\" catalog files to the "
0022           "standard\n"
0023        << "output. These files can be usually recognized by their \".gsbmf\" "
0024           "extension\n"
0025        << "(Generic Serialization Binary MetaFile). Normally, the program "
0026           "prints class\n"
0027        << "names, item names in the archive, and archive categories for all "
0028           "items in\n"
0029        << "the catalog. This default behavior can be modified with option "
0030           "switches.\n"
0031        << "The meaning of the switches is as follows:\n\n"
0032        << " -a   Print catalog annotations, if any.\n\n"
0033        << " -c   Print default archive compression mode.\n\n"
0034        << " -f   Full dump. Print complete info for each catalog entry.\n\n"
0035        << " -i   Include the catalog item ids into the printout.\n\n"
0036        << " -s   Print only the summary statistics for item types. If option "
0037           "\"-f\" is given\n"
0038        << "      together with \"-s\", the summary will be printed after the "
0039           "full dump.\n"
0040        << endl;
0041 }
0042 
0043 int main(int argc, char const *argv[]) {
0044   CmdLine cmdline(argc, argv);
0045 
0046   if (argc == 1) {
0047     print_usage(cmdline.progname());
0048     return 0;
0049   }
0050 
0051   std::string inputfile;
0052   bool printAnnotations = false;
0053   bool printCompressionMode = false;
0054   bool fullDump = false;
0055   bool printIds = false;
0056   bool summaryMode = false;
0057 
0058   try {
0059     printAnnotations = cmdline.has("-a");
0060     printCompressionMode = cmdline.has("-c");
0061     fullDump = cmdline.has("-f");
0062     printIds = cmdline.has("-i");
0063     summaryMode = cmdline.has("-s");
0064 
0065     cmdline.optend();
0066 
0067     if (cmdline.argc() != 1)
0068       throw CmdLineError("wrong number of command line arguments");
0069     cmdline >> inputfile;
0070   } catch (CmdLineError &e) {
0071     cerr << "Error in " << cmdline.progname() << ": " << e.str() << endl;
0072     print_usage(cmdline.progname());
0073     return 1;
0074   }
0075 
0076   ifstream in(inputfile.c_str(), ios_base::binary);
0077   if (!in.is_open()) {
0078     cerr << "Error: failed to open file \"" << inputfile << "\"" << endl;
0079     return 1;
0080   }
0081 
0082   unsigned compressionCode = 0, mergeLevel = 0;
0083   std::vector<std::string> annotations;
0084   CPP11_auto_ptr<ContiguousCatalog> cat;
0085   try {
0086     cat = CPP11_auto_ptr<ContiguousCatalog>(
0087         readBinaryCatalog<ContiguousCatalog>(in, &compressionCode, &mergeLevel, &annotations, true));
0088   } catch (std::exception &e) {
0089     cerr << "Failed to read catalog from file \"" << inputfile << "\". " << e.what() << endl;
0090     return 1;
0091   }
0092 
0093   if (printCompressionMode) {
0094     CStringStream::CompressionMode mode = static_cast<CStringStream::CompressionMode>(compressionCode);
0095     cout << "Default compression mode: " << CStringStream::compressionModeName(mode, false) << endl;
0096   }
0097   if (printAnnotations) {
0098     const unsigned nAnnotations = annotations.size();
0099     for (unsigned i = 0; i < nAnnotations; ++i) {
0100       if (i)
0101         cout << '\n';
0102       cout << "Annotation " << i << ": " << annotations[i] << endl;
0103     }
0104     if (!nAnnotations)
0105       cout << "This catalog does not have any annotations" << endl;
0106   }
0107 
0108   std::map<std::string, unsigned> typecount;
0109 
0110   const unsigned long long first = cat->smallestId();
0111   const unsigned long long last = cat->largestId();
0112   for (unsigned long long id = first; id <= last; ++id) {
0113     if (!cat->itemExists(id))
0114       continue;
0115 
0116     std::shared_ptr<const CatalogEntry> e = cat->retrieveEntry(id);
0117     if (fullDump) {
0118       if (id != first)
0119         cout << '\n';
0120       e->humanReadable(cout);
0121     } else if (!summaryMode) {
0122       if (printIds)
0123         cout << e->id() << "  ";
0124       cout << e->type().name() << "  " << '"' << e->name() << '"' << "  " << '"' << e->category() << '"' << endl;
0125     }
0126     if (summaryMode)
0127       typecount[e->type().name()]++;
0128   }
0129 
0130   if (summaryMode) {
0131     if (fullDump)
0132       cout << '\n';
0133     for (std::map<std::string, unsigned>::const_iterator it = typecount.begin(); it != typecount.end(); ++it)
0134       cout << it->second << ' ' << it->first << endl;
0135   }
0136 
0137   return 0;
0138 }