Skip to content

Commit d69ae7b

Browse files
committed
o2-sim: Provide standalone MC header information
Extracting (without breaking backward compatibility) the MC header information into a separate file. This file will be small and can quickly copied/provided to unblock start of signal transport in cases of embedding. The motivation is background event caching on the GRID. The headers file can be copied quickly and the rest of kinematics + hits may follow asyncronously since they are only needed at much later stages (digitization).
1 parent ee4f8a4 commit d69ae7b

File tree

5 files changed

+57
-1
lines changed

5 files changed

+57
-1
lines changed

DataFormats/Detectors/Common/include/DetectorsCommonDataFormats/NameConf.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class NameConf
4949
return o2::utils::concat_string(prefix, "_", DIGITS_STRING, d.getName(), ".root");
5050
}
5151

52-
// Filename to store kinematics + TrackRefs
52+
// Filename to store general run parameters (GRP)
5353
static std::string getGRPFileName(const std::string_view prefix = STANDARDSIMPREFIX)
5454
{
5555
return o2::utils::concat_string(prefix, "_", GRP_STRING, ".root");
@@ -61,6 +61,12 @@ class NameConf
6161
return o2::utils::concat_string(prefix, "_", KINE_STRING, ".root");
6262
}
6363

64+
// Filename to store kinematics + TrackRefs
65+
static std::string getMCHeadersFileName(const std::string_view prefix = STANDARDSIMPREFIX)
66+
{
67+
return o2::utils::concat_string(prefix, "_", MCHEADER_STRING, ".root");
68+
}
69+
6470
// Filename to store final MC configuration file
6571
static std::string getMCConfigFileName(const std::string_view prefix = STANDARDSIMPREFIX)
6672
{
@@ -104,6 +110,7 @@ class NameConf
104110
static constexpr std::string_view DIGITS_STRING = "Digits"; // hardcoded
105111
static constexpr std::string_view GRP_STRING = "grp"; // hardcoded
106112
static constexpr std::string_view KINE_STRING = "Kine"; // hardcoded
113+
static constexpr std::string_view MCHEADER_STRING = "MCHeader"; // hardcoded
107114
static constexpr std::string_view GEOM_FILE_STRING = "geometry";
108115
static constexpr std::string_view CUT_FILE_STRING = "proc-cut";
109116
static constexpr std::string_view CONFIG_STRING = "configuration";

DataFormats/simulation/include/SimulationDataFormat/MCEventHeader.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ class MCEventHeader : public FairMCEventHeader
8585

8686
MCEventStats& getMCEventStats() { return mEventStats; }
8787

88+
/// create a standalone ROOT file/tree with only the MCHeader branch
89+
static void extractFileFromKinematics(std::string_view kinefilename, std::string_view targetfilename);
90+
8891
protected:
8992
std::string mEmbeddingFileName;
9093
Int_t mEmbeddingEventIndex = 0;

DataFormats/simulation/src/MCEventHeader.cxx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#include "SimulationDataFormat/MCEventHeader.h"
1414
#include "FairRootManager.h"
15+
#include <TFile.h>
16+
#include <TTree.h>
1517

1618
namespace o2
1719
{
@@ -32,6 +34,32 @@ void MCEventHeader::Reset()
3234
mEmbeddingEventIndex = 0;
3335
}
3436

37+
void MCEventHeader::extractFileFromKinematics(std::string_view kinefilename, std::string_view targetfilename)
38+
{
39+
auto oldfile = TFile::Open(kinefilename.data());
40+
auto kinetree = (TTree*)oldfile->Get("o2sim");
41+
// deactivate all branches
42+
kinetree->SetBranchStatus("*", 0);
43+
// activate the header branch
44+
kinetree->SetBranchStatus("MCEventHeader*", 1);
45+
// create a new file + a clone of old tree header. Do not copy events
46+
auto newfile = TFile::Open(targetfilename.data(), "RECREATE");
47+
auto newtree = kinetree->CloneTree(0);
48+
// here we copy the branches
49+
newtree->CopyEntries(kinetree, kinetree->GetEntries());
50+
newtree->SetEntries(kinetree->GetEntries());
51+
// flush to disk
52+
newtree->Write();
53+
newfile->Close();
54+
delete newfile;
55+
56+
// clean
57+
if (oldfile) {
58+
oldfile->Close();
59+
delete oldfile;
60+
}
61+
}
62+
3563
/*****************************************************************/
3664
/*****************************************************************/
3765

macro/migrateSimFiles.C

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ void migrateSimFiles(const char* filebase = "o2sim")
7575
auto kinematicsfile = o2::base::NameConf::getMCKinematicsFileName(filebase);
7676
copyBranch(originalfilename.c_str(), kinematicsfile.c_str(), o2::detectors::SimTraits::KINEMATICSBRANCHES);
7777

78+
// split off additional MCHeaders file
79+
std::vector<std::string> headerbranches = {"MCEventHeader"};
80+
auto headersfile = o2::base::NameConf::getMCHeadersFileName(filebase);
81+
copyBranch(originalfilename.c_str(), headersfile.c_str(), headerbranches);
82+
7883
// loop over all possible detectors
7984
for (auto detid = o2::detectors::DetID::First; detid <= o2::detectors::DetID::Last; ++detid) {
8085
if (!grp->isDetReadOut(detid)) {

run/o2sim_parallel.cxx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "Framework/FreePortFinder.h"
3030
#include <sys/types.h>
3131
#include "DetectorsCommonDataFormats/NameConf.h"
32+
#include "SimulationDataFormat/MCEventHeader.h"
3233

3334
#include "rapidjson/document.h"
3435
#include "rapidjson/writer.h"
@@ -458,6 +459,18 @@ int main(int argc, char* argv[])
458459
// (mainly useful for continuous integration / automated testing suite)
459460
auto returncode = checkresult();
460461
if (returncode == 0) {
462+
// Extract a single file for MCEventHeaders
463+
// This file will be small and can quickly unblock start of signal transport (in embedding).
464+
// This is useful when we cache background events on the GRID. The headers file can be copied quickly
465+
// and the rest of kinematics + Hits may follow asyncronously since they are only needed at much
466+
// later stages (digitization).
467+
468+
auto& conf = o2::conf::SimConfig::Instance();
469+
// easy check: see if we have number of entries in output tree == number of events asked
470+
std::string kinefilename = o2::base::NameConf::getMCKinematicsFileName(conf.getOutPrefix().c_str());
471+
std::string headerfilename = o2::base::NameConf::getMCHeadersFileName(conf.getOutPrefix().c_str());
472+
o2::dataformats::MCEventHeader::extractFileFromKinematics(kinefilename, headerfilename);
473+
461474
LOG(INFO) << "SIMULATION RETURNED SUCCESFULLY";
462475
}
463476

0 commit comments

Comments
 (0)