Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid DQMIO file GUID collisions #37405

Merged
merged 3 commits into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion DQMServices/FwkIO/plugins/DQMRootOutputModule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(),
Expand Down
43 changes: 35 additions & 8 deletions DQMServices/FwkIO/plugins/DQMRootSource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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", "");
}
}
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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)
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions DQMServices/FwkIO/plugins/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion DQMServices/FwkIO/test/check_guid_file1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down