test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 
5 
6 #include <zlib.h>
7 
12 
14  : outputDir_(iConfig.getParameter<std::string>("fileDir")),
15  outputFileName_(iConfig.getParameter<std::string>("outputBinaryFile")),
16  maxNumberOfBinaries_(iConfig.getParameter<int>("maxNumberOfBinaries")) {
17 
18  auto fileBlobInputTag = iConfig.getParameter<edm::InputTag>("fileBlobInputTag");
19  fileBlobToken_ = consumes<FileBlobCollection, edm::BranchType::InLumi>(fileBlobInputTag);
20  if (hasBinaryNumberLimit()) {
21  edm::LogInfo("MillePedeFileActions")
22  << "Limiting the number of extracted binary files to "
24  }
25 }
26 
28 
30  const edm::EventSetup&)
31 {
32  if (enoughBinaries()) return;
33 
34  // Getting our hands on the vector of FileBlobs
35  edm::Handle<FileBlobCollection> fileBlobCollection;
36  iLumi.getByToken(fileBlobToken_, fileBlobCollection);
37  if (fileBlobCollection.isValid()) {
38  // Logging the amount of FileBlobs in the vector
39  edm::LogInfo("MillePedeFileActions")
40  << "Root file contains " << fileBlobCollection->size() << " FileBlob(s).";
41  // Loop over the FileBlobs in the vector, and write them to files:
42  for (const auto& blob: *fileBlobCollection) {
43  if (enoughBinaries()) break;
44  // We format the filename with a number, starting from 0 to the size of
45  // our vector.
46  // For this to work, the outputBinaryFile config parameter must contain a
47  // formatting directive for a number, like %04d.
48  char theNumberedOutputFileName[200];
49  sprintf(theNumberedOutputFileName, outputFileName_.c_str(), nBinaries_);
50 
51  // Log the filename to which we will write...
52  edm::LogInfo("MillePedeFileActions")
53  << "Writing FileBlob file to file "
54  << outputDir_ + theNumberedOutputFileName << ".";
55 
56  // ...and perform the writing operation.
57  writeGzipped(blob, outputDir_ + theNumberedOutputFileName);
58 
59  ++nBinaries_;
60  }
61  } else {
62  edm::LogError("MillePedeFileActions")
63  << "Error: The root file does not contain any vector of FileBlob.";
64  }
65 }
66 
67 
69  const std::string& fileName) {
70  // - use zlib directly to avoid boost dependencies for this simple task
71  // - zlib and gzip compression differ -> get uncompressed blob first
72  auto uncompressedBlob = blob.getUncompressedBlob();
73  gzFile fp = gzopen(fileName.c_str(), "wb");
74  if (fp == NULL) {
75  edm::LogError("MillePedeFileActions")
76  << "Problem while opening gzipped file '" << fileName << "'.";
77  }
78  auto nBytes = gzwrite(fp, &uncompressedBlob->front(), uncompressedBlob->size());
79  if (nBytes == 0 ||
80  nBytes != static_cast<decltype(nBytes)>(uncompressedBlob->size())) {
81  edm::LogError("MillePedeFileActions")
82  << "Problem while writing FileBlob to gzipped file '" << fileName << "'.";
83  }
84  auto zerr = gzclose(fp);
85  if (zerr != 0) {
86  edm::LogError("MillePedeFileActions")
87  << "Problem while closing gzipped file '" << fileName << "'.";
88  }
89 }
90 
91 
92 // Manage the parameters for the module:
93 // (Note that this will autogenerate the _cfi.py file.)
95  edm::ConfigurationDescriptions& descriptions) {
97 
98  desc.add<std::string>("fileDir", "")->setComment(
99  "Keep the fileDir empty if you want to write to the current "
100  "directory.");
101 
102  desc.add<std::string>("outputBinaryFile", "milleBinary%04d.dat")->setComment(
103  "Base filename of the files that will be created. This must "
104  "contain "
105  "a placeholder for an index number in the standard C formatting "
106  "style, like %04d.");
107 
108  desc.add<edm::InputTag>("fileBlobInputTag", edm::InputTag("millePedeFileConverter",""))->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)->setComment(
114  "Number of binaries to be extracted from the input files. "
115  "Use a negative value to apply no limit.");
116 
117  descriptions.add("millePedeFileExtractor", desc);
118  descriptions.setComment(
119  "This is the generic cfi file for the "
120  "MillePedeFileExtractor");
121 }
T getParameter(std::string const &) const
edm::EDGetTokenT< FileBlobCollection > fileBlobToken_
const std::string outputFileName_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
void zerr(int)
MillePedeFileExtractor(const edm::ParameterSet &)
#define NULL
Definition: scimark2.h:8
virtual void endLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &) override
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
bool isValid() const
Definition: HandleBase.h:75
void setComment(std::string const &value)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
std::unique_ptr< std::vector< unsigned char > > getUncompressedBlob() const
i didn&#39;t want to do two copies ... hope this works.
Definition: FileBlob.cc:74
static void writeGzipped(const FileBlob &, const std::string &)