GRSISort "v4.0.0.5"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
TAnalysisOptions.cxx
Go to the documentation of this file.
1#include "TAnalysisOptions.h"
2
3#include <algorithm>
4#include <cctype>
5#include <iostream>
6
7#include "TEnv.h"
8#include "TKey.h"
9
10#include "Globals.h"
11#include "DynamicLibrary.h"
12#include "TGRSIUtilities.h"
13#include "GRootCommands.h"
14
16{
17 /// Clears all of the variables in the TAnalysisOptions
18 fBuildWindow = 2000;
20 fAddbackWindow = 300;
21 fSuppressionWindow = 300.;
23 fStaticWindow = false;
24 fWaveformFitting = false;
26}
27
28void TAnalysisOptions::Print(Option_t*) const
29{
30 /// Print the current status of TAnalysisOptions, includes all names, lists and flags
31 std::cout << BLUE << "fBuildWindow: " << DCYAN << fBuildWindow << std::endl
32 << BLUE << "fBuildEventsByTimeStamp: " << DCYAN << fBuildEventsByTimeStamp << std::endl
33 << BLUE << "fAddbackWindow: " << DCYAN << fAddbackWindow << std::endl
34 << BLUE << "fSuppressionWindow: " << DCYAN << fSuppressionWindow << std::endl
35 << BLUE << "fSuppressionEnergy: " << DCYAN << fSuppressionEnergy << std::endl
36 << BLUE << "fStaticWindow: " << DCYAN << fStaticWindow << std::endl
37 << BLUE << "fWaveformFitting: " << DCYAN << fWaveformFitting << std::endl
38 << BLUE << "fIsCorrectingCrossTalk: " << DCYAN << fIsCorrectingCrossTalk << std::endl
39 << RESET_COLOR << std::endl;
40}
41
42bool TAnalysisOptions::WriteToFile(const std::string& fileName)
43{
44 TFile file(fileName.c_str(), "update");
45 if(file.IsOpen()) {
46 return WriteToFile(&file);
47 }
48 std::cout << R"(Failed to open file ")" << fileName << R"(" in update mode!)" << std::endl;
49 return false;
50}
51
53{
54 /// Writes options information to the root file
55 // Maintain old gDirectory info
56 bool success = true;
57 TDirectory* oldDir = gDirectory;
58
59 // if no file was provided, try to use the current file
60 if(file == nullptr) {
61 file = gDirectory->GetFile();
62 }
63 // check if we got a file
64 if(file == nullptr) {
65 std::cout << "Error, no file provided and no file open (gDirectory = " << gDirectory->GetName() << ")!" << std::endl;
66 return !success;
67 }
68
69 file->cd();
70 std::string oldoption = std::string(file->GetOption());
71 if(oldoption == "READ") {
72 file->ReOpen("UPDATE");
73 }
74
75 // check again that we have a directory to write to
76 if(!gDirectory) { // we don't compare to nullptr here, as ROOT >= 6.24.00 uses the TDirectoryAtomicAdapter structure with a bool() operator
77 std::cout << "No file opened to write TAnalysisOptions to." << std::endl;
78 return !success;
79 }
80 // write analysis options
81 std::cout << "Writing TAnalysisOptions to " << gDirectory->GetFile()->GetName() << std::endl;
82 Write("AnalysisOptions", TObject::kOverwrite);
83
84 // check if we need to change back to read mode
85 if(oldoption == "READ") {
86 std::cout << " Returning " << gDirectory->GetFile()->GetName() << " to \"" << oldoption.c_str() << "\" mode." << std::endl;
87 file->ReOpen("READ");
88 }
89
90 // go back to original gDirectory
91 oldDir->cd();
92
93 return success;
94}
95
96void TAnalysisOptions::ReadFromFile(const std::string& fileName)
97{
98 TFile file(fileName.c_str(), "read");
99 if(file.IsOpen()) {
100 return ReadFromFile(&file);
101 }
102 std::cout << R"(Failed to open file ")" << fileName << R"(" in read mode!)" << std::endl;
103}
104
106{
107 TDirectory* oldDir = gDirectory;
108 if(file != nullptr && file->IsOpen()) {
109 TList* list = file->GetListOfKeys();
110 TIter iter(list);
111 while(TKey* key = static_cast<TKey*>(iter.Next())) {
112 if((key == nullptr) || (strcmp(key->GetClassName(), "TAnalysisOptions") != 0)) {
113 continue;
114 }
115
116 *this = *static_cast<TAnalysisOptions*>(key->ReadObj());
117 oldDir->cd();
118 return;
119 }
120 std::cout << R"(Failed to find analysis options in file ")" << CYAN << file->GetName() << RESET_COLOR << R"(":)" << std::endl;
121 } else {
122 std::cout << R"(File ")" << file << R"(" is null or not open)" << std::endl;
123 }
124 oldDir->cd();
125}
126
127void TAnalysisOptions::SetCorrectCrossTalk(const bool& flag, Option_t* opt)
128{
130 TString opt1 = opt;
131 opt1.ToUpper();
132 if(opt1.Contains("Q")) {
133 return;
134 }
135
136 std::cout << "Please call TDetector::ResetFlags() on current event to avoid bugs" << std::endl;
137}
138
140{
141 bool match = true;
142 if(fBuildWindow != option->fBuildWindow) {
143 std::cout << "Build window " << fBuildWindow << " != " << option->fBuildWindow << std::endl;
144 match = false;
145 }
147 std::cout << "Building events by timestamp " << (fBuildEventsByTimeStamp ? "enabled" : "disabled") << " != " << (option->fBuildEventsByTimeStamp ? "enabled" : "disabled") << std::endl;
148 match = false;
149 }
150 if(fAddbackWindow != option->fAddbackWindow) {
151 std::cout << "Addback window " << fAddbackWindow << " != " << option->fAddbackWindow << std::endl;
152 match = false;
153 }
155 std::cout << "Suppression window " << fSuppressionWindow << " != " << option->fSuppressionWindow << std::endl;
156 match = false;
157 }
159 std::cout << "Suppression window " << fSuppressionEnergy << " != " << option->fSuppressionEnergy << std::endl;
160 match = false;
161 }
163 std::cout << "Cross-talk correction " << (fIsCorrectingCrossTalk ? "enabled" : "disabled") << " != " << (option->fIsCorrectingCrossTalk ? "enabled" : "disabled") << std::endl;
164 match = false;
165 }
166 if(fWaveformFitting != option->fWaveformFitting) {
167 std::cout << "Waveform fitting " << (fWaveformFitting ? "enabled" : "disabled") << " != " << (option->fWaveformFitting ? "enabled" : "disabled") << std::endl;
168 match = false;
169 }
170 if(fStaticWindow != option->fStaticWindow) {
171 std::cout << "Static window " << (fStaticWindow ? "enabled" : "disabled") << " != " << (option->fStaticWindow ? "enabled" : "disabled") << std::endl;
172 match = false;
173 }
174
175 return match;
176}
177
178Long64_t TAnalysisOptions::Merge(TCollection* list, Option_t*)
179{
180 // Attempting to copy what TH1 is doing.
181 if(list == nullptr) {
182 return 0;
183 }
184 // TH1 returns GetEntries if the list is empty, we just return 1?
185 if(list->IsEmpty()) {
186 return 1;
187 }
188 // TH1 uses TH1Merger with fH0 set to this, adding list to fInputList.
189 // The functor compares the histograms, and if they are compatible merges them by looping over all histograms in the input list and adding them to fH0, i.e. this
190 // So we just loop over all analysis options in the list and compare their values.
191 // If there's a mismatch, we print that to stdout.
192 for(auto* option : *list) {
193 if(option->IsA() != TAnalysisOptions::Class()) {
194 std::cout << "Don't know how to merge object of class \"" << option->ClassName() << "\" with TAnalysisOptions, skipping it!" << std::endl;
195 continue;
196 }
197 if(!Compare(static_cast<TAnalysisOptions*>(option))) {
198 continue;
199 }
200 }
201
202 return 1;
203}
#define BLUE
Definition Globals.h:6
#define DCYAN
Definition Globals.h:21
#define CYAN
Definition Globals.h:12
#define RESET_COLOR
Definition Globals.h:5
void Write(const std::shared_ptr< TMidasEvent > &evt, TMidasFile *outfile)
bool fIsCorrectingCrossTalk
True if we are correcting for cross-talk in GRIFFIN at analysis-level.
void SetCorrectCrossTalk(const bool &flag, Option_t *opt="")
bool WriteToFile(const std::string &file)
double fSuppressionEnergy
Minimum energy used to suppress Ge-Events. (default = 0 keV)
int64_t fBuildWindow
if building with a window(GRIFFIN) this is the size of the window. (default = 2us (2000))
void Print(Option_t *opt="") const override
double fSuppressionWindow
Time used to suppress Ge-Events. (default = 300 ns (300))
void Clear(Option_t *opt="") override
Long64_t Merge(TCollection *list)
bool fBuildEventsByTimeStamp
use time stamps instead of time (including CFD) to build events
double fAddbackWindow
Time used to build Addback-Ge-Events for TIGRESS/GRIFFIN. (default = 300 ns (300))
bool fWaveformFitting
If true, waveform fitting with SFU algorithm will be performed.
bool Compare(const TAnalysisOptions *options) const
void ReadFromFile(const std::string &file)
bool fStaticWindow
Flag to use static window (default moving)