Skip to content

Commit fa92e48

Browse files
Dmitri Peresunkodavidrohr
authored andcommitted
Fix memory leak in calibration
1 parent 8a67d61 commit fa92e48

7 files changed

Lines changed: 135 additions & 85 deletions

File tree

Detectors/PHOS/calib/include/PHOSCalibWorkflow/PHOSEnergyCalibDevice.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace phos
3939
class PHOSEnergyCalibDevice : public o2::framework::Task
4040
{
4141
public:
42-
explicit PHOSEnergyCalibDevice(bool useCCDB, std::shared_ptr<o2::base::GRPGeomRequest> req) : mUseCCDB(useCCDB), mCCDBRequest(req) {}
42+
explicit PHOSEnergyCalibDevice(bool useCCDB, std::shared_ptr<o2::base::GRPGeomRequest> req, const std::string& outputDir, const std::string& metaFileDir, bool writeRootOutput) : mUseCCDB(useCCDB), mWriteRootOutput(writeRootOutput), mOutputDir(outputDir), mMetaFileDir(metaFileDir), mCCDBRequest(req) {}
4343

4444
void init(o2::framework::InitContext& ic) final;
4545

@@ -65,9 +65,10 @@ class PHOSEnergyCalibDevice : public o2::framework::Task
6565
o2::framework::DataTakingContext mDataTakingContext{};
6666
bool mUseCCDB = false;
6767
bool mHasCalib = false;
68-
bool mPostHistos = true; /// post colllected histos to ccdb
69-
long mRunStartTime = 0; /// start time of the run (sec)
70-
float mPtMin = 1.5; /// minimal energy to fill inv. mass histo
68+
bool mPostHistos = true; /// post colllected histos to ccdb
69+
bool mWriteRootOutput = true; /// Write local root files
70+
long mRunStartTime = 0; /// start time of the run (sec)
71+
float mPtMin = 1.5; /// minimal energy to fill inv. mass histo
7172
float mEminHGTime = 1.5;
7273
float mEminLGTime = 5.;
7374
float mEDigMin = 0.05;
@@ -90,7 +91,7 @@ class PHOSEnergyCalibDevice : public o2::framework::Task
9091
std::shared_ptr<o2::base::GRPGeomRequest> mCCDBRequest;
9192
};
9293

93-
o2::framework::DataProcessorSpec getPHOSEnergyCalibDeviceSpec(bool useCCDB);
94+
o2::framework::DataProcessorSpec getPHOSEnergyCalibDeviceSpec(bool useCCDB, const std::string& outputDir, const std::string& metaFileDir, bool writeRootOutput);
9495
} // namespace phos
9596
} // namespace o2
9697

Detectors/PHOS/calib/include/PHOSCalibWorkflow/PHOSRunbyrunCalibDevice.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "Framework/Task.h"
1919
#include "Framework/WorkflowSpec.h"
2020
#include "Framework/ProcessingContext.h"
21+
#include "DetectorsCommonDataFormats/FileMetaData.h"
2122
#include "DataFormatsPHOS/Cluster.h"
2223
#include "DataFormatsPHOS/BadChannelsMap.h"
2324
#include "PHOSCalibWorkflow/PHOSRunbyrunCalibrator.h"
@@ -33,7 +34,7 @@ namespace phos
3334
class PHOSRunbyrunCalibDevice
3435
{
3536
public:
36-
PHOSRunbyrunCalibDevice(std::shared_ptr<o2::base::GRPGeomRequest> req) : mCCDBRequest(req) {}
37+
PHOSRunbyrunCalibDevice(std::shared_ptr<o2::base::GRPGeomRequest> req, const std::string& outputDir, const std::string& metaFileDir, bool writeRootOutput) : mWriteRootOutput(writeRootOutput), mOutputDir(outputDir), mMetaFileDir(metaFileDir), mCCDBRequest(req) {}
3738

3839
void init(o2::framework::InitContext& ic);
3940

@@ -51,13 +52,17 @@ class PHOSRunbyrunCalibDevice
5152

5253
private:
5354
bool mUseCCDB = false;
54-
long mRunStartTime = 0; /// start time of the run (sec)
55+
bool mWriteRootOutput = true; /// Write local root files
56+
std::string mOutputDir; /// where to write calibration digits
57+
std::string mMetaFileDir; /// where to store meta files
58+
long mRunStartTime = 0; /// start time of the run (ms)
5559
std::array<float, 8> mRunByRun; /// Final calibration object
5660
std::unique_ptr<PHOSRunbyrunCalibrator> mCalibrator; /// Agregator of calibration TimeFrameSlots
5761
std::shared_ptr<o2::base::GRPGeomRequest> mCCDBRequest;
62+
std::unique_ptr<o2::dataformats::FileMetaData> mHistoFileMetaData; /// Metadata for collected histograms
5863
};
5964

