Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:10:29

0001 /** Sample code to Read Streammer files in several possible scenarios
0002 
0003 Disclaimer: Most of the code here is randomly written during
0004             testing various parts, its not a supported testing code.
0005             Changes can and will be made, when and if required.
0006 
0007    following functions and scenarios:
0008 
0009   readSingleStream():
0010        Reads a single streamer file. It shows how the InitMsg and EventMsg
0011        can be accessed.
0012 
0013   readMultipleStreams():
0014        Reads multiple stream files and iterates through all events.
0015        The test case also show, how the File boundary crossing "event"
0016        is handled. Basically StreamerInputFile(reader) has newHeader()
0017        returning TRUE, only ONCE when a New file is opened an a INIT Message
0018        is read instead of an Event message during next().
0019 
0020   main():
0021 
0022       Code entry point, comment the function call that you don't want to make.
0023 
0024 */
0025 
0026 #include "FWCore/Utilities/interface/Exception.h"
0027 #include "IOPool/Streamer/interface/DumpTools.h"
0028 #include "IOPool/Streamer/interface/EventMessage.h"
0029 #include "IOPool/Streamer/interface/InitMessage.h"
0030 #include "IOPool/Streamer/interface/StreamerInputFile.h"
0031 #include "FWCore/Catalog/interface/InputFileCatalog.h"
0032 #include "FWCore/Catalog/interface/SiteLocalConfig.h"
0033 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0034 #include "FWCore/PluginManager/interface/PluginManager.h"
0035 #include "FWCore/PluginManager/interface/standard.h"
0036 #include "FWCore/Services/interface/setupSiteLocalConfig.h"
0037 #include "FWCore/ServiceRegistry/interface/ServiceRegistry.h"
0038 
0039 #include <iostream>
0040 
0041 int readSingleStream(bool verbose) {
0042   try {
0043     // ----------- init
0044     std::string initfilename = "teststreamfile.dat";
0045     edm::StreamerInputFile stream_reader(initfilename);
0046 
0047     std::cout << "Trying to Read The Init message from Streamer File: " << initfilename << std::endl;
0048     InitMsgView const* init = stream_reader.startMessage();
0049     if (verbose) {
0050       std::cout << "\n\n-------------INIT---------------------" << std::endl;
0051       std::cout << "Dump the Init Message from Streamer:-" << std::endl;
0052       dumpInitView(init);
0053     }
0054 
0055     // ------- event
0056 
0057     while (edm::StreamerInputFile::Next::kEvent == stream_reader.next()) {
0058       EventMsgView const* eview = stream_reader.currentRecord();
0059       if (verbose) {
0060         std::cout << "----------EVENT-----------" << std::endl;
0061         dumpEventView(eview);
0062       }
0063     }
0064 
0065   } catch (cms::Exception& e) {
0066     std::cerr << "Exception caught:  " << e.what() << std::endl;
0067     return 1;
0068   }
0069   return 0;
0070 }
0071 
0072 int readMultipleStreams(bool verbose) {
0073   try {
0074     auto operate = edm::setupSiteLocalConfig();
0075 
0076     int evCount = 0;
0077     std::vector<std::string> streamFiles;
0078     streamFiles.push_back("file:teststreamfile.dat");
0079     streamFiles.push_back("file:teststreamfile.dat");
0080 
0081     edm::InputFileCatalog catalog(streamFiles, "");
0082 
0083     edm::StreamerInputFile stream_reader(catalog.fileCatalogItems());
0084 
0085     std::cout << "Trying to Read The Init message from Streamer File: "
0086               << "teststreamfile.dat" << std::endl;
0087 
0088     InitMsgView const* init = stream_reader.startMessage();
0089     if (verbose) {
0090       std::cout << "\n\n-------------INIT---------------------" << std::endl;
0091       std::cout << "Dump the Init Message from Streamer:-" << std::endl;
0092       dumpInitView(init);
0093     }
0094 
0095     while (edm::StreamerInputFile::Next::kStop != stream_reader.next()) {
0096       if (stream_reader.newHeader()) {
0097         std::cout << "File Boundary has just been crossed, a new file is read" << std::endl;
0098         std::cout << "A new INIT Message is available" << std::endl;
0099         std::cout << "Event from next file is also avialble" << std::endl;
0100         stream_reader.openNextFile();
0101         continue;
0102       }
0103       EventMsgView const* eview = stream_reader.currentRecord();
0104       if (verbose) {
0105         std::cout << "----------EVENT-----------" << std::endl;
0106         dumpEventView(eview);
0107       }
0108       ++evCount;
0109     }
0110 
0111     std::cout << " TOTAL Events Read: " << evCount << std::endl;
0112   } catch (cms::Exception& e) {
0113     std::cerr << "Exception caught:  " << e.what() << std::endl;
0114     return 1;
0115   }
0116   return 0;
0117 }
0118 
0119 int readInvalidLFN(bool verbose) {
0120   try {
0121     auto operate = edm::setupSiteLocalConfig();
0122 
0123     int evCount = 0;
0124     std::vector<std::string> streamFiles;
0125     streamFiles.push_back("teststreamfile.dat");
0126 
0127     edm::InputFileCatalog catalog(streamFiles, "");
0128 
0129     edm::StreamerInputFile stream_reader(catalog.fileCatalogItems());
0130 
0131     std::cout << "Trying to Read The Init message from Streamer File: "
0132               << "teststreamfile.dat" << std::endl;
0133 
0134     InitMsgView const* init = stream_reader.startMessage();
0135     if (verbose) {
0136       std::cout << "\n\n-------------INIT---------------------" << std::endl;
0137       std::cout << "Dump the Init Message from Streamer:-" << std::endl;
0138       dumpInitView(init);
0139     }
0140 
0141     while (edm::StreamerInputFile::Next::kStop != stream_reader.next()) {
0142       if (stream_reader.newHeader()) {
0143         std::cout << "File Boundary has just been crossed, a new file is read" << std::endl;
0144         std::cout << "A new INIT Message is available" << std::endl;
0145         std::cout << "Event from next file is also avialble" << std::endl;
0146         stream_reader.openNextFile();
0147         continue;
0148       }
0149       EventMsgView const* eview = stream_reader.currentRecord();
0150       if (verbose) {
0151         std::cout << "----------EVENT-----------" << std::endl;
0152         dumpEventView(eview);
0153       }
0154       ++evCount;
0155     }
0156 
0157     std::cout << " TOTAL Events Read: " << evCount << std::endl;
0158   } catch (cms::Exception& e) {
0159     std::cerr << "Exception caught:  " << e.what() << std::endl;
0160     if (e.category() == "LogicalFileNameNotFound")
0161       return 0;
0162   }
0163   return 1;
0164 }
0165 
0166 void help() {
0167   std::cout << "Valid options are: " << std::endl;
0168   std::cout << "single, multi, invalid, all" << std::endl;
0169 }
0170 
0171 int main(int argc, char* argv[]) {
0172   if (argc < 2) {
0173     std::cout << "No command line argument supplied\n";
0174     help();
0175     return 1;
0176   }
0177 
0178   std::string doThis(argv[1]);
0179 
0180   int ret(0);
0181   if (doThis == "all" || doThis == "single")
0182     ret += readSingleStream(false);
0183   if (doThis == "all" || doThis == "multi")
0184     ret += readMultipleStreams(false);
0185   if (doThis == "all" || doThis == "invalid")
0186     ret += readInvalidLFN(false);
0187   std::cout << "\n\nReadStreamerFile TEST DONE\n" << std::endl;
0188 
0189   return ret;
0190 }