GRSISort "v4.0.0.5"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
TGRSISelector.cxx
Go to the documentation of this file.
1// The class definition in TGRSISelector.h has been generated automatically
2// by the ROOT utility TTree::MakeSelector(). This class is derived
3// from the ROOT class TSelector. For more information on the TSelector
4// framework see $ROOTSYS/README/README.SELECTOR or the ROOT User Manual.
5
6// The following methods are defined in this file:
7// Begin(): called every time a loop on the tree starts,
8// a convenient place to create your histograms.
9// SlaveBegin(): called after Begin(), when on PROOF called only on the
10// slave servers.
11// Process(): called for each event, in this function you decide what
12// to read and fill your histograms.
13// SlaveTerminate: called at the end of the loop on the tree, when on PROOF
14// called only on the slave servers.
15// Terminate(): called at the end of the loop on the tree,
16// a convenient place to draw/fit your histograms.
17//
18// To use this file, try the following session on your Tree T:
19//
20// Root > T->Process("TGRSISelector.C")
21// Root > T->Process("TGRSISelector.C","some options")
22// Root > T->Process("TGRSISelector.C+")
23//
24
25#include "TGRSIOptions.h"
26#include "TRunInfo.h"
27#include "TGRSISelector.h"
28#include "GValue.h"
29#include "TParserLibrary.h"
30
31#include "TBufferFile.h"
32#include "TSystem.h"
33#include "TH2.h"
34#include "TStyle.h"
35
36void TGRSISelector::Begin(TTree* /*tree*/)
37{
38 /// The Begin() function is called at the start of the query.
39 /// When running with PROOF Begin() is only called on the client.
40 /// The tree argument is deprecated (on PROOF 0 is passed).
41 TString option = GetOption();
42}
43
44void TGRSISelector::SlaveBegin(TTree* /*tree*/)
45{
46 /// The SlaveBegin() function is called after the Begin() function.
47 /// When running with PROOF SlaveBegin() is called on each slave server.
48 /// The tree argument is deprecated (on PROOF 0 is passed).
49 TString option = GetOption();
50
51 std::cout << "input list size = " << fInput->GetEntries() << std::endl;
52 for(int i = 0; i < fInput->GetEntries(); ++i) {
53 std::cout << fInput->At(i)->GetName() << ": ";
54 fInput->At(i)->Print();
55 }
56
57 // read the analysis options that were passed along and copy them to the local TGRSIOptions
58 fAnalysisOptions = static_cast<TAnalysisOptions*>(fInput->FindObject("TAnalysisOptions"));
60 // read the TPPG that was passed along
61 fPpg = static_cast<TPPG*>(fInput->FindObject("TPPG"));
62 if(fPpg == nullptr) {
63 std::cerr << "failed to find TPPG!" << std::endl;
64 }
65
66 // read the TRunInfo that was passed along
67 fRunInfo = static_cast<TRunInfo*>(fInput->FindObject("TRunInfo"));
68 if(fRunInfo == nullptr) {
69 std::cerr << "failed to find TRunInfo!" << std::endl;
70 }
71
72 // if we have a data parser/detector library load it
73 if(fInput->FindObject("ParserLibrary") != nullptr) {
74 std::string library(fInput->FindObject("ParserLibrary")->GetTitle());
76 if(!library.empty()) {
77 // this might throw a runtime exception, but we don't want to catch it here as we need the library for things to work properly!
79 } else {
80 std::cout << "no parser library!" << std::endl;
82 }
83 }
84
85 const char* workingDirectory = "";
86 if(fInput->FindObject("pwd") != nullptr) {
87 workingDirectory = fInput->FindObject("pwd")->GetTitle();
88 }
89 int index = 0;
90 while(fInput->FindObject(Form("calFile%d", index)) != nullptr) {
91 const char* fileName = static_cast<TNamed*>(fInput->FindObject(Form("calFile%d", index)))->GetTitle();
92 if(fileName[0] == 0) {
93 std::cout << "Error, empty file name!" << std::endl;
94 break;
95 }
96 // if we have a relative path and a working directory, combine them
97 if(workingDirectory[0] != 0 && fileName[0] != '/') {
98 TChannel::ReadCalFile(Form("%s/%s", workingDirectory, fileName));
99 } else {
100 TChannel::ReadCalFile(fileName);
101 }
102 ++index;
103 }
104 index = 0;
105 while(fInput->FindObject(Form("valFile%d", index)) != nullptr) {
106 const char* fileName = static_cast<TNamed*>(fInput->FindObject(Form("valFile%d", index)))->GetTitle();
107 if(fileName[0] == 0) {
108 std::cout << "Error, empty file name!" << std::endl;
109 break;
110 }
111 // if we have a relative path and a working directory, combine them
112 if(workingDirectory[0] != 0 && fileName[0] != '/') {
113 GValue::ReadValFile(Form("%s/%s", workingDirectory, fileName));
114 } else {
115 GValue::ReadValFile(fileName);
116 }
117 ++index;
118 }
119 index = 0;
120 while(fInput->FindObject(Form("cutFile%d", index)) != nullptr) {
121 std::cout << "trying to open " << Form("cutFile%d", index) << std::flush << " = " << fInput->FindObject(Form("cutFile%d", index)) << std::flush << " with title " << static_cast<TNamed*>(fInput->FindObject(Form("cutFile%d", index)))->GetTitle() << std::endl;
122 const char* fileName = static_cast<TNamed*>(fInput->FindObject(Form("cutFile%d", index)))->GetTitle();
123 if(fileName[0] == 0) {
124 std::cout << "Error, empty file name!" << std::endl;
125 break;
126 }
127 // if we have a relative path and a working directory, combine them
128 TFile* file = nullptr;
129 if(workingDirectory[0] != 0 && fileName[0] != '/') {
130 file = new TFile(Form("%s/%s", workingDirectory, fileName));
131 } else {
132 file = new TFile(fileName);
133 }
134 if(file != nullptr && file->IsOpen()) {
135 TIter iter(file->GetListOfKeys());
136 TKey* key = nullptr;
137 while((key = static_cast<TKey*>(iter.Next())) != nullptr) {
138 if(strcmp(key->GetClassName(), "TCutG") != 0) {
139 continue;
140 }
141 auto* tmpCut = static_cast<TCutG*>(key->ReadObj());
142 if(tmpCut != nullptr) {
143 fCuts[tmpCut->GetName()] = tmpCut;
144 }
145 }
146 } else {
147 std::cout << "Error, failed to open file " << fileName << "!" << std::endl;
148 break;
149 }
150 ++index;
151 }
152 for(const auto& cut : fCuts) {
153 std::cout << cut.first << " = " << cut.second << std::endl;
154 }
155
156 if(GValue::Size() == 0) {
157 std::cout << "No g-values!" << std::endl;
158 } else {
159 std::cout << GValue::Size() << " g-values" << std::endl;
160 }
161
163 CheckSizes("use");
164}
165
166Bool_t TGRSISelector::Process(Long64_t entry)
167{
168 /// The Process() function is called for each entry in the tree (or possibly
169 /// keyed object in the case of PROOF) to be processed. The entry argument
170 /// specifies which entry in the currently loaded tree is to be processed.
171 /// It can be passed to either TGRSISelector::GetEntry() or TBranch::GetEntry()
172 /// to read either all or the required parts of the data. When processing
173 /// keyed objects with PROOF, the object is already loaded and is available
174 /// via the fObject pointer.
175 ///
176 /// This function should contain the "body" of the analysis. It can contain
177 /// simple or elaborate selection criteria, run algorithms on the data
178 /// of the event and typically fill histograms.
179 ///
180 /// The processing can be stopped by calling Abort().
181 ///
182 /// Use fStatus to set the return value of TTree::Process().
183 ///
184 /// The return value is currently not used
185
186 static TFile* current_file = nullptr;
187 if(current_file != fChain->GetCurrentFile()) {
188 current_file = fChain->GetCurrentFile();
189 std::cout << "Starting to sort: " << current_file->GetName() << std::endl;
190 TChannel::ReadCalFromFile(current_file);
192 }
193
194 fChain->GetEntry(entry);
195 fEntry = entry;
196 try {
199 std::cout << DRED << "Exception in " << __PRETTY_FUNCTION__ << ": " << e.detail() << RESET_COLOR << std::endl; // NOLINT(cppcoreguidelines-pro-type-const-cast, cppcoreguidelines-pro-bounds-array-to-pointer-decay)
200 throw e;
201 }
202
203 return kTRUE;
204}
205
207{
208 /// The SlaveTerminate() function is called after all entries or objects
209 /// have been processed. When running with PROOF SlaveTerminate() is called
210 /// on each slave server.
211
212 // check size of each object in the output list
213 EndOfSort();
214 CheckSizes("send to server");
215 fOutput->Add(new TChannel(TChannel::GetChannelMap()->begin()->second));
216 fOutput->Add(new TNamed("prefix", GetOutputPrefix().c_str()));
217}
218
220{
221 /// The Terminate() function is the last function to be called during
222 /// a query. It always runs on the client, it can be used to present
223 /// the results graphically or save the results to file.
224
225 CheckSizes("write");
226
227 TGRSIOptions* options = TGRSIOptions::Get();
228 if(fRunInfo == nullptr) {
230 std::cout << "replaced null run info with:" << std::endl;
231 fRunInfo->Print();
232 }
233 Int_t runNumber = TRunInfo::RunNumber();
234 Int_t subRunNumber = TRunInfo::SubRunNumber();
235
236 if(fOutput->FindObject("prefix") != nullptr) {
237 fOutputPrefix = static_cast<TNamed*>(fOutput->FindObject("prefix"))->GetTitle();
238 }
239
240 std::string outputFileName;
241 if(runNumber != 0 && subRunNumber != -1) {
242 // both run and subrun number set => single file processed
243 outputFileName = Form("%s%05d_%03d.root", fOutputPrefix.c_str(), runNumber, subRunNumber);
244 } else if(runNumber != 0) {
245 // multiple subruns of a single run
246 outputFileName = Form("%s%05d_%03d-%03d.root", fOutputPrefix.c_str(), runNumber, TRunInfo::FirstSubRunNumber(), TRunInfo::LastSubRunNumber());
247 } else {
248 // multiple runs
249 outputFileName = Form("%s%05d-%05d.root", fOutputPrefix.c_str(), TRunInfo::FirstRunNumber(), TRunInfo::LastRunNumber());
250 }
251 auto* outputFile = new TFile(outputFileName.c_str(), "recreate");
252 if(!outputFile->IsOpen()) {
253 std::cerr << "Failed to open output file " << outputFileName << "!" << std::endl
254 << std::endl;
255 return;
256 }
257 outputFileName = outputFileName.substr(0, outputFileName.find_last_of('.')) + ".log";
258 options->LogFile(outputFileName);
259 std::cout << "Opened '" << outputFile->GetName() << "' for writing:" << std::endl;
260
261 outputFile->cd();
262 fOutput->Write();
263 fRunInfo->Write();
264 if(fPpg != nullptr) {
265 fPpg->Write();
266 } else {
267 std::cerr << "failed to find TPPG, can't write it!" << std::endl;
268 }
271 outputFile->Close();
272 std::cout << "Closed '" << outputFile->GetName() << "'" << std::endl;
273}
274
275void TGRSISelector::Init(TTree* tree)
276{
277 /// The Init() function is called when the selector needs to initialize
278 /// a new tree or chain. Typically here the branch addresses and branch
279 /// pointers of the tree will be set.
280 /// It is normally not necessary to make changes to the generated
281 /// code, but the routine can be extended by the user if needed.
282 /// Init() will be called many times when running on PROOF
283 /// (once per file to be processed).
284 /// Set branch addresses and branch pointers
285 if(tree == nullptr) {
286 return;
287 }
288 fChain = tree;
289 InitializeBranches(tree);
290}
291
293{
294 /// The Notify() function is called when a new file is opened. This
295 /// can be either for a new TTree in a TChain or when when a new TTree
296 /// is started when using PROOF. It is normally not necessary to make changes
297 /// to the generated code, but the routine can be extended by the
298 /// user if needed. The return value is currently not used.
299
300 return kTRUE;
301}
302
303void TGRSISelector::CheckSizes(const char* usage)
304{
305 // check size of each object in the output list
306 for(const auto&& obj : *fOutput) {
307 TBufferFile buf(TBuffer::kWrite, 10000);
308 obj->IsA()->WriteBuffer(buf, obj);
309 if(buf.Length() > fSizeLimit) {
310 std::cout << DRED << obj->ClassName() << " '" << obj->GetName() << "' too large to " << usage << ": " << buf.Length() << " bytes = " << buf.Length() / 1024. / 1024. / 1024. << " GB, removing it!" << RESET_COLOR << std::endl;
311 // we only remove it from the output list, not deleting the object itself
312 // this way the selector will still work and fill that histogram, it just won't get written to file
313 fOutput->Remove(obj);
314 }
315 }
316}
#define DRED
Definition Globals.h:18
#define RESET_COLOR
Definition Globals.h:5
static int ReadValFile(const char *filename="", Option_t *opt="replace")
Definition GValue.cxx:194
static int Size()
Definition GValue.h:63
bool WriteToFile(const std::string &file)
void ReadFromFile(const std::string &file)
static int WriteToRoot(TFile *fileptr=nullptr)
static std::unordered_map< unsigned int, TChannel * > * GetChannelMap()
Definition TChannel.h:72
static Int_t ReadCalFile(std::ifstream &infile)
static Int_t ReadCalFromFile(TFile *tempf, Option_t *opt="overwrite")
std::string detail() const noexcept
Definition TGRSIMap.h:112
static TGRSIOptions * Get(int argc=0, char **argv=nullptr)
Do not use!
std::string LogFile() const
static TAnalysisOptions * AnalysisOptions()
void Print(Option_t *opt="") const override
void ParserLibrary(std::string &library)
void SlaveBegin(TTree *tree) override
Bool_t Notify() override
virtual void FillHistograms()=0
void CheckSizes(const char *usage)
Function to check size of objects in output list.
void SlaveTerminate() override
TTree * fChain
! pointer to the analyzed TTree or TChain
static constexpr int fSizeLimit
! 1 GB size limit for objects in ROOT
TRunInfo * fRunInfo
virtual void InitializeBranches(TTree *tree)=0
Bool_t Process(Long64_t entry) override
virtual void CreateHistograms()=0
std::string fOutputPrefix
! pre-fix for output files
std::string GetOutputPrefix() const
std::map< std::string, TCutG * > fCuts
void Terminate() override
void Begin(TTree *tree) override
void Init(TTree *tree) override
virtual void EndOfSort()
TAnalysisOptions * fAnalysisOptions
! pointer to analysis options
Definition TPPG.h:132
Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0) override
Definition TPPG.h:149
void Load()
if necessary loads shared object library and sets/initializes all other functions
static int LastRunNumber()
Definition TRunInfo.h:217
static int SubRunNumber()
Definition TRunInfo.h:212
static int LastSubRunNumber()
Definition TRunInfo.h:218
static int RunNumber()
Definition TRunInfo.h:211
static int FirstSubRunNumber()
Definition TRunInfo.h:215
void Print(Option_t *opt="") const override
Definition TRunInfo.cxx:72
static int FirstRunNumber()
Definition TRunInfo.h:214
static TParserLibrary * Get(bool verbose=false)
Definition TSingleton.h:33