60-
o2::framework::DataProcessorSpec getPHOSRunbyrunCalibDeviceSpec(bool useCCDB);
65+
o2::framework::DataProcessorSpec getPHOSRunbyrunCalibDeviceSpec(bool useCCDB, const std::string& outputDir, const std::string& metaFileDir, bool writeRootOutput);
6166
} // namespace phos
6267
} // namespace o2
6368

Detectors/PHOS/calib/include/PHOSCalibWorkflow/PHOSRunbyrunCalibrator.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class PHOSRunbyrunSlot
4545

4646
void print() const;
4747
void fill(const gsl::span<const Cluster>& clusters, const gsl::span<const TriggerRecord>& trs);
48-
void fill(const gsl::span<const Cluster>& /*clusters*/){}; //not used
48+
void fill(const gsl::span<const Cluster>& /*clusters*/){}; // not used
4949
void merge(const PHOSRunbyrunSlot* prev);
5050
void clear();
5151

@@ -58,12 +58,12 @@ class PHOSRunbyrunSlot
5858

5959
private:
6060
bool mUseCCDB = false;
61-
long mRunStartTime = 0; /// start time of the run (sec)
62-
float mPtCut = 1.5; /// ptmin of a pair cut (GeV/c)
63-
std::string mCCDBPath{"http://alice-ccdb.cern.ch"}; /// CCDB path to retrieve current CCDB objects for comparison
64-
std::array<boostHisto, 8> mReMi; /// Real and Mixed inv mass distributions per module
65-
std::unique_ptr<RingBuffer> mBuffer; /// Buffer for current and previous events
66-
std::unique_ptr<BadChannelsMap> mBadMap; /// Latest bad channels map
61+
long mRunStartTime = 0; /// start time of the run (sec)
62+
float mPtCut = 1.5; /// ptmin of a pair cut (GeV/c)
63+
std::string mCCDBPath{"http://alice-ccdb.cern.ch"}; /// CCDB path to retrieve current CCDB objects for comparison
64+
std::array<boostHisto, 8> mReMi; /// Real and Mixed inv mass distributions per module
65+
std::unique_ptr<RingBuffer> mBuffer; /// Buffer for current and previous events
66+
std::unique_ptr<BadChannelsMap> mBadMap; /// Latest bad channels map
6767

6868
ClassDefNV(PHOSRunbyrunSlot, 1);
6969
};
@@ -84,9 +84,9 @@ class PHOSRunbyrunCalibrator final : public o2::calibration::TimeSlotCalibration
8484
bool process(TFType tf, const gsl::span<const Cluster>& clu, const gsl::span<const TriggerRecord>& trs);
8585

8686
std::array<float, 8> getCalibration() { return mRunByRun; }
87-
void endOfStream();
87+
void writeHistos();
8888

89-
//Functions used in histo fittings
89+
// Functions used in histo fittings
9090
double CBRatio(double* x, double* p);
9191
double CBSignal(double* x, double* p);
9292
double bg(double* x, double* p);
@@ -97,10 +97,10 @@ class PHOSRunbyrunCalibrator final : public o2::calibration::TimeSlotCalibration
9797

9898
private:
9999
bool mUseCCDB = false;
100-
long mRunStartTime = 0; /// start time of the run (sec)
101-
std::string mCCDBPath{"http://alice-ccdb.cern.ch"}; /// CCDB path to retrieve current CCDB objects for comparison
102-
std::array<float, 8> mRunByRun; /// Final calibration object
103-
std::array<TH1F*, 8> mReMi; /// Real and Mixed inv mass distributions per module
100+
long mRunStartTime = 0; /// start time of the run (sec)
101+
std::string mCCDBPath{"http://alice-ccdb.cern.ch"}; /// CCDB path to retrieve current CCDB objects for comparison
102+
std::array<float, 8> mRunByRun; /// Final calibration object
103+
std::array<TH1F*, 8> mReMi; /// Real and Mixed inv mass distributions per module
104104

105105
ClassDefOverride(PHOSRunbyrunCalibrator, 1);
106106
};

