hpstr
The Heavy Photon Search Toolkit for Reconstruction (hpstr) provides an interface to physics data from the HPS experiment saved in the LCIO format and converts it into an ROOT based format.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
FlatTupleMaker.cxx
Go to the documentation of this file.
1/*
2 * @file FlatTupleMaker.cxx
3 * @author Omar Moreno
4 * @date January 18, 2016
5 * @author PF
6 * @date Jan, 2020
7 * @brief
8 *
9 */
10
11#include <FlatTupleMaker.h>
12
13FlatTupleMaker::FlatTupleMaker(std::string file_name, std::string tree_name) {
14
15 file = new TFile(file_name.c_str(), "RECREATE");
16
17 tree = new TTree(tree_name.c_str(), tree_name.c_str());
18
19}
20
21//Will save it in current open file
22FlatTupleMaker::FlatTupleMaker(std::string tree_name) {
23 file = nullptr;
24 tree = new TTree(tree_name.c_str(), tree_name.c_str());
25}
26
28 if (file)
29 delete file;
30 if (tree)
31 delete tree;
32}
33
34void FlatTupleMaker::addVariable(std::string variable_name) {
35
36 // Set the default value of the variable to something unrealistic
37 variables[variable_name] = -9999;
38
39 // Add a leaf to the ROOT tree and set its address to the address of the
40 // newly created variable.
41 tree->Branch(variable_name.c_str(), &variables[variable_name], (variable_name + "/D").c_str());
42}
43
44void FlatTupleMaker::addString(std::string variable_name) {
45
46 // Set the default value of the variable to something unrealistic
47 string_variables[variable_name] ="EMPTY";
48
49 // Add a leaf to the ROOT tree and set its address to the address of the
50 // newly created variable.
51 tree->Branch(variable_name.c_str(), &string_variables[variable_name]);
52}
53void FlatTupleMaker::addVector(std::string variable_name) {
54 vectors[variable_name] = {};
55 tree->Branch(variable_name.c_str(), &vectors[variable_name]);
56}
57
58void FlatTupleMaker::addToVector(std::string variable_name, double value) {
59 vectors[variable_name].push_back(value);
60}
61
62bool FlatTupleMaker::hasVariable(std::string variable_name) {
63
64 auto search = variables.find(variable_name);
65 if (search != variables.end()) return true;
66
67 return false;
68}
69
71 if (file) {
72 file->Write();
73 file->Close();
74 }
75}
76
77std::vector<double> FlatTupleMaker::getVector(std::string variable_name) {
78 return vectors[variable_name];
79}
80
82
83 // Fill the event with the current variables.
84 tree->Fill();
85
86 // Reset the variables to their original values
87 for (auto& element : variables) {
88 element.second = -9999;
89 }
90
91 for (auto& element : vectors) {
92 element.second.clear();
93 }
94}
std::vector< double > getVector(std::string variable_name)
description
void addVector(std::string vector_name)
description
void addVariable(std::string variable_name)
description
std::map< std::string, std::vector< double > > vectors
void close()
description
std::map< std::string, double > variables
FlatTupleMaker(std::string file_name, std::string tree_name)
Constructor.
void addToVector(std::string variable_name, double value)
description
bool hasVariable(std::string variable_name)
description
void addString(std::string variable_name)
description
void fill()
description
std::map< std::string, std::string > string_variables