Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-06-24 22:51:36

0001 // Original Author:  Broen van Besien
0002 //         Created:  Mon, 23 Mar 2015 14:56:15 GMT
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   // Create output directory if not available
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   // Getting our hands on the vector of FileBlobs
0042   edm::Handle<FileBlobCollection> fileBlobCollection;
0043   iLumi.getByToken(fileBlobToken_, fileBlobCollection);
0044   if (fileBlobCollection.isValid()) {
0045     // Logging the amount of FileBlobs in the vector
0046     edm::LogInfo("MillePedeFileActions") << "Root file contains " << fileBlobCollection->size() << " FileBlob(s).";
0047     // Loop over the FileBlobs in the vector, and write them to files:
0048     for (const auto& blob : *fileBlobCollection) {
0049       if (enoughBinaries())
0050         break;
0051       // We format the filename with a number, starting from 0 to the size of
0052       // our vector.
0053       // For this to work, the outputBinaryFile config parameter must contain a
0054       // formatting directive for a number, like %04d.
0055       char theNumberedOutputFileName[200];
0056       sprintf(theNumberedOutputFileName, outputFileName_.c_str(), nBinaries_);
0057 
0058       // Log the filename to which we will write...
0059       edm::LogInfo("MillePedeFileActions")
0060           << "Writing FileBlob file to file " << outputDir_ + theNumberedOutputFileName << ".";
0061 
0062       // ...and perform the writing operation.
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   // - use zlib directly to avoid boost dependencies for this simple task
0074   // - zlib and gzip compression differ -> get uncompressed blob first
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 // Manage the parameters for the module:
0091 // (Note that this will autogenerate the _cfi.py file.)
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 }