Detectors/PHOS/calib/src/PHOSEnergyCalibDevice.cxx

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ using namespace o2::phos;
3131
void PHOSEnergyCalibDevice::init(o2::framework::InitContext& ic)
3232
{
3333
o2::base::GRPGeomHelper::instance().setRequest(mCCDBRequest);
34-
mOutputDir = ic.options().get<std::string>("output-dir");
35-
mOutputDir = o2::utils::Str::rectifyDirectory(mOutputDir);
36-
mMetaFileDir = ic.options().get<std::string>("meta-output-dir");
37-
mMetaFileDir = o2::utils::Str::rectifyDirectory(mMetaFileDir);
38-
LOG(info) << "Meta dir=" << mMetaFileDir;
3934

4035
mPtMin = ic.options().get<float>("ptminmgg");
4136
mEminHGTime = ic.options().get<float>("eminhgtime");
@@ -51,6 +46,7 @@ void PHOSEnergyCalibDevice::init(o2::framework::InitContext& ic)
5146
LOG(info) << " Emin for LG time=" << mEminLGTime;
5247
LOG(info) << " Emin for out digits=" << mEDigMin;
5348
LOG(info) << " Cluster Emin for out digits=" << mECluMin;
49+
LOG(info) << " Root output dir=" << mOutputDir;
5450

5551
mCalibrator = std::make_unique<PHOSEnergyCalibrator>();
5652

@@ -64,6 +60,7 @@ void PHOSEnergyCalibDevice::init(o2::framework::InitContext& ic)
6460
mHasCalib = true;
6561
}
6662
mCalibrator->setCuts(mPtMin, mEminHGTime, mEminLGTime, mEminHGTime, mEminLGTime);
63+
mCalibrator->setUpdateAtTheEndOfRunOnly();
6764

6865
// Create geometry instance (inclusing reading mis-alignement)
6966
// instance will be pick up by Calibrator
@@ -148,14 +145,17 @@ void PHOSEnergyCalibDevice::fillOutputTree()
148145

149146
LOG(info) << "Filling tree with " << mOutputDigits.size() << " digits";
150147
if (!mFileOut) { // create file and tree
151-
mFileName = mOutputDir + fmt::format("PHOS_CalibDigits_{}.root", mRunNumber);
152-
LOG(info) << "Creating new tree for PHOS calib digits, output file=" << mFileName;
153-
mFileOut = std::make_unique<TFile>(mFileName.c_str(), "recreate");
148+
if (mWriteRootOutput) {
149+
mFileName = mOutputDir + fmt::format("PHOS_CalibDigits_{}.root", mRunNumber);
150+
LOG(info) << "Creating new tree for PHOS calib digits, output file=" << mFileName;
151+
mFileOut = std::make_unique<TFile>(mFileName.c_str(), "recreate");
152+
mFileMetaData = std::make_unique<o2::dataformats::FileMetaData>();
153+
mHistoFileName = mOutputDir + fmt::format("PHOS_CalibHistos_{}.root", mRunNumber);
154+
mHistoFileOut = std::make_unique<TFile>(mHistoFileName.c_str(), "recreate");
155+
mHistoFileMetaData = std::make_unique<o2::dataformats::FileMetaData>();
156+
}
157+
// else Tree will be memory resident
154158
mTreeOut = std::make_unique<TTree>("phosCalibDig", "O2 PHOS calib tree");
155-
mFileMetaData = std::make_unique<o2::dataformats::FileMetaData>();
156-
mHistoFileName = mOutputDir + fmt::format("PHOS_CalibHistos_{}.root", mRunNumber);
157-
mHistoFileOut = std::make_unique<TFile>(mHistoFileName.c_str(), "recreate");
158-
mHistoFileMetaData = std::make_unique<o2::dataformats::FileMetaData>();
159159
}
160160
auto* br = mTreeOut->GetBranch("PHOSCalib");
161161
auto* pptr = &mOutputDigits;
@@ -172,6 +172,9 @@ void PHOSEnergyCalibDevice::fillOutputTree()
172172

