Skip to content

Commit

Permalink
Merge pull request #37414 from dan131riley/dqmio-guid-uniqueness-12_2_X
Browse files Browse the repository at this point in the history
Avoid DQMIO file GUID collisions, 12_2_X backport
cmsbuild authored Mar 31, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 525f823 + f5ed094 commit 9f64c63
Showing 4 changed files with 45 additions and 10 deletions.
5 changes: 4 additions & 1 deletion DQMServices/FwkIO/plugins/DQMRootOutputModule.cc
Original file line number Diff line number Diff line change
@@ -38,6 +38,7 @@
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/MessageLogger/interface/JobReport.h"
#include "FWCore/Utilities/interface/Digest.h"
#include "FWCore/Utilities/interface/GlobalIdentifier.h"

#include "DataFormats/Provenance/interface/ProcessHistory.h"
#include "DataFormats/Provenance/interface/ProcessHistoryID.h"
@@ -334,8 +335,10 @@ void DQMRootOutputModule::openFile(edm::FileBlock const&) {

edm::Service<edm::JobReport> jr;
cms::Digest branchHash;
std::string guid{m_file->GetUUID().AsString()};
std::string guid{edm::createGlobalIdentifier()};
std::transform(guid.begin(), guid.end(), guid.begin(), (int (*)(int))std::toupper);

m_file->WriteObject(&guid, kCmsGuid);
m_jrToken = jr->outputFileOpened(m_fileName,
m_logicalFileName,
std::string(),
43 changes: 35 additions & 8 deletions DQMServices/FwkIO/plugins/DQMRootSource.cc
Original file line number Diff line number Diff line change
@@ -369,8 +369,23 @@ class DQMRootSource : public edm::PuttableSourceBase, DQMTTreeIO {

// Index of currenlty processed row in m_fileMetadatas
unsigned int m_currentIndex;

// All open DQMIO files
std::vector<TFile*> m_openFiles;
struct OpenFileInfo {
OpenFileInfo(TFile* file, edm::JobReport::Token jrToken) : m_file(file), m_jrToken(jrToken) {}
~OpenFileInfo() {
edm::Service<edm::JobReport> jr;
jr->inputFileClosed(edm::InputType::Primary, m_jrToken);
}

OpenFileInfo(OpenFileInfo&&) = default;
OpenFileInfo& operator=(OpenFileInfo&&) = default;

std::unique_ptr<TFile> m_file;
edm::JobReport::Token m_jrToken;
};
std::vector<OpenFileInfo> m_openFiles;

// An item here is a row read from DQMIO indices (metadata) table
std::vector<FileMetadata> m_fileMetadatas;
};
@@ -422,7 +437,7 @@ DQMRootSource::DQMRootSource(edm::ParameterSet const& iPSet, const edm::InputSou
m_nextItemType(edm::InputSource::IsFile),
m_treeReaders(kNIndicies, std::shared_ptr<TreeReaderBase>()),
m_currentIndex(0),
m_openFiles(std::vector<TFile*>()),
m_openFiles(std::vector<OpenFileInfo>()),
m_fileMetadatas(std::vector<FileMetadata>()) {
edm::sortAndRemoveOverlaps(m_lumisToProcess);

@@ -450,8 +465,7 @@ DQMRootSource::DQMRootSource(edm::ParameterSet const& iPSet, const edm::InputSou

DQMRootSource::~DQMRootSource() {
for (auto& file : m_openFiles) {
if (file != nullptr && file->IsOpen()) {
file->Close();
if (file.m_file && file.m_file->IsOpen()) {
logFileAction("Closed file", "");
}
}
@@ -470,6 +484,8 @@ std::shared_ptr<edm::FileBlock> DQMRootSource::readFile_() {

for (auto& fileitem : m_catalog.fileCatalogItems()) {
TFile* file;
std::string pfn;
std::string lfn;
std::list<std::string> exInfo;
//loop over names of a file, each of them corresponds to a data catalog
bool isGoodFile(true);
@@ -493,7 +509,7 @@ std::shared_ptr<edm::FileBlock> DQMRootSource::readFile_() {
if (!m_skipBadFiles) {
edm::Exception ex(edm::errors::FileOpenError, "", e);
ex.addContext("Opening DQM Root file");
ex << "\nInput file " << it->c_str() << " was not found, could not be opened, or is corrupted.\n";
ex << "\nInput file " << *it << " was not found, could not be opened, or is corrupted.\n";
//report previous exceptions when use other names to open file
for (auto const& s : exInfo)
ex.addAdditionalInfo(s);
@@ -509,12 +525,14 @@ std::shared_ptr<edm::FileBlock> DQMRootSource::readFile_() {
// Check if a file is usable
if (file && !file->IsZombie()) {
logFileAction("Successfully opened file ", it->c_str());
pfn = *it;
lfn = fileitem.logicalFileName();
break;
} else {
if (std::next(it) == fNames.end()) {
if (!m_skipBadFiles) {
edm::Exception ex(edm::errors::FileOpenError);
ex << "Input file " << it->c_str() << " could not be opened.\n";
ex << "Input file " << *it << " could not be opened.\n";
ex.addContext("Opening DQM Root file");
//report previous exceptions when use other names to open file
for (auto const& s : exInfo)
@@ -533,12 +551,21 @@ std::shared_ptr<edm::FileBlock> DQMRootSource::readFile_() {
if (!isGoodFile && m_skipBadFiles)
continue;

m_openFiles.insert(m_openFiles.begin(), file);
std::unique_ptr<std::string> guid{file->Get<std::string>(kCmsGuid)};
if (not guid) {
guid = std::make_unique<std::string>(file->GetUUID().AsString());
std::transform(guid->begin(), guid->end(), guid->begin(), (int (*)(int))std::toupper);
}

edm::Service<edm::JobReport> jr;
auto jrToken = jr->inputFileOpened(
pfn, lfn, std::string(), std::string(), "DQMRootSource", "source", *guid, std::vector<std::string>());
m_openFiles.emplace_back(file, jrToken);

// Check file format version, which is encoded in the Title of the TFile
if (strcmp(file->GetTitle(), "1") != 0) {
edm::Exception ex(edm::errors::FileReadError);
ex << "Input file " << fNames[0].c_str() << " does not appear to be a DQM Root file.\n";
ex << "Input file " << fNames[0] << " does not appear to be a DQM Root file.\n";
}

// Read metadata from the file
3 changes: 3 additions & 0 deletions DQMServices/FwkIO/plugins/format.h
Original file line number Diff line number Diff line change
@@ -65,6 +65,9 @@ static const char* const kTypeBranch = "Type";
static const char* const kFirstIndex = "FirstIndex";
static const char* const kLastIndex = "LastIndex";

//File GUID
static const char* const kCmsGuid = "cms::edm::GUID";

//Meta data info
static const char* const kMetaDataDirectoryAbsolute = "/MetaData";
static const char* const kMetaDataDirectory = kMetaDataDirectoryAbsolute + 1;
4 changes: 3 additions & 1 deletion DQMServices/FwkIO/test/check_guid_file1.py
Original file line number Diff line number Diff line change
@@ -6,8 +6,10 @@
fname_root = "dqm_file1.root"
fname_report = "dqm_file1_jobreport.xml"

kCmsGuid = "cms::edm::GUID"

f = ROOT.TFile.Open(fname_root)
guid_file = f.GetUUID().AsString().upper()
guid_file = getattr(f, kCmsGuid)
f.Close()

guid_report = None

0 comments on commit 9f64c63

Please sign in to comment.