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