File indexing completed on 2024-04-06 11:56:34
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 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 void MillePedeFileExtractor::endLuminosityBlock(const edm::LuminosityBlock& iLumi, const edm::EventSetup&) {
0028 if (enoughBinaries())
0029 return;
0030
0031
0032 if (!outputDir_.empty()) {
0033 std::string command = "mkdir -p " + outputDir_;
0034 int shellReturn = gSystem->Exec(command.c_str());
0035 edm::LogInfo("MillePedeFileActions") << "@SUB=MillePedeFileExtractor::endLuminosityBlock"
0036 << "Command returns " << shellReturn;
0037 }
0038
0039
0040 edm::Handle<FileBlobCollection> fileBlobCollection;
0041 iLumi.getByToken(fileBlobToken_, fileBlobCollection);
0042
0043 if (fileBlobCollection.failedToGet()) {
0044 edm::LogError("MillePedeFileActions") << "Failed to get collection from input tag: " << fileBlobInputTag_.encode();
0045 return;
0046 }
0047
0048 if (fileBlobCollection.isValid()) {
0049
0050 edm::LogInfo("MillePedeFileActions") << "Root file contains " << fileBlobCollection->size() << " FileBlob(s).";
0051
0052 for (const auto& blob : *fileBlobCollection) {
0053 if (enoughBinaries())
0054 break;
0055
0056
0057
0058
0059 char theNumberedOutputFileName[200];
0060 sprintf(theNumberedOutputFileName, outputFileName_.c_str(), nBinaries_);
0061
0062
0063 edm::LogInfo("MillePedeFileActions")
0064 << "Writing FileBlob file to file " << outputDir_ + theNumberedOutputFileName << ".";
0065
0066
0067 writeGzipped(blob, outputDir_ + theNumberedOutputFileName);
0068
0069 ++nBinaries_;
0070 }
0071 } else {
0072 edm::LogError("MillePedeFileActions")
0073 << "Error: The root file does not contain any vector of FileBlob under the label " << fileBlobInputTag_.encode()
0074 << ".";
0075 }
0076 }
0077
0078 void MillePedeFileExtractor::writeGzipped(const FileBlob& blob, const std::string& fileName) {
0079
0080
0081 auto uncompressedBlob = blob.getUncompressedBlob();
0082 gzFile fp = gzopen(fileName.c_str(), "wb");
0083 if (fp == nullptr) {
0084 edm::LogError("MillePedeFileActions") << "Problem while opening gzipped file '" << fileName << "'.";
0085 }
0086 auto nBytes = gzwrite(fp, &uncompressedBlob->front(), uncompressedBlob->size());
0087 if (nBytes == 0 || nBytes != static_cast<decltype(nBytes)>(uncompressedBlob->size())) {
0088 edm::LogError("MillePedeFileActions") << "Problem while writing FileBlob to gzipped file '" << fileName << "'.";
0089 }
0090 auto zerr = gzclose(fp);
0091 if (zerr != 0) {
0092 edm::LogError("MillePedeFileActions") << "Problem while closing gzipped file '" << fileName << "'.";
0093 }
0094 }
0095
0096
0097
0098 void MillePedeFileExtractor::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0099 edm::ParameterSetDescription desc;
0100
0101 desc.add<std::string>("fileDir", "")
0102 ->setComment(
0103 "Keep the fileDir empty if you want to write to the current "
0104 "directory.");
0105
0106 desc.add<std::string>("outputBinaryFile", "milleBinary%04d.dat")
0107 ->setComment(
0108 "Base filename of the files that will be created. This must "
0109 "contain "
0110 "a placeholder for an index number in the standard C formatting "
0111 "style, like %04d.");
0112
0113 desc.add<edm::InputTag>("fileBlobInputTag", edm::InputTag("millePedeFileConverter", ""))
0114 ->setComment(
0115 "Name of the module that should have generated the blob in the "
0116 "root file. Make sure you overwrite this, if you have changed "
0117 "this is the configuration of the MillePedeFileConverter.");
0118
0119 desc.add<int>("maxNumberOfBinaries", 1000)
0120 ->setComment(
0121 "Number of binaries to be extracted from the input files. "
0122 "Use a negative value to apply no limit.");
0123
0124 descriptions.add("millePedeFileExtractor", desc);
0125 descriptions.setComment(
0126 "This is the generic cfi file for the "
0127 "MillePedeFileExtractor");
0128 }