|
19 | 19 | #include <numeric> // for iota |
20 | 20 | #include <MathUtils/Cartesian.h> |
21 | 21 | #include <DataFormatsCalibration/MeanVertexObject.h> |
| 22 | +#include <filesystem> |
22 | 23 |
|
23 | 24 | using namespace o2::steer; |
24 | 25 |
|
@@ -196,10 +197,52 @@ o2::parameters::GRPObject const& DigitizationContext::getGRP() const |
196 | 197 |
|
197 | 198 | void DigitizationContext::saveToFile(std::string_view filename) const |
198 | 199 | { |
| 200 | + // checks if the path content of filename exists ... otherwise it is created before creating the ROOT file |
| 201 | + auto ensure_path_exists = [](std::string_view filename) { |
| 202 | + try { |
| 203 | + // Extract the directory path from the filename |
| 204 | + std::filesystem::path file_path(filename); |
| 205 | + std::filesystem::path dir_path = file_path.parent_path(); |
| 206 | + |
| 207 | + // Check if the directory path is empty (which means filename was just a name without path) |
| 208 | + if (dir_path.empty()) { |
| 209 | + // nothing to do |
| 210 | + return true; |
| 211 | + } |
| 212 | + |
| 213 | + // Create directories if they do not exist |
| 214 | + if (!std::filesystem::exists(dir_path)) { |
| 215 | + if (std::filesystem::create_directories(dir_path)) { |
| 216 | + // std::cout << "Directories created successfully: " << dir_path.string() << std::endl; |
| 217 | + return true; |
| 218 | + } else { |
| 219 | + std::cerr << "Failed to create directories: " << dir_path.string() << std::endl; |
| 220 | + return false; |
| 221 | + } |
| 222 | + } |
| 223 | + return true; |
| 224 | + } catch (const std::filesystem::filesystem_error& ex) { |
| 225 | + std::cerr << "Filesystem error: " << ex.what() << std::endl; |
| 226 | + return false; |
| 227 | + } catch (const std::exception& ex) { |
| 228 | + std::cerr << "General error: " << ex.what() << std::endl; |
| 229 | + return false; |
| 230 | + } |
| 231 | + }; |
| 232 | + |
| 233 | + if (!ensure_path_exists(filename)) { |
| 234 | + LOG(error) << "Filename contains path component which could not be created"; |
| 235 | + return; |
| 236 | + } |
| 237 | + |
199 | 238 | TFile file(filename.data(), "RECREATE"); |
200 | | - auto cl = TClass::GetClass(typeid(*this)); |
201 | | - file.WriteObjectAny(this, cl, "DigitizationContext"); |
202 | | - file.Close(); |
| 239 | + if (file.IsOpen()) { |
| 240 | + auto cl = TClass::GetClass(typeid(*this)); |
| 241 | + file.WriteObjectAny(this, cl, "DigitizationContext"); |
| 242 | + file.Close(); |
| 243 | + } else { |
| 244 | + LOG(error) << "Could not write to file " << filename.data(); |
| 245 | + } |
203 | 246 | } |
204 | 247 |
|
205 | 248 | DigitizationContext* DigitizationContext::loadFromFile(std::string_view filename) |
@@ -391,13 +434,15 @@ void DigitizationContext::applyMaxCollisionFilter(long startOrbit, long orbitsPe |
391 | 434 | mEventParts = newparts; |
392 | 435 | } |
393 | 436 |
|
394 | | -void DigitizationContext::finalizeTimeframeStructure(long startOrbit, long orbitsPerTF) |
| 437 | +int DigitizationContext::finalizeTimeframeStructure(long startOrbit, long orbitsPerTF) |
395 | 438 | { |
396 | 439 | mTimeFrameStartIndex = getTimeFrameBoundaries(mEventRecords, startOrbit, orbitsPerTF); |
397 | 440 | LOG(info) << "Fixed " << mTimeFrameStartIndex.size() << " timeframes "; |
398 | 441 | for (auto p : mTimeFrameStartIndex) { |
399 | 442 | LOG(info) << p.first << " " << p.second; |
400 | 443 | } |
| 444 | + |
| 445 | + return mTimeFrameStartIndex.size(); |
401 | 446 | } |
402 | 447 |
|
403 | 448 | std::unordered_map<int, int> DigitizationContext::getCollisionIndicesForSource(int source) const |
@@ -483,3 +528,55 @@ void DigitizationContext::sampleInteractionVertices(o2::dataformats::MeanVertexO |
483 | 528 | } |
484 | 529 | } |
485 | 530 | } |
| 531 | + |
| 532 | +DigitizationContext DigitizationContext::extractSingleTimeframe(int timeframeid, std::vector<int> const& sources_to_offset) |
| 533 | +{ |
| 534 | + DigitizationContext r; // make a return object |
| 535 | + if (mTimeFrameStartIndex.size() == 0) { |
| 536 | + LOG(error) << "No timeframe structure determined; Returning empty object. Please call ::finalizeTimeframeStructure before calling this function"; |
| 537 | + return r; |
| 538 | + } |
| 539 | + r.mSimPrefixes = mSimPrefixes; |
| 540 | + r.mMuBC = mMuBC; |
| 541 | + try { |
| 542 | + auto startend = mTimeFrameStartIndex.at(timeframeid); |
| 543 | + |
| 544 | + auto startindex = startend.first; |
| 545 | + auto endindex = startend.second; |
| 546 | + |
| 547 | + std::copy(mEventRecords.begin() + startindex, mEventRecords.begin() + endindex, std::back_inserter(r.mEventRecords)); |
| 548 | + std::copy(mEventParts.begin() + startindex, mEventParts.begin() + endindex, std::back_inserter(r.mEventParts)); |
| 549 | + if (mInteractionVertices.size() > endindex) { |
| 550 | + std::copy(mInteractionVertices.begin() + startindex, mInteractionVertices.begin() + endindex, std::back_inserter(r.mInteractionVertices)); |
| 551 | + } |
| 552 | + |
| 553 | + // let's assume we want to fix the ids for source = source_id |
| 554 | + // Then we find the first index that has this source_id and take the corresponding number |
| 555 | + // as offset. Thereafter we subtract this offset from all known event parts. |
| 556 | + auto perform_offsetting = [&r](int source_id) { |
| 557 | + auto indices_for_source = r.getCollisionIndicesForSource(source_id); |
| 558 | + int minvalue = std::numeric_limits<int>::max(); |
| 559 | + for (auto& p : indices_for_source) { |
| 560 | + if (p.first < minvalue) { |
| 561 | + minvalue = p.first; |
| 562 | + } |
| 563 | + } |
| 564 | + // now fix them |
| 565 | + for (auto& p : indices_for_source) { |
| 566 | + auto index_into_mEventParts = p.second; |
| 567 | + for (auto& part : r.mEventParts[index_into_mEventParts]) { |
| 568 | + if (part.sourceID == source_id) { |
| 569 | + part.entryID -= minvalue; |
| 570 | + } |
| 571 | + } |
| 572 | + } |
| 573 | + }; |
| 574 | + for (auto source_id : sources_to_offset) { |
| 575 | + perform_offsetting(source_id); |
| 576 | + } |
| 577 | + |
| 578 | + } catch (std::exception) { |
| 579 | + LOG(warn) << "No such timeframe id in collision context. Returing empty object"; |
| 580 | + } |
| 581 | + return r; |
| 582 | +} |
0 commit comments