File indexing completed on 2022-06-24 22:51:36
0001
0002
0003
0004 #include "Alignment/MillePedeAlignmentAlgorithm/plugins/MillePedeFileExtractor.h"
0005 #include "FWCore/Framework/interface/LuminosityBlock.h"
0006
0007 #include <zlib.h>
0008
0009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0010 #include "CondFormats/Common/interface/FileBlob.h"
0011 #include "FWCore/Utilities/interface/InputTag.h"
0012 #include "FWCore/Utilities/interface/EDGetToken.h"
0013
0014 #include <TSystem.h>
0015
0016 MillePedeFileExtractor::MillePedeFileExtractor(const edm::ParameterSet& iConfig)
0017 : outputDir_(iConfig.getParameter<std::string>("fileDir")),
0018 outputFileName_(iConfig.getParameter<std::string>("outputBinaryFile")),
0019 maxNumberOfBinaries_(iConfig.getParameter<int>("maxNumberOfBinaries")) {
0020 auto fileBlobInputTag = iConfig.getParameter<edm::InputTag>("fileBlobInputTag");
0021 fileBlobToken_ = consumes<FileBlobCollection, edm::BranchType::InLumi>(fileBlobInputTag);
0022 if (hasBinaryNumberLimit()) {
0023 edm::LogInfo("MillePedeFileActions") << "Limiting the number of extracted binary files to " << maxNumberOfBinaries_;
0024 }
0025 }
0026
0027 MillePedeFileExtractor::~MillePedeFileExtractor() {}
0028
0029 void MillePedeFileExtractor::endLuminosityBlock(const edm::LuminosityBlock& iLumi, const edm::EventSetup&) {
0030 if (enoughBinaries())
0031 return;
0032
0033
0034 if (!outputDir_.empty()) {
0035 std::string command = "mkdir -p " + outputDir_;
0036 int shellReturn = gSystem->Exec(command.c_str());
0037 edm::LogInfo("MillePedeFileActions") << "@SUB=MillePedeFileExtractor::endLuminosityBlock"
0038 << "Command returns " << shellReturn;
0039 }
0040
0041
0042 edm::Handle<FileBlobCollection> fileBlobCollection;
0043 iLumi.getByToken(fileBlobToken_, fileBlobCollection);
0044 if (fileBlobCollection.isValid()) {
0045
0046 edm::LogInfo("MillePedeFileActions") << "Root file contains " << fileBlobCollection->size() << " FileBlob(s).";
0047
0048 for (const auto& blob : *fileBlobCollection) {
0049 if (enoughBinaries())
0050 break;
0051
0052
0053
0054
0055 char theNumberedOutputFileName[200];
0056 sprintf(theNumberedOutputFileName, outputFileName_.c_str(), nBinaries_);
0057
0058
0059 edm::LogInfo("MillePedeFileActions")
0060 << "Writing FileBlob file to file " << outputDir_ + theNumberedOutputFileName << ".";
0061
0062
0063 writeGzipped(blob, outputDir_ + theNumberedOutputFileName);
0064
0065 ++nBinaries_;
0066 }
0067 } else {
0068 edm::LogError("MillePedeFileActions") << "Error: The root file does not contain any vector of FileBlob.";
0069 }
0070 }
0071
0072 void MillePedeFileExtractor::writeGzipped(const FileBlob& blob, const std::string& fileName) {
0073
0074
0075 auto uncompressedBlob = blob.getUncompressedBlob();
0076 gzFile fp = gzopen(fileName.c_str(), "wb");
0077 if (fp == nullptr) {
0078 edm::LogError("MillePedeFileActions") << "Problem while opening gzipped file '" << fileName << "'.";
0079 }
0080 auto nBytes = gzwrite(fp, &uncompressedBlob->front(), uncompressedBlob->size());
0081 if (nBytes == 0 || nBytes != static_cast<decltype(nBytes)>(uncompressedBlob->size())) {
0082 edm::LogError("MillePedeFileActions") << "Problem while writing FileBlob to gzipped file '" << fileName << "'.";
0083 }
0084 auto zerr = gzclose(fp);
0085 if (zerr != 0) {
0086 edm::LogError("MillePedeFileActions") << "Problem while closing gzipped file '" << fileName << "'.";
0087 }
0088 }
0089
0090
0091
0092 void MillePedeFileExtractor::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0093 edm::ParameterSetDescription desc;
0094
0095 desc.add<std::string>("fileDir", "")
0096 ->setComment(
0097 "Keep the fileDir empty if you want to write to the current "
0098 "directory.");
0099
0100 desc.add<std::string>("outputBinaryFile", "milleBinary%04d.dat")
0101 ->setComment(
0102 "Base filename of the files that will be created. This must "
0103 "contain "
0104 "a placeholder for an index number in the standard C formatting "
0105 "style, like %04d.");
0106
0107 desc.add<edm::InputTag>("fileBlobInputTag", edm::InputTag("millePedeFileConverter", ""))
0108 ->setComment(
0109 "Name of the module that should have generated the blob in the "
0110 "root file. Make sure you overwrite this, if you have changed "
0111 "this is the configuration of the MillePedeFileConverter.");
0112
0113 desc.add<int>("maxNumberOfBinaries", 1000)
0114 ->setComment(
0115 "Number of binaries to be extracted from the input files. "
0116 "Use a negative value to apply no limit.");
0117
0118 descriptions.add("millePedeFileExtractor", desc);
0119 descriptions.setComment(
0120 "This is the generic cfi file for the "
0121 "MillePedeFileExtractor");
0122 }