FairShip
Loading...
Searching...
No Matches
test_split_set.cxx
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2// SPDX-FileCopyrightText: Copyright CERN for the benefit of the SHiP
3// Collaboration
4
5// The split set of a ShipMCTrack must default to "not split", survive copies
6// and I/O, and read back as "not split" from files written before it existed.
7
8#include <TClonesArray.h>
9#include <TFile.h>
10#include <TTree.h>
11
12#include <cstdio>
13#include <iostream>
14#include <memory>
15
16#include "ShipMCTrack.h"
17
18namespace {
19
20int failures = 0;
21
22void Check(bool ok, const char* what) {
23 if (!ok) {
24 std::cerr << "FAIL " << what << std::endl;
25 ++failures;
26 }
27}
28
29} // namespace
30
31int main(int argc, char** argv) {
32 ShipMCTrack plain;
33 Check(plain.GetSplitSet() == -1, "default split set");
34 Check(plain.GetSplitRole() == ShipMCTrack::kNotSplit, "default split role");
35 Check(!plain.IsSplitDecay() && !plain.IsSplitSurvivor(), "default flags");
36
37 ShipMCTrack clone(321, 0, 0., 0., 50., 50.002, 0., 0., 10., 0.3, 0, 0, 7,
38 0.25);
39 Check(clone.GetSplitSet() == -1, "standard constructor split set");
41 Check(clone.IsSplitDecay() && !clone.IsSplitSurvivor(), "decay role");
42 const ShipMCTrack copy(clone);
43 Check(copy.GetSplitSet() == 3 && copy.IsSplitDecay() &&
44 copy.GetSplitWeight() == 0.5,
45 "copy keeps split set");
46
47 // Round trip through a file.
48 const char* fname = "test_split_set.root";
49 {
50 TFile f(fname, "RECREATE");
51 ShipMCTrack survivor(clone);
53 f.WriteObject(&clone, "clone");
54 f.WriteObject(&survivor, "survivor");
55 }
56 {
57 TFile f(fname);
58 std::unique_ptr<ShipMCTrack> c(f.Get<ShipMCTrack>("clone"));
59 std::unique_ptr<ShipMCTrack> s(f.Get<ShipMCTrack>("survivor"));
60 Check(c && c->GetSplitSet() == 3 && c->IsSplitDecay() &&
61 c->GetSplitWeight() == 0.5,
62 "read back clone");
63 Check(s && s->GetSplitSet() == 3 && s->IsSplitSurvivor(),
64 "read back survivor");
65 }
66 std::remove(fname);
67
68 // A file written before the split set existed (ShipMCTrack version 9).
69 if (argc > 1) {
70 TFile f(argv[1]);
71 auto* tree = f.Get<TTree>("cbmsim");
72 Check(tree != nullptr, "old file has cbmsim");
73 if (tree) {
74 // This file predates the move to std::vector: MCTrack is a TClonesArray.
75 TClonesArray* tracks = nullptr;
76 tree->SetBranchAddress("MCTrack", &tracks);
77 Long64_t n = 0;
78 bool allUnsplit = true;
79 for (Long64_t i = 0; i < tree->GetEntries(); ++i) {
80 tree->GetEntry(i);
81 for (Int_t j = 0; tracks && j < tracks->GetEntriesFast(); ++j) {
82 const auto* t = dynamic_cast<const ShipMCTrack*>(tracks->At(j));
83 if (!t) continue;
84 ++n;
85 allUnsplit &= t->GetSplitSet() == -1 &&
86 t->GetSplitRole() == ShipMCTrack::kNotSplit;
87 }
88 }
89 Check(n > 0, "old file has tracks");
90 Check(allUnsplit, "old tracks read back as not split");
91 tree->ResetBranchAddresses();
92 delete tracks;
93 }
94 }
95
96 if (failures) {
97 std::cerr << failures << " check(s) failed" << std::endl;
98 return 1;
99 }
100 return 0;
101}
Bool_t IsSplitDecay() const
Bool_t IsSplitSurvivor() const
void SetSplitSet(Int_t splitSet, Int_t role, Double_t splitWeight)
Int_t GetSplitRole() const
Double_t GetSplitWeight() const
@ kSplitDecay
a clone standing for one decay of the split track
Definition ShipMCTrack.h:34
@ kNotSplit
not part of a split set
Definition ShipMCTrack.h:32
@ kSplitSurvivor
the split track itself, or its continuation
Definition ShipMCTrack.h:33
Int_t GetSplitSet() const