Skip to content

Commit

Permalink
Guards when opening and saving to root files (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
kjvbrt authored Jul 15, 2024
1 parent 8186bc7 commit 92e9e63
Show file tree
Hide file tree
Showing 14 changed files with 257 additions and 102 deletions.
22 changes: 15 additions & 7 deletions RecCalorimeter/src/components/CorrectECalBarrelSliWinCluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "edm4hep/VertexCollection.h"

// ROOT
#include "TSystem.h"
#include "TFile.h"
#include "TLorentzVector.h"
#include "TFitResult.h"
Expand Down Expand Up @@ -430,25 +431,32 @@ StatusCode CorrectECalBarrelSliWinCluster::execute() {
StatusCode CorrectECalBarrelSliWinCluster::finalize() { return GaudiAlgorithm::finalize(); }

StatusCode CorrectECalBarrelSliWinCluster::initNoiseFromFile() {
// check if file exists
// Check if file exists
if (m_noiseFileName.empty()) {
error() << "Name of the file with noise values not set" << endmsg;
error() << "Name of the file with the noise values not set!" << endmsg;
return StatusCode::FAILURE;
}
std::unique_ptr<TFile> file(TFile::Open(m_noiseFileName.value().c_str(), "READ"));
if (file->IsZombie()) {
error() << "Couldn't open the file with noise constants" << endmsg;
if (gSystem->AccessPathName(m_noiseFileName.value().c_str())) {
error() << "Provided file with the noise values not found!" << endmsg;
error() << "File path: " << m_noiseFileName.value() << endmsg;
return StatusCode::FAILURE;
}
std::unique_ptr<TFile> noiseFile(TFile::Open(m_noiseFileName.value().c_str(), "READ"));
if (noiseFile->IsZombie()) {
error() << "Unable to read the file with the noise values!" << endmsg;
error() << "File path: " << m_noiseFileName.value() << endmsg;
return StatusCode::FAILURE;
} else {
info() << "Opening the file with noise constants: " << m_noiseFileName << endmsg;
info() << "Using the following file with the noise values: "
<< m_noiseFileName.value() << endmsg;
}

std::string pileupParamHistoName;
// Read the histograms with parameters for the pileup noise from the file
for (unsigned i = 0; i < 2; i++) {
pileupParamHistoName = m_pileupHistoName + std::to_string(i);
debug() << "Getting histogram with a name " << pileupParamHistoName << endmsg;
m_histoPileupConst.push_back(*dynamic_cast<TH1F*>(file->Get(pileupParamHistoName.c_str())));
m_histoPileupConst.push_back(*dynamic_cast<TH1F*>(noiseFile->Get(pileupParamHistoName.c_str())));
if (m_histoPileupConst.at(i).GetNbinsX() < 1) {
error() << "Histogram " << pileupParamHistoName
<< " has 0 bins! check the file with noise and the name of the histogram!" << endmsg;
Expand Down
22 changes: 15 additions & 7 deletions RecCalorimeter/src/components/MassInv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "edm4hep/MCParticleCollection.h"

// ROOT
#include "TSystem.h"
#include "TFile.h"
#include "TLorentzVector.h"
#include "TFitResult.h"
Expand Down Expand Up @@ -807,25 +808,32 @@ StatusCode MassInv::execute() {
StatusCode MassInv::finalize() { return GaudiAlgorithm::finalize(); }

StatusCode MassInv::initNoiseFromFile() {
// check if file exists
// Check if file exists
if (m_noiseFileName.empty()) {
error() << "Name of the file with noise values not set" << endmsg;
error() << "Name of the file with the noise values not provided!" << endmsg;
return StatusCode::FAILURE;
}
std::unique_ptr<TFile> file(TFile::Open(m_noiseFileName.value().c_str(), "READ"));
if (file->IsZombie()) {
error() << "Couldn't open the file with noise constants" << endmsg;
if (gSystem->AccessPathName(m_noiseFileName.value().c_str())) {
error() << "Provided file with the noise values not found!" << endmsg;
error() << "File path: " << m_noiseFileName.value() << endmsg;
return StatusCode::FAILURE;
}
std::unique_ptr<TFile> inFile(TFile::Open(m_noiseFileName.value().c_str(), "READ"));
if (inFile->IsZombie()) {
error() << "Unable to open the file with the noise values!" << endmsg;
error() << "File path: " << m_noiseFileName.value() << endmsg;
return StatusCode::FAILURE;
} else {
info() << "Opening the file with noise constants: " << m_noiseFileName << endmsg;
info() << "Using the following file with the noise constants: "
<< m_noiseFileName.value() << endmsg;
}

std::string pileupParamHistoName;
// Read the histograms with parameters for the pileup noise from the file
for (unsigned i = 0; i < 2; i++) {
pileupParamHistoName = m_pileupHistoName + std::to_string(i);
debug() << "Getting histogram with a name " << pileupParamHistoName << endmsg;
m_histoPileupConst.push_back(*dynamic_cast<TH1F*>(file->Get(pileupParamHistoName.c_str())));
m_histoPileupConst.push_back(*dynamic_cast<TH1F*>(inFile->Get(pileupParamHistoName.c_str())));
if (m_histoPileupConst.at(i).GetNbinsX() < 1) {
error() << "Histogram " << pileupParamHistoName
<< " has 0 bins! check the file with noise and the name of the histogram!" << endmsg;
Expand Down
16 changes: 12 additions & 4 deletions RecCalorimeter/src/components/NoiseCaloCellsFromFileTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "DD4hep/Detector.h"

// ROOT
#include "TSystem.h"
#include "TFile.h"
#include "TH1F.h"
#include "TMath.h"
Expand Down Expand Up @@ -116,17 +117,24 @@ StatusCode NoiseCaloCellsFromFileTool::finalize() {
}

StatusCode NoiseCaloCellsFromFileTool::initNoiseFromFile() {
// check if file exists
// Check if file exists
if (m_noiseFileName.empty()) {
error() << "Name of the file with noise values not set" << endmsg;
error() << "Name of the file with the noise values not provided!" << endmsg;
return StatusCode::FAILURE;
}
if (gSystem->AccessPathName(m_noiseFileName.value().c_str())) {
error() << "Provided file with the noise values not found!" << endmsg;
error() << "File path: " << m_noiseFileName.value() << endmsg;
return StatusCode::FAILURE;
}
std::unique_ptr<TFile> noiseFile(TFile::Open(m_noiseFileName.value().c_str(), "READ"));
if (noiseFile->IsZombie()) {
error() << "Couldn't open the file with noise constants" << endmsg;
error() << "Unable to open the file with the noise values!" << endmsg;
error() << "File path: " << m_noiseFileName.value() << endmsg;
return StatusCode::FAILURE;
} else {
info() << "Opening the file with noise constants: " << m_noiseFileName << endmsg;
info() << "Using the following file with noise values: "
<< m_noiseFileName.value() << endmsg;
}

std::string elecNoiseLayerHistoName, pileupLayerHistoName;
Expand Down
50 changes: 36 additions & 14 deletions RecCalorimeter/src/components/ReadCaloCrosstalkMap.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "ReadCaloCrosstalkMap.h"

#include "TSystem.h"
#include "TFile.h"
#include "TTree.h"
#include "TBranch.h"
Expand All @@ -21,32 +22,53 @@ StatusCode ReadCaloCrosstalkMap::initialize() {
return StatusCode::SUCCESS;
}

StatusCode sc = GaudiTool::initialize();
info() <<"Loading crosstalk map..." << endmsg;
if (sc.isFailure()) return sc;
std::unique_ptr<TFile> file(TFile::Open(m_fileName.value().c_str(),"READ"));
{
StatusCode sc = GaudiTool::initialize();
info() << "Loading crosstalk map..." << endmsg;
if (sc.isFailure()) return sc;
}

// Check if crosstalk file exists
if (gSystem->AccessPathName(m_fileName.value().c_str())) {
error() << "Provided file with the crosstalk map not found!" << endmsg;
error() << "File path: " << m_fileName.value() << endmsg;
return StatusCode::FAILURE;
}
std::unique_ptr<TFile> xtalkFile(TFile::Open(m_fileName.value().c_str(), "READ"));
if (xtalkFile->IsZombie()) {
error() << "Unable to read the file with the crosstalk map!" << endmsg;
error() << "File path: " << m_fileName.value() << endmsg;
return StatusCode::FAILURE;
} else {
info() << "Using the following file with the crosstalk map: "
<< m_fileName.value() << endmsg;
}

TTree* tree = nullptr;
file->GetObject("crosstalk_neighbours",tree);
xtalkFile->GetObject("crosstalk_neighbours", tree);
ULong64_t read_cellId;
std::vector<uint64_t> *read_neighbours=0;
std::vector<double> *read_crosstalks=0;
tree->SetBranchAddress("cellId",&read_cellId);
std::vector<uint64_t>* read_neighbours = nullptr;
std::vector<double>* read_crosstalks = nullptr;

tree->SetBranchAddress("cellId", &read_cellId);
tree->SetBranchAddress("list_crosstalk_neighbours", &read_neighbours);
tree->SetBranchAddress("list_crosstalks", &read_crosstalks);
for (uint i = 0; i < tree->GetEntries(); i++) {
tree->GetEntry(i);
m_mapNeighbours.insert(std::pair<uint64_t, std::vector<uint64_t>>(read_cellId, *read_neighbours));
m_mapCrosstalks.insert(std::pair<uint64_t, std::vector<double>>(read_cellId, *read_crosstalks));
}

info() <<"Crosstalk input: " << m_fileName.value().c_str() << endmsg;
info() << "Total number of cells = " << tree->GetEntries() << ", Size of crosstalk neighbours = " << m_mapNeighbours.size() << ", Size of coefficients = " << m_mapCrosstalks.size() << endmsg;

info() << "Crosstalk input: " << m_fileName.value().c_str() << endmsg;
info() << "Total number of cells = " << tree->GetEntries()
<< ", Size of crosstalk neighbours = " << m_mapNeighbours.size()
<< ", Size of coefficients = " << m_mapCrosstalks.size() << endmsg;
delete tree;
delete read_neighbours;
delete read_crosstalks;
file->Close();
return sc;
xtalkFile->Close();

return StatusCode::SUCCESS;
}

StatusCode ReadCaloCrosstalkMap::finalize() { return GaudiTool::finalize(); }
Expand Down
16 changes: 12 additions & 4 deletions RecCalorimeter/src/components/ReadNoiseFromFileTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "DDSegmentation/Segmentation.h"

// ROOT
#include "TSystem.h"
#include "TFile.h"
#include "TH1F.h"
#include "TMath.h"
Expand Down Expand Up @@ -58,17 +59,24 @@ StatusCode ReadNoiseFromFileTool::finalize() {
}

StatusCode ReadNoiseFromFileTool::initNoiseFromFile() {
// check if file exists
// Check if file exists
if (m_noiseFileName.empty()) {
error() << "Name of the file with noise values not set" << endmsg;
error() << "Name of the file with the noise values not provided!" << endmsg;
return StatusCode::FAILURE;
}
if (gSystem->AccessPathName(m_noiseFileName.value().c_str())) {
error() << "Provided file with the noise values not found!" << endmsg;
error() << "File path: " << m_noiseFileName.value() << endmsg;
return StatusCode::FAILURE;
}
std::unique_ptr<TFile> file(TFile::Open(m_noiseFileName.value().c_str(), "READ"));
if (file->IsZombie()) {
error() << "Couldn't open the file with noise constants" << endmsg;
error() << "Unable to open the file with the noise values!" << endmsg;
error() << "File path: " << m_noiseFileName.value() << endmsg;
return StatusCode::FAILURE;
} else {
info() << "Opening the file with noise constants: " << m_noiseFileName << endmsg;
info() << "Using the following file with the noise values: "
<< m_noiseFileName.value() << endmsg;
}

std::string elecNoiseLayerHistoName, pileupLayerHistoName;
Expand Down
38 changes: 31 additions & 7 deletions RecCalorimeter/src/components/TopoCaloNeighbours.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,46 @@
#include "TopoCaloNeighbours.h"

#include "TSystem.h"
#include "TFile.h"
#include "TTree.h"
#include "TBranch.h"

DECLARE_COMPONENT(TopoCaloNeighbours)

TopoCaloNeighbours::TopoCaloNeighbours(const std::string& type, const std::string& name,
const IInterface* parent)
const IInterface* parent)
: GaudiTool(type, name, parent) {
declareInterface<ICaloReadNeighboursMap>(this);
}

StatusCode TopoCaloNeighbours::initialize() {
StatusCode sc = GaudiTool::initialize();
if (sc.isFailure()) return sc;
std::unique_ptr<TFile> file(TFile::Open(m_fileName.value().c_str(),"READ"));
{
StatusCode sc = GaudiTool::initialize();
if (sc.isFailure()) return sc;
}

// Check if neighbours map file exists
if (m_fileName.empty()) {
error() << "Proper filepath for the neighbours map not provided!" << endmsg;
return StatusCode::FAILURE;
}
if (gSystem->AccessPathName(m_fileName.value().c_str())) {
error() << "Provided neighbours map file not found!" << endmsg;
error() << "File path: " << m_fileName.value() << endmsg;
return StatusCode::FAILURE;
}
std::unique_ptr<TFile> inFile(TFile::Open(m_fileName.value().c_str(), "READ"));
if (inFile->IsZombie()) {
error() << "Unable to open the provide file with neighbours map!" << endmsg;
error() << "File path: " << m_fileName.value() << endmsg;
return StatusCode::FAILURE;
} else {
info() << "Using the following file with neighbours map: "
<< m_fileName.value() << endmsg;
}

TTree* tree = nullptr;
file->GetObject("neighbours",tree);
inFile->GetObject("neighbours", tree);
ULong64_t readCellId;
std::vector<uint64_t>* readNeighbours = nullptr;
tree->SetBranchAddress("cellId",&readCellId);
Expand All @@ -38,8 +61,9 @@ StatusCode TopoCaloNeighbours::initialize() {
}
delete tree;
delete readNeighbours;
file->Close();
return sc;
inFile->Close();

return StatusCode::SUCCESS;
}

StatusCode TopoCaloNeighbours::finalize() { return GaudiTool::finalize(); }
Expand Down
38 changes: 31 additions & 7 deletions RecCalorimeter/src/components/TopoCaloNoisyCells.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "TopoCaloNoisyCells.h"

#include "TBranch.h"
#include "TSystem.h"
#include "TFile.h"
#include "TBranch.h"
#include "TTree.h"

DECLARE_COMPONENT(TopoCaloNoisyCells)
Expand All @@ -12,11 +13,33 @@ TopoCaloNoisyCells::TopoCaloNoisyCells(const std::string& type, const std::strin
}

StatusCode TopoCaloNoisyCells::initialize() {
StatusCode sc = GaudiTool::initialize();
if (sc.isFailure()) return sc;
std::unique_ptr<TFile> file(TFile::Open(m_fileName.value().c_str(), "READ"));
{
StatusCode sc = GaudiTool::initialize();
if (sc.isFailure()) return sc;
}

// Check if file exists
if (m_fileName.empty()) {
error() << "Name of the file with the noisy cells not provided!" << endmsg;
return StatusCode::FAILURE;
}
if (gSystem->AccessPathName(m_fileName.value().c_str())) {
error() << "Provided file with the noisy cells not found!" << endmsg;
error() << "File path: " << m_fileName.value() << endmsg;
return StatusCode::FAILURE;
}
std::unique_ptr<TFile> inFile(TFile::Open(m_fileName.value().c_str(), "READ"));
if (inFile->IsZombie()) {
error() << "Unable to open the file with the noisy cells!" << endmsg;
error() << "File path: " << m_fileName.value() << endmsg;
return StatusCode::FAILURE;
} else {
info() << "Using the following file with the noisy cells: "
<< m_fileName.value() << endmsg;
}

TTree* tree = nullptr;
file->GetObject("noisyCells", tree);
inFile->GetObject("noisyCells", tree);
ULong64_t readCellId;
double readNoisyCells;
double readNoisyCellsOffset;
Expand All @@ -28,8 +51,9 @@ StatusCode TopoCaloNoisyCells::initialize() {
m_map.insert(std::pair<uint64_t, std::pair<double, double>>(readCellId, std::make_pair(readNoisyCells, readNoisyCellsOffset)));
}
delete tree;
file->Close();
return sc;
inFile->Close();

return StatusCode::SUCCESS;
}

StatusCode TopoCaloNoisyCells::finalize() { return GaudiTool::finalize(); }
Expand Down
Loading

0 comments on commit 92e9e63

Please sign in to comment.