CMS 3D CMS Logo

MillePedeFileExtractor.cc
Go to the documentation of this file.
1 // Original Author: Broen van Besien
2 // Created: Mon, 23 Mar 2015 14:56:15 GMT
3 
6 
7 #include <zlib.h>
8 
13 
14 #include <TSystem.h>
15 
17  : outputDir_(iConfig.getParameter<std::string>("fileDir")),
18  outputFileName_(iConfig.getParameter<std::string>("outputBinaryFile")),
19  maxNumberOfBinaries_(iConfig.getParameter<int>("maxNumberOfBinaries")) {
20  auto fileBlobInputTag = iConfig.getParameter<edm::InputTag>("fileBlobInputTag");
21  fileBlobToken_ = consumes<FileBlobCollection, edm::BranchType::InLumi>(fileBlobInputTag);
22  if (hasBinaryNumberLimit()) {
23  edm::LogInfo("MillePedeFileActions") << "Limiting the number of extracted binary files to " << maxNumberOfBinaries_;
24  }
25 }
26 
28 
30  if (enoughBinaries())
31  return;
32 
33  // Create output directory if not available
34  if (!outputDir_.empty()) {
35  std::string command = "mkdir -p " + outputDir_;
36  int shellReturn = gSystem->Exec(command.c_str());
37  edm::LogInfo("MillePedeFileActions") << "@SUB=MillePedeFileExtractor::endLuminosityBlock"
38  << "Command returns " << shellReturn;
39  }
40 
41  // Getting our hands on the vector of FileBlobs
42  edm::Handle<FileBlobCollection> fileBlobCollection;
43  iLumi.getByToken(fileBlobToken_, fileBlobCollection);
44  if (fileBlobCollection.isValid()) {
45  // Logging the amount of FileBlobs in the vector
46  edm::LogInfo("MillePedeFileActions") << "Root file contains " << fileBlobCollection->size() << " FileBlob(s).";
47  // Loop over the FileBlobs in the vector, and write them to files:
48  for (const auto& blob : *fileBlobCollection) {
49  if (enoughBinaries())
50  break;
51  // We format the filename with a number, starting from 0 to the size of
52  // our vector.
53  // For this to work, the outputBinaryFile config parameter must contain a
54  // formatting directive for a number, like %04d.
55  char theNumberedOutputFileName[200];
56  sprintf(theNumberedOutputFileName, outputFileName_.c_str(), nBinaries_);
57 
58  // Log the filename to which we will write...
59  edm::LogInfo("MillePedeFileActions")
60  << "Writing FileBlob file to file " << outputDir_ + theNumberedOutputFileName << ".";
61 
62  // ...and perform the writing operation.
63  writeGzipped(blob, outputDir_ + theNumberedOutputFileName);
64 
65  ++nBinaries_;
66  }
67  } else {
68  edm::LogError("MillePedeFileActions") << "Error: The root file does not contain any vector of FileBlob.";
69  }
70 }
71 
73  // - use zlib directly to avoid boost dependencies for this simple task
74  // - zlib and gzip compression differ -> get uncompressed blob first
75  auto uncompressedBlob = blob.getUncompressedBlob();
76  gzFile fp = gzopen(fileName.c_str(), "wb");
77  if (fp == nullptr) {
78  edm::LogError("MillePedeFileActions") << "Problem while opening gzipped file '" << fileName << "'.";
79  }
80  auto nBytes = gzwrite(fp, &uncompressedBlob->front(), uncompressedBlob->size());
81  if (nBytes == 0 || nBytes != static_cast<decltype(nBytes)>(uncompressedBlob->size())) {
82  edm::LogError("MillePedeFileActions") << "Problem while writing FileBlob to gzipped file '" << fileName << "'.";
83  }
84  auto zerr = gzclose(fp);
85  if (zerr != 0) {
86  edm::LogError("MillePedeFileActions") << "Problem while closing gzipped file '" << fileName << "'.";
87  }
88 }
89 
90 // Manage the parameters for the module:
91 // (Note that this will autogenerate the _cfi.py file.)
94 
95  desc.add<std::string>("fileDir", "")
96  ->setComment(
97  "Keep the fileDir empty if you want to write to the current "
98  "directory.");
99 
100  desc.add<std::string>("outputBinaryFile", "milleBinary%04d.dat")
101  ->setComment(
102  "Base filename of the files that will be created. This must "
103  "contain "
104  "a placeholder for an index number in the standard C formatting "
105  "style, like %04d.");
106 
107  desc.add<edm::InputTag>("fileBlobInputTag", edm::InputTag("millePedeFileConverter", ""))
108  ->setComment(
109  "Name of the module that should have generated the blob in the "
110  "root file. Make sure you overwrite this, if you have changed "
111  "this is the configuration of the MillePedeFileConverter.");
112 
113  desc.add<int>("maxNumberOfBinaries", 1000)
114  ->setComment(
115  "Number of binaries to be extracted from the input files. "
116  "Use a negative value to apply no limit.");
117 
118  descriptions.add("millePedeFileExtractor", desc);
119  descriptions.setComment(
120  "This is the generic cfi file for the "
121  "MillePedeFileExtractor");
122 }
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
edm::EDGetTokenT< FileBlobCollection > fileBlobToken_
const std::string outputFileName_
void zerr(int)
MillePedeFileExtractor(const edm::ParameterSet &)
Log< level::Error, false > LogError
void endLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &) override
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Log< level::Info, false > LogInfo
void setComment(std::string const &value)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
void add(std::string const &label, ParameterSetDescription const &psetDescription)
bool isValid() const
Definition: HandleBase.h:70
list command
Definition: mps_check.py:25
std::unique_ptr< std::vector< unsigned char > > getUncompressedBlob() const
i didn&#39;t want to do two copies ... hope this works.
Definition: FileBlob.cc:75
static void writeGzipped(const FileBlob &, const std::string &)