Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:56:34

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   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   // Create output directory if not available
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   // Getting our hands on the vector of FileBlobs
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     // Logging the amount of FileBlobs in the vector
0050     edm::LogInfo("MillePedeFileActions") << "Root file contains " << fileBlobCollection->size() << " FileBlob(s).";
0051     // Loop over the FileBlobs in the vector, and write them to files:
0052     for (const auto& blob : *fileBlobCollection) {
0053       if (enoughBinaries())
0054         break;
0055       // We format the filename with a number, starting from 0 to the size of
0056       // our vector.
0057       // For this to work, the outputBinaryFile config parameter must contain a
0058       // formatting directive for a number, like %04d.
0059       char theNumberedOutputFileName[200];
0060       sprintf(theNumberedOutputFileName, outputFileName_.c_str(), nBinaries_);
0061 
0062       // Log the filename to which we will write...
0063       edm::LogInfo("MillePedeFileActions")
0064           << "Writing FileBlob file to file " << outputDir_ + theNumberedOutputFileName << ".";
0065 
0066       // ...and perform the writing operation.
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   // - use zlib directly to avoid boost dependencies for this simple task
0080   // - zlib and gzip compression differ -> get uncompressed blob first
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 // Manage the parameters for the module:
0097 // (Note that this will autogenerate the _cfi.py file.)
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 }