173173
void PHOSEnergyCalibDevice::writeOutFile()
174174
{
175+
if (!mWriteRootOutput) {
176+
return;
177+
}
175178
// write collected vector and metadata
176179
if (!mTreeOut) { // nothing to write,
177180
return;
@@ -192,15 +195,19 @@ void PHOSEnergyCalibDevice::writeOutFile()
192195

193196
std::string metaFileNameTmp = mMetaFileDir + fmt::format("PHOS_CalibDigits_{}.tmp", mRunNumber);
194197
std::string metaFileName = mMetaFileDir + fmt::format("PHOS_CalibDigits_{}.done", mRunNumber);
195-
try {
196-
std::ofstream metaFileOut(metaFileNameTmp);
197-
metaFileOut << *mFileMetaData.get();
198-
metaFileOut.close();
199-
std::filesystem::rename(metaFileNameTmp, metaFileName);
200-
} catch (std::exception const& e) {
201-
LOG(error) << "Failed to store PHOS meta data file " << metaFileName << ", reason: " << e.what();
198+
if (mMetaFileDir.compare("/dev/null")) {
199+
try {
200+
std::ofstream metaFileOut(metaFileNameTmp);
201+
metaFileOut << *mFileMetaData.get();
202+
metaFileOut.close();
203+
std::filesystem::rename(metaFileNameTmp, metaFileName);
204+
} catch (std::exception const& e) {
205+
LOG(error) << "Failed to store PHOS meta data file " << metaFileName << ", reason: " << e.what();
206+
}
207+
LOG(info) << "Stored metadate file " << mFileName << ".done";
208+
} else {
209+
LOG(info) << "Scipped storing metafile as meta-dir=" << mMetaFileDir;
202210
}
203-
LOG(info) << "Stored metadate file " << mFileName << ".done";
204211
mFileMetaData.reset();
205212

206213
LOG(info) << "Writing calibration histograms";
@@ -217,18 +224,20 @@ void PHOSEnergyCalibDevice::writeOutFile()
217224

218225
metaFileNameTmp = mMetaFileDir + fmt::format("PHOS_CalibHistos_{}.tmp", mRunNumber);
219226
metaFileName = mMetaFileDir + fmt::format("PHOS_CalibHistos_{}.done", mRunNumber);
220-
try {
221-
std::ofstream metaFileOut(metaFileNameTmp);
222-
metaFileOut << *mHistoFileMetaData.get();
223-
metaFileOut.close();
224-
std::filesystem::rename(metaFileNameTmp, metaFileName);
225-
} catch (std::exception const& e) {
226-
LOG(error) << "Failed to store PHOS histos meta data file " << metaFileName << ", reason: " << e.what();
227+
if (mMetaFileDir.compare("/dev/null")) {
228+
try {
229+
std::ofstream metaFileOut(metaFileNameTmp);
230+
metaFileOut << *mHistoFileMetaData.get();
231+
metaFileOut.close();
232+
std::filesystem::rename(metaFileNameTmp, metaFileName);
233+
} catch (std::exception const& e) {
234+
LOG(error) << "Failed to store PHOS histos meta data file " << metaFileName << ", reason: " << e.what();
235+
}
236+
LOG(info) << "Stored histos metadate file " << mHistoFileName << ".done";
227237
}
228-
LOG(info) << "Stored histos metadate file " << mHistoFileName << ".done";
229238
mHistoFileMetaData.reset();
230239
}
231-
o2::framework::DataProcessorSpec o2::phos::getPHOSEnergyCalibDeviceSpec(bool useCCDB)
240+
o2::framework::DataProcessorSpec o2::phos::getPHOSEnergyCalibDeviceSpec(bool useCCDB, const std::string& outputDir, const std::string& metaFileDir, bool writeRootOutput)
232241
{
233242

234243
std::vector<InputSpec> inputs;
@@ -252,13 +261,11 @@ o2::framework::DataProcessorSpec o2::phos::getPHOSEnergyCalibDeviceSpec(bool use
252261
return o2::framework::DataProcessorSpec{"PHOSEnergyCalibDevice",
253262
inputs,
254263
outputs,
255-
o2::framework::adaptFromTask<PHOSEnergyCalibDevice>(useCCDB, ccdbRequest),
264+
o2::framework::adaptFromTask<PHOSEnergyCalibDevice>(useCCDB, ccdbRequest, outputDir, metaFileDir, writeRootOutput),
256265
o2::framework::Options{
257266
{"ptminmgg", o2::framework::VariantType::Float, 1.5f, {"minimal pt to fill mgg calib histos"}},
258267
{"eminhgtime", o2::framework::VariantType::Float, 1.5f, {"minimal E (GeV) to fill HG time calib histos"}},
259268
{"eminlgtime", o2::framework::VariantType::Float, 5.f, {"minimal E (GeV) to fill LG time calib histos"}},
260269
{"ecalibdigitmin", o2::framework::VariantType::Float, 0.05f, {"minimal digtit E (GeV) to keep digit for calibration"}},
261-
{"ecalibclumin", o2::framework::VariantType::Float, 0.4f, {"minimal cluster E (GeV) to keep digit for calibration"}},
262-
{"output-dir", VariantType::String, "./", {"ROOT trees output directory"}},
263-
{"meta-output-dir", VariantType::String, "./", {"metafile output directory"}}}};
270+
{"ecalibclumin", o2::framework::VariantType::Float, 0.4f, {"minimal cluster E (GeV) to keep digit for calibration"}}}};
264271
}

0 commit comments

Comments
 (0)