Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-05-31 04:19:43

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 using namespace edm::streamer;
0042 
0043 int readSingleStream(bool verbose) {
0044   try {
0045     // ----------- init
0046     std::string initfilename = "teststreamfile.dat";
0047     StreamerInputFile stream_reader(initfilename);
0048 
0049     std::cout << "Trying to Read The Init message from Streamer File: " << initfilename << std::endl;
0050     InitMsgView const* init = stream_reader.startMessage();
0051     if (verbose) {
0052       std::cout << "\n\n-------------INIT---------------------" << std::endl;
0053       std::cout << "Dump the Init Message from Streamer:-" << std::endl;
0054       dumpInitView(init);
0055     }
0056 
0057     // ------- event
0058 
0059     while (StreamerInputFile::Next::kEvent == stream_reader.next()) {
0060       EventMsgView const* eview = stream_reader.currentRecord();
0061       if (verbose) {
0062         std::cout << "----------EVENT-----------" << std::endl;
0063         dumpEventView(eview);
0064       }
0065     }
0066 
0067   } catch (cms::Exception& e) {
0068     std::cerr << "Exception caught:  " << e.what() << std::endl;
0069     return 1;
0070   }
0071   return 0;
0072 }
0073 
0074 int readMultipleStreams(bool verbose) {
0075   try {
0076     auto operate = edm::setupSiteLocalConfig();
0077 
0078     int evCount = 0;
0079     std::vector<std::string> streamFiles;
0080     streamFiles.push_back("file:teststreamfile.dat");
0081     streamFiles.push_back("file:teststreamfile.dat");
0082 
0083     edm::InputFileCatalog catalog(streamFiles, "");
0084 
0085     StreamerInputFile stream_reader(catalog.fileCatalogItems());
0086 
0087     std::cout << "Trying to Read The Init message from Streamer File: "
0088               << "teststreamfile.dat" << std::endl;
0089 
0090     InitMsgView const* init = stream_reader.startMessage();
0091     if (verbose) {
0092       std::cout << "\n\n-------------INIT---------------------" << std::endl;
0093       std::cout << "Dump the Init Message from Streamer:-" << std::endl;
0094       dumpInitView(init);
0095     }
0096 
0097     while (StreamerInputFile::Next::kStop != stream_reader.next()) {
0098       if (stream_reader.newHeader()) {
0099         std::cout << "File Boundary has just been crossed, a new file is read" << std::endl;
0100         std::cout << "A new INIT Message is available" << std::endl;
0101         std::cout << "Event from next file is also avialble" << std::endl;
0102         stream_reader.openNextFile();
0103         continue;
0104       }
0105       EventMsgView const* eview = stream_reader.currentRecord();
0106       if (verbose) {
0107         std::cout << "----------EVENT-----------" << std::endl;
0108         dumpEventView(eview);
0109       }
0110       ++evCount;
0111     }
0112 
0113     std::cout << " TOTAL Events Read: " << evCount << std::endl;
0114   } catch (cms::Exception& e) {
0115     std::cerr << "Exception caught:  " << e.what() << std::endl;
0116     return 1;
0117   }
0118   return 0;
0119 }
0120 
0121 int readInvalidLFN(bool verbose) {
0122   try {
0123     auto operate = edm::setupSiteLocalConfig();
0124 
0125     int evCount = 0;
0126     std::vector<std::string> streamFiles;
0127     streamFiles.push_back("teststreamfile.dat");
0128 
0129     edm::InputFileCatalog catalog(streamFiles, "");
0130 
0131     StreamerInputFile stream_reader(catalog.fileCatalogItems());
0132 
0133     std::cout << "Trying to Read The Init message from Streamer File: "
0134               << "teststreamfile.dat" << std::endl;
0135 
0136     InitMsgView const* init = stream_reader.startMessage();
0137     if (verbose) {
0138       std::cout << "\n\n-------------INIT---------------------" << std::endl;
0139       std::cout << "Dump the Init Message from Streamer:-" << std::endl;
0140       dumpInitView(init);
0141     }
0142 
0143     while (StreamerInputFile::Next::kStop != stream_reader.next()) {
0144       if (stream_reader.newHeader()) {
0145         std::cout << "File Boundary has just been crossed, a new file is read" << std::endl;
0146         std::cout << "A new INIT Message is available" << std::endl;
0147         std::cout << "Event from next file is also avialble" << std::endl;
0148         stream_reader.openNextFile();
0149         continue;
0150       }
0151       EventMsgView const* eview = stream_reader.currentRecord();
0152       if (verbose) {
0153         std::cout << "----------EVENT-----------" << std::endl;
0154         dumpEventView(eview);
0155       }
0156       ++evCount;
0157     }
0158 
0159     std::cout << " TOTAL Events Read: " << evCount << std::endl;
0160   } catch (cms::Exception& e) {
0161     std::cerr << "Exception caught:  " << e.what() << std::endl;
0162     if (e.category() == "LogicalFileNameNotFound")
0163       return 0;
0164   }
0165   return 1;
0166 }
0167 
0168 void help() {
0169   std::cout << "Valid options are: " << std::endl;
0170   std::cout << "single, multi, invalid, all" << std::endl;
0171 }
0172 
0173 int main(int argc, char* argv[]) {
0174   if (argc < 2) {
0175     std::cout << "No command line argument supplied\n";
0176     help();
0177     return 1;
0178   }
0179 
0180   std::string doThis(argv[1]);
0181 
0182   int ret(0);
0183   if (doThis == "all" || doThis == "single")
0184     ret += readSingleStream(false);
0185   if (doThis == "all" || doThis == "multi")
0186     ret += readMultipleStreams(false);
0187   if (doThis == "all" || doThis == "invalid")
0188     ret += readInvalidLFN(false);
0189   std::cout << "\n\nReadStreamerFile TEST DONE\n" << std::endl;
0190 
0191   return ret;
0192 }