REST-for-Physics  v2.3
Rare Event Searches ToolKit for Physics
TRestCut.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 
56 
57 #include "TRestCut.h"
58 
59 #include "TRestDataBase.h"
60 #include "TRestManager.h"
61 #include "TRestProcessRunner.h"
62 #include "TRestStringOutput.h"
63 
64 using namespace std;
65 ClassImp(TRestCut);
66 
67 TRestCut::TRestCut() { Initialize(); }
68 
69 void TRestCut::Initialize() { fCuts.clear(); }
70 
72  auto ele = GetElement("cut");
73  while (ele != nullptr) {
74  string name = GetParameter("name", ele, "");
75  if (name.empty() || name == "Not defined") {
76  RESTError << "< cut does not contain a name!" << RESTendl;
77  exit(1);
78  }
79 
80  string cutStr = GetParameter("value", ele, "");
81  string variable = GetParameter("variable", ele, "");
82  string condition = GetParameter("condition", ele, "");
83 
84  if (!cutStr.empty()) {
85  cutStr = Replace(cutStr, " AND ", " && ");
86  cutStr = Replace(cutStr, " OR ", " || ");
87  fCutStrings.push_back(cutStr);
88  AddCut(TCut(name.c_str(), cutStr.c_str()));
89  } else if (!variable.empty() && !condition.empty()) {
90  fParamCut.push_back(std::make_pair(variable, condition));
91  string cutVar = variable + condition;
92  AddCut(TCut(name.c_str(), cutVar.c_str()));
93  } else {
94  RESTError << "TRestCut does not contain a valid parameter/condition or cut string!" << RESTendl;
95  RESTError << "<cut name='cc1' value='XX>10 AND XX<90'/>" << RESTendl;
96  RESTError << "<cut name='cc3' variable='sAna_ThresholdIntegral' condition='>0'" << RESTendl;
97  exit(1);
98  }
99 
100  ele = GetNextElement(ele);
101  }
102 }
103 
104 TRestCut& TRestCut::operator=(TRestCut& cut) {
105  SetName(cut.GetName());
106  SetTitle(cut.GetTitle());
107  fCuts = cut.GetCuts();
108  fCutStrings = cut.GetCutStrings();
109  fParamCut = cut.GetParamCut();
110  return *this;
111 }
112 
113 void TRestCut::AddCut(const TCut& cut) {
114  if ((string)cut.GetName() == "") {
115  RESTWarning << "TRestCut::AddCut: cannot add cut without name!" << RESTendl;
116  return;
117  }
118  if ((string)cut.GetTitle() == "") {
119  RESTWarning << "TRestCut::AddCut: cannot add empty cut!" << RESTendl;
120  return;
121  }
122  for (const auto& c : fCuts) {
123  if ((string)c.GetName() == (string)cut.GetName()) {
124  RESTWarning << "TRestCut::AddCut: cut with name \"" << c.GetName() << "\" already added!"
125  << RESTendl;
126  return;
127  }
128  }
129  fCuts.push_back(cut);
130 }
131 
132 TCut TRestCut::GetCut(string name) {
133  for (auto c : fCuts) {
134  if ((string)c.GetName() == name) {
135  return c;
136  }
137  }
138  return {};
139 }
140 
141 void TRestCut::AddCut(TRestCut* cut) {
142  if (cut == nullptr) {
143  RESTWarning << "Cut to be added is nullptr, skipping" << RESTendl;
144  return;
145  }
146  for (const auto& c : cut->GetCuts()) {
147  AddCut(c);
148  }
149 
150  const auto paramCut = cut->GetParamCut();
151  fParamCut.insert(fParamCut.end(), paramCut.begin(), paramCut.end());
152 
153  const auto cutStrings = cut->GetCutStrings();
154  fCutStrings.insert(fCutStrings.end(), cutStrings.begin(), cutStrings.end());
155 }
156 
159  RESTMetadata << " " << RESTendl;
160  RESTMetadata << "Cuts added: " << RESTendl;
161  for (const auto& cut : fCuts) {
162  RESTMetadata << cut.GetName() << " " << cut.GetTitle() << RESTendl;
163  }
164  RESTMetadata << "+++" << RESTendl;
165 }
166 
167 Int_t TRestCut::Write(const char* name, Int_t option, Int_t bufsize) {
168  // write cuts to file.
169 
170  for (auto c : fCuts) {
171  c.Write();
172  }
173 
174  return TRestMetadata::Write(name, option, bufsize);
175 }
A class to help on cuts definitions. To be used with TRestAnalysisTree.
Definition: TRestCut.h:31
void PrintMetadata() override
Implemented it in the derived metadata class to print out specific metadata information.
Definition: TRestCut.cxx:157
Int_t Write(const char *name, Int_t option, Int_t bufsize) override
overwriting the write() method with fStore considered
Definition: TRestCut.cxx:167
void Initialize() override
Making default settings.
Definition: TRestCut.cxx:69
void InitFromConfigFile() override
To make settings from rml file. This method must be implemented in the derived class.
Definition: TRestCut.cxx:71
virtual void PrintMetadata()
Implemented it in the derived metadata class to print out specific metadata information.
virtual Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0)
overwriting the write() method with fStore considered
std::string Replace(std::string in, std::string thisString, std::string byThisString, size_t fromPosition=0, Int_t N=0)
Replace any occurences of thisSring by byThisString inside string in.