REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestDataQualityProcess.cxx
1 /*************************************************************************
2  * This file is part of the REST software framework. *
3  * *
4  * Copyright (C) 2016 GIFNA/TREX (University of Zaragoza) *
5  * For more information see http://gifna.unizar.es/trex *
6  * *
7  * REST is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * REST is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have a copy of the GNU General Public License along with *
18  * REST in $REST_PATH/LICENSE. *
19  * If not, see http://www.gnu.org/licenses/. *
20  * For the list of contributors see $REST_PATH/CREDITS. *
21  *************************************************************************/
22 
48 #include "TRestDataQualityProcess.h"
49 using namespace std;
50 
51 ClassImp(TRestDataQualityProcess);
52 
57 
73  Initialize();
74 
75  LoadConfig(configFilename);
76 }
77 
82 
87  SetName(this->ClassName());
88  SetTitle("Default config");
89 }
90 
103 void TRestDataQualityProcess::LoadConfig(const string& configFilename, const string& name) {
104  if (LoadConfigFromFile(configFilename, name)) LoadDefaultConfig();
105 }
106 
112  // For example, try to initialize a pointer to existing metadata
113  // accessible from TRestRun
114 }
115 
120 void TRestDataQualityProcess::Initialize() { SetSectionName(this->ClassName()); }
121 
126  fEvent = inputEvent;
127 
128  // This process does nothing at the event-by-event level
129 
130  return fEvent;
131 }
132 
138  for (unsigned int n = 0; n < fQualityNumber.size(); n++) {
140  for (int r = 0; r < fRules[n].GetNumberOfRules(); r++) {
141  // We implement metadata-based quality numbers
142  if (fRules[n].GetType(r) == "metadata") {
143  if (EvaluateMetadataRule(fRules[n].GetValue(r), fRules[n].GetRange(r)))
144  EnableBit(fQualityNumber[n], fRules[n].GetBit(r));
145  else
146  DisableBit(fQualityNumber[n], fRules[n].GetBit(r));
147  }
148 
149  if (fRules[n].GetType(r) == "obsAverage") {
150  // TODO implementation
151  }
152 
153  if (fRules[n].GetType(r) == "obsMax") {
154  // TODO implementation
155  }
156  }
157  }
158 
159  if (GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Info) PrintMetadata();
160 
163  // this->GetFullAnalysisTree()->GetEntry(3);
164  // this->GetFullAnalysisTree()->PrintObservables();
165  // cout << "Nobs : " << fRunInfo->GetAnalysisTree()->GetNumberOfObservables() << endl;
166  // cout << "Entries : " << fRunInfo->GetAnalysisTree()->GetEntries() << endl;
167  // cout << "Nobs : " << this->GetFullAnalysisTree()->GetNumberOfObservables() << endl;
168  // cout << "Entries : " << this->GetFullAnalysisTree()->GetEntries() << endl;
169 }
170 
176  size_t position = 0;
177  string QNumberString;
178 
179  // We get each qualityNumber definition
180  while ((QNumberString = GetKEYStructure("qualityNumber", position)) != "NotFound") {
181  string qualityDefinition = GetKEYDefinition("qualityNumber", QNumberString);
182 
183  TString name = GetFieldValue("name", qualityDefinition);
184  fQualityTag.push_back(name);
185 
186  fQualityNumber.push_back(0);
187 
188  TRestDataQualityRules rules;
189 
190  RESTDebug << "Quality number tag : " << name << RESTendl;
191  RESTDebug << "------------------" << RESTendl;
192 
193  size_t position_2 = 0;
194  string ruleDefinition;
195  while ((ruleDefinition = GetKEYDefinition("rule", position_2, QNumberString)) != "") {
196  TVector2 range = StringTo2DVector(GetFieldValue("range", ruleDefinition));
197  TString type = GetFieldValue("type", ruleDefinition);
198  TString value = GetFieldValue("value", ruleDefinition);
199  Int_t bit = StringToInteger(GetFieldValue("bit", ruleDefinition));
200 
201  // TODO check that the bit is not already defined inswide rules.bits
202  // If already defined output a warning << and do not push_back the values into rules
203 
204  // TODO if types = metadata check that the class and the data member exist in fRunInfo
205  // If don't we output a warning << and do not push_back the values into rules
206 
207  // TODO if types = obsAverage check that the observable name exists in the analysisTree
208  // If don't we output a warning << and do not push_back the values into rules
209 
210  // TODO if types = obsMax check that the observable name exists in the analysisTree
211  // If don't we output a warning << and do not push_back the values into rules
212 
213  // If everything in TODO is ok we push the rule!
214  rules.AddRule(type, value, range, bit);
215 
216  RESTDebug << "Rule " << rules.GetNumberOfRules() << RESTendl;
217  RESTDebug << "+++++++++++" << RESTendl;
218  RESTDebug << "Type : " << type << RESTendl;
219  RESTDebug << "Value : " << value << RESTendl;
220  RESTDebug << "Bit : " << bit << RESTendl;
221  RESTDebug << "Range : (" << range.X() << ", " << range.Y() << ")" << RESTendl;
222  }
223  fRules.push_back(rules);
224  }
225 
227 }
228 
234  BeginPrintProcess();
235 
236  for (unsigned int n = 0; n < fQualityNumber.size(); n++) {
237  RESTMetadata << " " << RESTendl;
238  RESTMetadata << "xxxxxxxxxxxxxxxxxxxxxx" << RESTendl;
239  RESTMetadata << " tag : " << fQualityTag[n] << ". Quality number : " << fQualityNumber[n] << RESTendl;
240  RESTMetadata << "xxxxxxxxxxxxxxxxxxxxxx" << RESTendl;
241  RESTMetadata << " " << RESTendl;
242  RESTMetadata << "Rules that have been found in range:" << RESTendl;
243  RESTMetadata << " ----------------- " << RESTendl;
244  Int_t rulesInRange = 0;
245  for (int r = 0; r < fRules[n].GetNumberOfRules(); r++)
246  if (isBitEnabled(fQualityNumber[n], fRules[n].GetBit(r))) {
247  RESTMetadata << fRules[n].GetValue(r) << " is in range (" << fRules[n].GetRange(r).X() << ", "
248  << fRules[n].GetRange(r).Y() << ")" << RESTendl;
249  rulesInRange++;
250  }
251  if (!rulesInRange) RESTMetadata << "No rules found in range!" << RESTendl;
252  RESTMetadata << " " << RESTendl;
253 
254  RESTMetadata << "Rules that have NOT been found in range:" << RESTendl;
255  RESTMetadata << " ----------------- " << RESTendl;
256  Int_t rulesOutRange = 0;
257  for (int r = 0; r < fRules[n].GetNumberOfRules(); r++)
258  if (!isBitEnabled(fQualityNumber[n], fRules[n].GetBit(r))) {
259  RESTMetadata << fRules[n].GetValue(r) << " is NOT in range (" << fRules[n].GetRange(r).X()
260  << ", " << fRules[n].GetRange(r).Y() << ")" << RESTendl;
261  rulesOutRange++;
262  }
263  if (!rulesOutRange) RESTMetadata << "No rules found outside range!" << RESTendl;
264  }
265 
266  EndPrintProcess();
267 }
268 
269 Bool_t TRestDataQualityProcess::EvaluateMetadataRule(TString value, TVector2 range) {
270  vector<string> results = REST_StringHelper::Split((string)value, "::", false, true);
271 
272  if (results.size() == 2) {
273  if (fRunInfo->GetMetadataClass(results[0])) {
274  string val = fRunInfo->GetMetadataClass(results[0])->GetDataMemberValue(results[1]);
275  Double_t dblVal = StringToDouble(val);
276 
277  // If the metadata value is in range we return true
278  if (dblVal >= range.X() && dblVal <= range.Y()) return true;
279  } else if (GetFriend(results[0])) {
280  string val = GetFriend(results[0])->GetDataMemberValue(results[1]);
281  Double_t dblVal = StringToDouble(val);
282 
283  // If the metadata value is in range we return true
284  if (dblVal >= range.X() && dblVal <= range.Y()) return true;
285  } else {
286  RESTError << "TRestDataQualityProcess::EvaluateMetadataRule." << RESTendl;
287  RESTError << "Metadata class " << results[0] << " is not available inside TRestRun" << RESTendl;
288  }
289  } else
290  RESTError << "TRestDataQualityProcess::EvaluateMetadataRule. Wrong number of elements found"
291  << RESTendl;
292  return false;
293 }
A process to define and store quality numbers.
~TRestDataQualityProcess()
Default destructor.
void EndProcess() override
Function to use when all events have been processed.
void InitFromConfigFile() override
Function reading input parameters from the RML TRestDataQualityProcess section.
void PrintMetadata() override
It prints out the process parameters stored in the metadata structure.
TRestDataQualityProcess()
Default constructor.
void InitProcess() override
Function to use in initialization of process members before starting to process the event.
void LoadDefaultConfig()
Function to load the default config in absence of RML input.
void Initialize() override
Function to initialize input/output event members and define the section name and library version.
TRestEvent * ProcessEvent(TRestEvent *inputEvent) override
The main processing event function.
void LoadConfig(const std::string &configFilename, const std::string &name="")
Function to load the configuration from an external configuration file.
A class to define the properties of a rule inside TRestDataQualityRules.
A base class for any REST event.
Definition: TRestEvent.h:38
@ REST_Info
+show most of the information for each steps
Int_t GetChar(std::string hint="Press a KEY to continue ...")
Helps to pause the program, printing a message before pausing.
std::vector< std::string > Split(std::string in, std::string separator, bool allowBlankString=false, bool removeWhiteSpaces=false, int startPos=-1)
Split the input string according to the given separator. Returning a vector of fragments.
Double_t StringToDouble(std::string in)
Gets a double from a string.
Int_t StringToInteger(std::string in)
Gets an integer from a string.
TVector2 StringTo2DVector(std::string in)
Gets a 2D-vector from a string.