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 
4 #include <memory>
5 
7 
12 
14  : theOutputDir(iConfig.getParameter<std::string>("fileDir")),
15  theOutputFileName(iConfig.getParameter<std::string>("outputBinaryFile")) {
16 
17  edm::InputTag fileBlobInputTag = iConfig.getParameter<edm::InputTag>("fileBlobInputTag");
18  theFileBlobToken = consumes<FileBlobCollection, edm::BranchType::InRun>(fileBlobInputTag);
19  // nothing else in the constructor
20 }
21 
23 
25  edm::EventSetup const&) {
26  // Getting our hands on the vector of FileBlobs
27  edm::Handle<FileBlobCollection> theFileBlobCollection;
28  iRun.getByToken(theFileBlobToken, theFileBlobCollection);
29  if (theFileBlobCollection.isValid()) {
30  // Logging the amount of FileBlobs in the vector
31  int theVectorSize = theFileBlobCollection->size();
32  edm::LogInfo("MillePedeFileActions") << "Root file contains "
33  << theVectorSize << " FileBlob(s).";
34  // Loop over the FileBlobs in the vector, and write them to files:
35  for (std::vector<FileBlob>::const_iterator it =
36  theFileBlobCollection->begin();
37  it != theFileBlobCollection->end(); ++it) {
38  // We format the filename with a number, starting from 0 to the size of
39  // our vector.
40  // For this to work, the outputBinaryFile config parameter must contain a
41  // formatting directive for a number, like %04d.
42  char theNumberedOutputFileName[200];
43  int theNumber = it - theFileBlobCollection->begin();
44  sprintf(theNumberedOutputFileName, theOutputFileName.c_str(), theNumber);
45  // Log the filename to which we will write...
46  edm::LogInfo("MillePedeFileActions")
47  << "Writing FileBlob file to file "
48  << theOutputDir + theNumberedOutputFileName << ".";
49  // ...and perform the writing operation.
50  it->write(theOutputDir + theNumberedOutputFileName);
51  // Carefull, it seems that when writing to an impossible file, this is
52  // swallowed by the FileBlob->write operation and no error is thrown.
53  }
54  } else {
55  edm::LogError("MillePedeFileActions")
56  << "Error: The root file does not contain any vector of FileBlob.";
57  }
58 }
59 
60 // Manage the parameters for the module:
61 // (Note that this will autogenerate the _cfi.py file.)
63  edm::ConfigurationDescriptions& descriptions) {
65 
66  desc.add<std::string>("fileDir", "")->setComment(
67  "Keep the fileDir empty if you want to write to the current "
68  "directory.");
69 
70  desc.add<std::string>("outputBinaryFile", "milleBinary%04d.dat")->setComment(
71  "Base filename of the files that will be created. This must "
72  "contain "
73  "a placeholder for an index number in the standard C formatting "
74  "style, like %04d.");
75 
76  desc.add<edm::InputTag>("fileBlobInputTag", edm::InputTag("millePedeFileConverter",""))->setComment(
77  "Name of the module that should have generated the blob in the "
78  "root file. Make sure you overwrite this, if you have changed "
79  "this is the configuration of the MillePedeFileConverter.");
80 
81 
82  descriptions.add("millePedeFileExtractor", desc);
83  descriptions.setComment(
84  "This is the generic cfi file for the "
85  "MillePedeFileExtractor");
86 }
virtual void endRun(edm::Run const &, edm::EventSetup const &) override
T getParameter(std::string const &) const
MillePedeFileExtractor(const edm::ParameterSet &)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Run.h:258
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)
edm::EDGetTokenT< FileBlobCollection > theFileBlobToken
void add(std::string const &label, ParameterSetDescription const &psetDescription)
Definition: Run.h:43