BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
Data.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2023.
4
5This file is part of BDSIM.
6
7BDSIM is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published
9by the Free Software Foundation version 3 of the License.
10
11BDSIM is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#include "Data.hh"
20
21#include "Rtypes.h"
22#include "TDirectory.h"
23#include "TFile.h"
24#include "TTree.h"
25
26#include "BDSOutputROOTEventHeader.hh"
27
28#include <map>
29#include <string>
30#include <vector>
31
32ClassImp(DataDummyClass)
33
34DataDummyClass::DataDummyClass()
35{;}
36
37DataDummyClass::~DataDummyClass()
38{;}
39
40TFile* DataDummyClass::CreateEmptyRebdsimFile(const std::string& fileName,
41 unsigned long long int nOriginalEventsIn)
42{
43 return RBDS::CreateEmptyRebdsimFile(fileName, nOriginalEventsIn);
44}
45
46std::map<std::string, TDirectory*> DataDummyClass::CreateDirectories(TFile* outputFile,
47 std::string treeName)
48{
49 return RBDS::CreateDirectories(outputFile, treeName);
50}
51
52TFile* RBDS::CreateEmptyRebdsimFile(const std::string& fileName,
53 unsigned long long int nOriginalEventsIn)
54{
55 TFile* outputFile = new TFile(fileName.c_str(), "RECREATE");
56
57 outputFile->cd();
59 headerOut->Fill(); // updates time stamp
60 headerOut->SetFileType("REBDSIM");
61 headerOut->nOriginalEvents = nOriginalEventsIn;
62
63 TTree* headerTree = new TTree("Header", "REBDSIM Header");
64 headerTree->Branch("Header.", "BDSOutputROOTEventHeader", headerOut);
65 headerTree->Fill();
66 headerTree->Write("", TObject::kOverwrite);
67
68 std::vector<std::string> expectedTrees = {"Beam.",
69 "Event.",
70 "Run.",
71 "Options.",
72 "Model."};
73 for (const auto& treeName : expectedTrees)
74 {RBDS::CreateDirectories(outputFile, treeName);}
75
76 return outputFile;
77}
78
79std::map<std::string, TDirectory*> RBDS::CreateDirectories(TFile* outputFile,
80 std::string treeName)
81{
82 // treeName typically has a "." at the end, deleting it here:
83 std::string cleanedName = treeName.erase(treeName.size() - 1);
84 std::string perEntryDirName = "PerEntryHistograms";
85 std::string simpleDirName = "SimpleHistograms";
86 std::string mergedDirName = "MergedHistograms";
87 // We have to pedantically check the existance of directories in the
88 // case we're overwriting a file, ROOT will segfault classically if
89 // the directory already exists ignoring the overwriting mode.
90 // Always more code to compensate for ROOT.
91 TDirectory* rebdsimDir = outputFile->GetDirectory(cleanedName.c_str());
92 if (!rebdsimDir)
93 {rebdsimDir = outputFile->mkdir(cleanedName.c_str());}
94 rebdsimDir->cd();
95
96 TDirectory* perEntryDir = rebdsimDir->GetDirectory(perEntryDirName.c_str());
97 if (!perEntryDir)
98 {rebdsimDir->mkdir(perEntryDirName.c_str());}
99
100 TDirectory* simpleDir = rebdsimDir->GetDirectory(simpleDirName.c_str());
101 if (!simpleDir)
102 {rebdsimDir->mkdir(simpleDirName.c_str());}
103
104 TDirectory* mergedDir = rebdsimDir->GetDirectory(mergedDirName.c_str());
105 if (!mergedDir)
106 {rebdsimDir->mkdir(mergedDirName.c_str());}
107
108 std::map<std::string, TDirectory*> result = {{cleanedName, rebdsimDir},
109 {perEntryDirName, perEntryDir},
110 {simpleDirName, simpleDir},
111 {mergedDirName, mergedDir}};
112 return result;
113}
Information about the software and the file.
void SetFileType(const std::string &fileTypeIn)
Update the file type.
unsigned long long int nOriginalEvents
Number of original events if skimmed.
void Fill(const std::vector< std::string > &analysedFilesIn=std::vector< std::string >(), const std::vector< std::string > &combinedFilesIn=std::vector< std::string >())
A dummy class as ROOT won't seem to import namespaced functions in CLING for python.
Definition: Data.hh:56
TFile * CreateEmptyRebdsimFile(const std::string &fileName, unsigned long long int nOriginalEventsIn=1)
Create a new TFile and add a header to it. The header object is filled and written.
Definition: Data.cc:52
std::map< std::string, TDirectory * > CreateDirectories(TFile *outputFile, std::string treeName)
Definition: Data.cc:79