Skip to content

Commit be9daac

Browse files
committed
TPC SCD calib: Ues time stamps for filenames and some fixes for merging slots
1 parent 3aba537 commit be9daac

3 files changed

Lines changed: 44 additions & 19 deletions

File tree

Detectors/GlobalTrackingWorkflow/tpcinterpolationworkflow/include/TPCInterpolationWorkflow/TPCResidualAggregatorSpec.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ class ResidualAggregatorDevice : public o2::framework::Task
7373
storeMetaFile = false;
7474
}
7575

76+
LOGP(info, "Creating aggregator with {} entries per voxel minimum. Output file writing enabled: {}, meta file writing enabled: {}",
77+
minEnt, writeOutput, storeMetaFile);
7678
mAggregator = std::make_unique<o2::tpc::ResidualAggregator>(minEnt);
7779
if (writeOutput) {
7880
mAggregator->setOutputDir(outputDir);
@@ -110,6 +112,10 @@ class ResidualAggregatorDevice : public o2::framework::Task
110112
updateTimeDependentParams(pc);
111113
std::chrono::duration<double, std::milli> ccdbUpdateTime = std::chrono::high_resolution_clock::now() - runStartTime;
112114

115+
// assume that the orbit reset time (given here in ms) can change within a run
116+
auto orbitResetTime = o2::base::GRPGeomHelper::instance().getOrbitResetTimeMS();
117+
118+
// we always require the unbinned residuals and the associated track references
113119
auto residualsData = pc.inputs().get<gsl::span<o2::tpc::UnbinnedResid>>("unbinnedRes");
114120
auto trackRefs = pc.inputs().get<gsl::span<o2::tpc::TrackDataCompact>>("trackRefs");
115121

@@ -133,7 +139,7 @@ class ResidualAggregatorDevice : public o2::framework::Task
133139

134140
o2::base::TFIDInfoHelper::fillTFIDInfo(pc, mAggregator->getCurrentTFInfo());
135141
LOG(info) << "Processing TF " << mAggregator->getCurrentTFInfo().tfCounter << " with " << trkData->size() << " tracks and " << residualsData.size() << " unbinned residuals associated to them";
136-
mAggregator->process(residualsData, trackRefs, trkDataPtr, lumi);
142+
mAggregator->process(residualsData, trackRefs, orbitResetTime, trkDataPtr, lumi);
137143
std::chrono::duration<double, std::milli> runDuration = std::chrono::high_resolution_clock::now() - runStartTime;
138144
LOGP(info, "Duration for run method: {} ms. From this taken for time dependent param update: {} ms",
139145
std::chrono::duration_cast<std::chrono::milliseconds>(runDuration).count(),

Detectors/TPC/calibration/SpacePoints/include/SpacePoints/ResidualAggregator.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct ResidualsContainer {
4949
void fillStatisticsBranches();
5050
uint64_t getNEntries() const { return nResidualsTotal; }
5151

52-
void fill(const o2::dataformats::TFIDInfo& ti, const gsl::span<const UnbinnedResid> resid, const gsl::span<const o2::tpc::TrackDataCompact> trkRefsIn, const gsl::span<const o2::tpc::TrackData>* trkDataIn, const o2::ctp::LumiInfo* lumiInput);
52+
void fill(const o2::dataformats::TFIDInfo& ti, const gsl::span<const UnbinnedResid> resid, const gsl::span<const o2::tpc::TrackDataCompact> trkRefsIn, long orbitResetTime, const gsl::span<const o2::tpc::TrackData>* trkDataIn, const o2::ctp::LumiInfo* lumiInput);
5353
void merge(ResidualsContainer* prev);
5454
void print();
5555
void writeToFile(bool closeFileAfterwards);
@@ -59,7 +59,6 @@ struct ResidualsContainer {
5959
std::array<std::vector<TrackResiduals::LocalResid>*, SECTORSPERSIDE * SIDES> residualsPtr{};
6060
std::array<std::vector<TrackResiduals::VoxStats>, SECTORSPERSIDE * SIDES> stats{}; ///< voxel statistics sent to the aggregator
6161
std::array<std::vector<TrackResiduals::VoxStats>*, SECTORSPERSIDE * SIDES> statsPtr{};
62-
uint32_t runNumber; ///< run number (required for meta data file)
6362
std::vector<uint32_t> tfOrbits, *tfOrbitsPtr{&tfOrbits}; ///< first TF orbit
6463
std::vector<uint32_t> sumOfResiduals, *sumOfResidualsPtr{&sumOfResiduals}; ///< sum of residuals for each TF
6564
std::vector<o2::ctp::LumiInfo> lumi, *lumiPtr{&lumi}; ///< luminosity information from CTP per TF
@@ -68,9 +67,6 @@ struct ResidualsContainer {
6867
std::vector<TrackDataCompact> trackInfo, *trackInfoPtr{&trackInfo}; ///< allows to obtain track type for each binned residual downstream
6968

7069
std::string fileName{"o2tpc_residuals"};
71-
std::string treeNameResiduals{"resid"};
72-
std::string treeNameStats{"stats"};
73-
std::string treeNameRecords{"records"};
7470
std::unique_ptr<TFile> fileOut{nullptr};
7571
std::unique_ptr<TTree> treeOutResidualsUnbinned{nullptr};
7672
std::unique_ptr<TTree> treeOutTrackData{nullptr};
@@ -86,11 +82,13 @@ struct ResidualsContainer {
8682
int autosaveInterval{0}; ///< if > 0, then the output written to file for every n-th TF
8783

8884
// additional info
85+
long orbitReset{0}; ///< current orbit reset time in ms
86+
uint32_t firstTForbit{0}; ///< stored for the first seen TF to allow conversion to time stamp
8987
TFType firstSeenTF{o2::calibration::INFINITE_TF}; ///< the first TF which was added to this container
9088
TFType lastSeenTF{0}; ///< the last TF which was added to this container
9189
uint64_t nResidualsTotal{0}; ///< the total number of residuals for this container
9290

93-
ClassDefNV(ResidualsContainer, 4);
91+
ClassDefNV(ResidualsContainer, 5);
9492
};
9593

9694
class ResidualAggregator final : public o2::calibration::TimeSlotCalibration<ResidualsContainer>

Detectors/TPC/calibration/SpacePoints/src/ResidualAggregator.cxx

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ using namespace o2::tpc;
2727
ResidualsContainer::~ResidualsContainer()
2828
{
2929
// trees must be deleted before the file is closed, otherwise segfaults
30+
LOGP(debug, "Deleting ResidualsContainer with {} entries. File name is {}", getNEntries(), fileName);
3031
treeOutResidualsUnbinned.reset();
3132
treeOutTrackData.reset();
3233
treeOutResiduals.reset();
3334
treeOutStats.reset();
3435
treeOutRecords.reset();
3536
if (fileOut) {
37+
LOG(debug) << "Removing output file of discarded slot";
3638
// this slot was not finalized, need to close and remove the file
3739
fileOut->Close();
3840
fileOut.reset();
@@ -52,26 +54,30 @@ ResidualsContainer::ResidualsContainer(const ResidualsContainer& rhs)
5254

5355
ResidualsContainer::ResidualsContainer(ResidualsContainer&& rhs)
5456
{
57+
LOGP(debug, "Move operator called for rhs with {} entries", rhs.nResidualsTotal);
5558
trackResiduals = rhs.trackResiduals;
5659
fileOut = std::move(rhs.fileOut);
5760
fileName = std::move(rhs.fileName);
5861
treeOutResidualsUnbinned = std::move(rhs.treeOutResidualsUnbinned);
5962
treeOutTrackData = std::move(rhs.treeOutTrackData);
6063
treeOutResiduals = std::move(rhs.treeOutResiduals);
6164
treeOutStats = std::move(rhs.treeOutStats);
65+
treeOutRecords = std::move(rhs.treeOutRecords);
6266
for (int iSec = 0; iSec < SECTORSPERSIDE * SIDES; ++iSec) {
6367
residuals[iSec] = std::move(rhs.residuals[iSec]);
6468
stats[iSec] = std::move(rhs.stats[iSec]);
6569
}
66-
runNumber = rhs.runNumber;
6770
tfOrbits = std::move(rhs.tfOrbits);
6871
sumOfResiduals = std::move(rhs.sumOfResiduals);
6972
lumi = std::move(rhs.lumi);
7073
unbinnedRes = std::move(rhs.unbinnedRes);
7174
trackInfo = std::move(rhs.trackInfo);
7275
trkData = std::move(rhs.trkData);
76+
orbitReset = rhs.orbitReset;
77+
firstTForbit = rhs.firstTForbit;
7378
firstSeenTF = rhs.firstSeenTF;
7479
lastSeenTF = rhs.lastSeenTF;
80+
nResidualsTotal = rhs.nResidualsTotal;
7581
}
7682

7783
void ResidualsContainer::init(const TrackResiduals* residualsEngine, std::string outputDir, bool wFile, bool wBinnedResid, bool wUnbinnedResid, bool wTrackData, int autosave, int compression)
@@ -98,9 +104,9 @@ void ResidualsContainer::init(const TrackResiduals* residualsEngine, std::string
98104
treeOutTrackData->Branch("trk", &trkDataPtr);
99105
}
100106
if (writeBinnedResid) {
101-
treeOutResiduals = std::make_unique<TTree>(treeNameResiduals.c_str(), "TPC binned residuals");
102-
treeOutStats = std::make_unique<TTree>(treeNameStats.c_str(), "Voxel statistics mean position and nEntries");
103-
treeOutRecords = std::make_unique<TTree>(treeNameRecords.c_str(), "Statistics per TF slot");
107+
treeOutResiduals = std::make_unique<TTree>("resid", "TPC binned residuals");
108+
treeOutStats = std::make_unique<TTree>("stats", "Voxel statistics mean position and nEntries");
109+
treeOutRecords = std::make_unique<TTree>("records", "Statistics per TF slot");
104110
for (int iSec = 0; iSec < SECTORSPERSIDE * SIDES; ++iSec) {
105111
residualsPtr[iSec] = &residuals[iSec];
106112
statsPtr[iSec] = &stats[iSec];
@@ -122,6 +128,7 @@ void ResidualsContainer::init(const TrackResiduals* residualsEngine, std::string
122128
treeOutRecords->Branch("sumOfResiduals", &sumOfResidualsPtr);
123129
treeOutRecords->Branch("lumi", &lumiPtr);
124130
}
131+
LOG(debug) << "Done initializing residuals container for file named " << fileName;
125132
}
126133

127134
void ResidualsContainer::fillStatisticsBranches()
@@ -135,7 +142,7 @@ void ResidualsContainer::fillStatisticsBranches()
135142
}
136143
}
137144

138-
void ResidualsContainer::fill(const o2::dataformats::TFIDInfo& ti, const gsl::span<const UnbinnedResid> resid, const gsl::span<const o2::tpc::TrackDataCompact> trkRefsIn, const gsl::span<const o2::tpc::TrackData>* trkDataIn, const o2::ctp::LumiInfo* lumiInput)
145+
void ResidualsContainer::fill(const o2::dataformats::TFIDInfo& ti, const gsl::span<const UnbinnedResid> resid, const gsl::span<const o2::tpc::TrackDataCompact> trkRefsIn, long orbitResetTime, const gsl::span<const o2::tpc::TrackData>* trkDataIn, const o2::ctp::LumiInfo* lumiInput)
139146
{
140147
// receives large vector of unbinned residuals and fills the sector-wise vectors
141148
// with binned residuals and statistics
@@ -145,6 +152,8 @@ void ResidualsContainer::fill(const o2::dataformats::TFIDInfo& ti, const gsl::sp
145152
lastSeenTF = ti.tfCounter;
146153
}
147154
if (ti.tfCounter < firstSeenTF) {
155+
orbitReset = orbitResetTime;
156+
firstTForbit = ti.firstTForbit;
148157
firstSeenTF = ti.tfCounter;
149158
}
150159
for (const auto& residIn : resid) {
@@ -206,7 +215,6 @@ void ResidualsContainer::fill(const o2::dataformats::TFIDInfo& ti, const gsl::sp
206215
treeOutResidualsUnbinned->Fill();
207216
unbinnedRes.clear();
208217
}
209-
runNumber = ti.runNumber;
210218
tfOrbits.push_back(ti.firstTForbit);
211219
if (lumiInput) {
212220
lumi.push_back(*lumiInput);
@@ -255,7 +263,7 @@ void ResidualsContainer::merge(ResidualsContainer* prev)
255263
{
256264
// the previous slot is merged to this one and afterwards
257265
// the previous one will be deleted
258-
LOG(info) << "Merging previous slot into current one";
266+
LOGP(debug, "Merging previous slot with {} entries into current one with {} entries", prev->getNEntries(), getNEntries());
259267
if (writeBinnedResid) {
260268
for (int iSec = 0; iSec < SECTORSPERSIDE * SIDES; ++iSec) {
261269
// merge statistics
@@ -275,8 +283,9 @@ void ResidualsContainer::merge(ResidualsContainer* prev)
275283
stat.nEntries += statPrev.nEntries;
276284
}
277285
// prepare merging of residuals
278-
prev->treeOutResiduals->SetBranchAddress(Form("sec%d", iSec), &residualsPtr);
286+
prev->treeOutResiduals->SetBranchAddress(Form("sec%d", iSec), &residualsPtr[iSec]);
279287
}
288+
prev->treeOutResiduals->SetBranchAddress("trackInfo", &trackInfoPtr);
280289
// We append the entries of the tree of the following slot to the
281290
// previous slot and afterwards move the merged tree to this slot.
282291
// This way the order of the entries is preserved
@@ -305,6 +314,12 @@ void ResidualsContainer::merge(ResidualsContainer* prev)
305314
treeOutTrackData = std::move(prev->treeOutTrackData);
306315
treeOutResidualsUnbinned = std::move(prev->treeOutResidualsUnbinned);
307316

317+
// since we want to continue using the TTrees of the previous slot, we must
318+
// avoid that ROOT deletes them when the TFile of the previous slot is erased
319+
treeOutResiduals->SetDirectory(fileOut.get());
320+
treeOutTrackData->SetDirectory(fileOut.get());
321+
treeOutResidualsUnbinned->SetDirectory(fileOut.get());
322+
308323
nResidualsTotal += prev->nResidualsTotal;
309324

310325
// append the current vector to the vector of the previous container and afterwards swap them,
@@ -317,6 +332,7 @@ void ResidualsContainer::merge(ResidualsContainer* prev)
317332
std::swap(prev->lumi, lumi);
318333

319334
firstSeenTF = prev->firstSeenTF;
335+
LOGP(debug, "Done with the merge. Current slot has {} entries", getNEntries());
320336
}
321337

322338
void ResidualsContainer::print()
@@ -334,8 +350,9 @@ ResidualAggregator::~ResidualAggregator()
334350

335351
bool ResidualAggregator::hasEnoughData(const Slot& slot) const
336352
{
337-
LOG(debug) << "There are " << slot.getContainer()->getNEntries() << " entries currently. Min entries prt voxel: " << mMinEntries;
353+
LOG(debug) << "There are " << slot.getContainer()->getNEntries() << " entries currently. Min entries per voxel: " << mMinEntries;
338354
auto entriesPerVoxel = slot.getContainer()->getNEntries() / (mTrackResiduals.getNVoxelsPerSector() * SECTORSPERSIDE * SIDES);
355+
LOGP(debug, "Slot has {} entries per voxel, at least {} are required", entriesPerVoxel, mMinEntries);
339356
return entriesPerVoxel >= mMinEntries;
340357
}
341358

@@ -350,13 +367,17 @@ void ResidualAggregator::finalizeSlot(Slot& slot)
350367
auto finalizeStartTime = std::chrono::high_resolution_clock::now();
351368
auto cont = slot.getContainer();
352369
cont->print();
353-
if (!mWriteOutput) {
354-
LOG(info) << "Skip writing output, since file output is disabled";
370+
if (!mWriteOutput || cont->getNEntries() == 0) {
371+
LOGP(info, "Skip writing output with {} entries, since file output is disabled or slot is empty", cont->getNEntries());
355372
return;
356373
}
357374
cont->writeToFile(true);
358375

359-
auto fileName = fmt::format("o2tpc_residuals_{}_{}_{}_{}.root", slot.getTFStart(), slot.getTFEnd(), cont->firstSeenTF, cont->lastSeenTF);
376+
long orbitOffsetStart = (cont->firstSeenTF - slot.getTFStart()) * o2::base::GRPGeomHelper::getNHBFPerTF();
377+
long orbitOffsetEnd = (slot.getTFEnd() - cont->firstSeenTF) * o2::base::GRPGeomHelper::getNHBFPerTF();
378+
long timeStartMS = cont->orbitReset + (cont->firstTForbit - orbitOffsetStart) * o2::constants::lhc::LHCOrbitMUS * 1.e-3;
379+
long timeEndMS = cont->orbitReset + (cont->firstTForbit + orbitOffsetEnd) * o2::constants::lhc::LHCOrbitMUS * 1.e-3;
380+
auto fileName = fmt::format("o2tpc_residuals_{}_{}_{}_{}.root", timeStartMS, timeEndMS, slot.getTFStart(), slot.getTFEnd());
360381
auto fileNameWithPath = mOutputDir + fileName;
361382
std::filesystem::rename(o2::utils::Str::concat_string(mOutputDir, cont->fileName, ".part"), fileNameWithPath);
362383
if (mStoreMetaData) {

0 commit comments

Comments
 (0)