Heavy ion Analysis Libriares
Loading...
Searching...
No Matches
CutMonitor.cxx
1/*
2 * CutMonitor.cpp
3 *
4 * Created on: 10-07-2013
5 * Author: Daniel Wielanek
6 */
7
8#include "CutMonitor.h"
9
10#include "Cout.h"
11#include "Cut.h"
12#include "Package.h"
13#include "Parameter.h"
14#include "StdString.h"
15
16#include <TClass.h>
17#include <TH1.h>
18
19
20namespace Hal {
21 void CutMonitor::AddForcedCut(Cut* cut, Int_t no) { fCut[no] = cut; }
22
23 void CutMonitor::SetAxis(Int_t bins, Double_t min, Double_t max, Int_t opt) {
24 if (opt < fAxisNo) {
25 fAxisMin[opt] = min;
26 fAxisMax[opt] = max;
27 fAxisBins[opt] = bins;
28 } else {
29 Cout::PrintInfo(Form("You cant configure more than %i axis in %s", fAxisNo + 1, this->ClassName()), EInfo::kLowWarning);
30 }
31 }
32
34
35 Cut* CutMonitor::GetCut(Int_t i) const { return fCut[i]; }
36
37 Int_t CutMonitor::GetCutParameter(Int_t i) const {
38 if (i >= fAxisNo) return -1;
39 if (i < 0) return -1;
40 return fOptionAxis[i];
41 }
42
44 TObject(),
45 fAxisNo(size),
46 fCuts(0),
47 fCollectionID(-1),
48 fAxisBins(NULL),
49 fOptionAxis(NULL),
50 fHistoPassed(NULL),
51 fHistoFailed(NULL),
52 fAxisMin(NULL),
53 fAxisMax(NULL),
54 fCut(NULL),
55 fCutNames(NULL) {
56 fInit = kFALSE;
57 fExUpdate = kFALSE;
58 fCuts = 0;
59 if (fAxisNo > 0) {
60 fUpdateRatio = ECutUpdate::kNo;
61 fCut = new Cut*[fAxisNo];
62 fCutNames = new TString[fAxisNo];
63 fAxisBins = new Int_t[fAxisNo];
64 fOptionAxis = new Int_t[fAxisNo];
65 fAxisMin = new Double_t[fAxisNo];
66 fAxisMax = new Double_t[fAxisNo];
67 for (int i = 0; i < fAxisNo; i++) {
68 fCut[i] = NULL;
69 fAxisMin[i] = 0;
70 fAxisMax[i] = 1.0;
71 fOptionAxis[i] = 0;
72 fAxisBins[i] = 100;
73 }
74 }
75 }
76
78 TObject(other), fAxisNo(other.fAxisNo), fHistoPassed(NULL), fHistoFailed(NULL) {
79 fInit = kFALSE;
80 fExUpdate = other.fExUpdate;
81 fCuts = other.fCuts;
83 fCut = new Cut*[fAxisNo];
84 fCutNames = new TString[fAxisNo];
85 fAxisBins = new Int_t[fAxisNo];
86 fOptionAxis = new Int_t[fAxisNo];
87 fAxisMin = new Double_t[fAxisNo];
88 fAxisMax = new Double_t[fAxisNo];
90 for (int i = 0; i < fAxisNo; i++) {
91 fCut[i] = NULL; // this will be set by CutContainer
92 fCutNames[i] = other.fCutNames[i];
93 fAxisMin[i] = other.fAxisMin[i];
94 fAxisMax[i] = other.fAxisMax[i];
95 fAxisBins[i] = other.fAxisBins[i];
96 fOptionAxis[i] = other.fOptionAxis[i];
97 }
98 }
99
100 Bool_t CutMonitor::Init(Int_t /*task_id*/) {
101 if (fInit) {
102#ifdef HAL_DEBUG
103 Cout::PrintInfo(Form("%s is initialized ", this->ClassName()), EInfo::kDebugInfo);
104#endif
105 return kFALSE;
106 }
107 for (int i = 0; i < fAxisNo; i++) {
108 if (fCut[i] == NULL) {
109 Cout::PrintInfo(Form("Missed cut %i %s in %s", i, fCutNames[i].Data(), this->ClassName()), EInfo::kError);
110 return kFALSE;
111 }
112 }
113 for (int i = 0; i < fAxisNo; i++) {
114 for (int j = i + 1; j < fAxisNo; j++) {
115 if (fCut[i]->GetUpdateRatio() != fCut[j]->GetUpdateRatio()) {
116 Cout::PrintInfo("Not compatible update ratios this might cause wrong "
117 "results in CutMonitor",
118 EInfo::kLowWarning);
119 }
120 }
121 }
122 TH1::AddDirectory(kFALSE);
124 TH1::AddDirectory(kTRUE);
125 fInit = kTRUE;
126 return kTRUE;
127 }
128
129 void CutMonitor::SetAxisList(std::initializer_list<Double_t> axis, Char_t opt) {
130 std::vector<Double_t> vec = Hal::Std::GetVector(axis);
131 if (vec.size() != 3) return;
132 switch (opt) {
133 case 'x': {
134 SetXaxis(vec[0], vec[1], vec[2]);
135 } break;
136 case 'y': {
137 SetYaxis(vec[0], vec[1], vec[2]);
138 } break;
139 case 'z': {
140 SetZaxis(vec[0], vec[1], vec[2]);
141 } break;
142 default: break;
143 }
144 }
145
147 fHistoPassed->Reset();
148 fHistoFailed->Reset();
149 }
150
151 ECutUpdate CutMonitor::GetUpdateRatio() const { return fUpdateRatio; }
152
153 void CutMonitor::Update(Bool_t /*passed*/, TObject* /*obj*/) {}
154
155 void CutMonitor::TrueUpdate(Bool_t /*passed*/) {}
156
158
160
161 void CutMonitor::AddCut(TString cut, Int_t parameter_no) {
162 if (Hal::Std::FindParam(cut, "Cloned")) {
163 Cout::PrintInfo("You can't add Cloned Cuts to CutMonitor", EInfo::kLowWarning);
164 return;
165 }
166 TClass* classdata = NULL;
167 if (cut.Contains("(")) { // complex cut?
168 TString name(cut(0, cut.First('(')));
169 classdata = TClass::GetClass(name);
170 } else {
171 classdata = TClass::GetClass(cut);
172 }
173 ECutUpdate newUpd = ECutUpdate::kNo;
174
175 if (classdata == nullptr) {
176 Cout::PrintInfo(Form("Cannot find class %s", cut.Data()), EInfo::kLowWarning);
177 } else {
178 if (classdata->InheritsFrom("Hal::EventCut")) newUpd = ECutUpdate::kEvent;
179 if (classdata->InheritsFrom("Hal::TrackCut")) newUpd = ECutUpdate::kTrack;
180 if (classdata->InheritsFrom("Hal::TwoTrackCut")) newUpd = ECutUpdate::kTwoTrack;
181 }
182
183 if (fCuts == 0) {
184 fUpdateRatio = newUpd;
185 } else {
186 if (fUpdateRatio < newUpd) { fUpdateRatio = newUpd; }
187 }
188 if (fCuts < fAxisNo) {
189 fCutNames[fCuts] = cut;
190 fOptionAxis[fCuts] = parameter_no;
191 fCuts++;
192 if (fUpdateRatio < newUpd) { fUpdateRatio = newUpd; }
193 }
194 }
195
196 void CutMonitor::SetXaxis(Int_t bins, Double_t min, Double_t max) {
197 if (fAxisNo > 0) {
198 SetAxis(bins, min, max, 0);
199 } else {
200 Cout::PrintInfo("CutMonitor::X axis not found", EInfo::kLowWarning);
201 }
202 }
203
204 void CutMonitor::SetYaxis(Int_t bins, Double_t min, Double_t max) {
205 if (fAxisNo > 1) {
206 SetAxis(bins, min, max, 1);
207 } else {
208 Cout::PrintInfo("CutMonitor::Y axis not found", EInfo::kLowWarning);
209 }
210 }
211
212 void CutMonitor::SetZaxis(Int_t bins, Double_t min, Double_t max) {
213 if (fAxisNo > 2) {
214 SetAxis(bins, min, max, 2);
215 } else {
216 Cout::PrintInfo("CutMonitor::Z axis not found", EInfo::kLowWarning);
217 }
218 }
219
221 for (int i = 0; i < fAxisNo; i++) {
222 SetAxis(source->fAxisBins[i], source->fAxisMin[i], source->fAxisMax[i], i);
223 }
224 }
225
226 CutMonitor* CutMonitor::MakeCopy() const { return new CutMonitor(*this); }
227
228 CutMonitor::~CutMonitor() {
229 delete[] fCut;
230 if (fHistoFailed) delete fHistoFailed;
231 if (fHistoPassed) delete fHistoPassed;
232 delete[] fAxisBins;
233 delete[] fOptionAxis;
234 delete[] fAxisMin;
235 delete[] fAxisMax;
236 delete[] fCutNames;
237 }
238
240 if (this != &other) {
241 fCuts = other.fCuts;
243 if (fAxisNo != other.fAxisNo) { Cout::PrintInfo("Copy incompatible cut monitors", EInfo::kLowWarning); }
244 for (int i = 0; i < fAxisNo; i++) {
245 fAxisBins[i] = other.fAxisBins[i];
246 fOptionAxis[i] = other.fOptionAxis[i];
247 fAxisMin[i] = other.fAxisMin[i];
248 fAxisMax[i] = other.fAxisMax[i];
249 fCut[i] = other.fCut[i];
250 fCutNames[i] = other.fCutNames[i];
251 }
252 if (fHistoFailed) delete fHistoFailed;
253 fHistoFailed = nullptr;
254 if (fHistoPassed) delete fHistoPassed;
255 fHistoPassed = nullptr;
256 if (other.fInit) {
257 fHistoPassed = (TH1*) other.fHistoPassed->Clone();
258 fHistoFailed = (TH1*) other.fHistoFailed->Clone();
259 }
260 fInit = other.fInit;
261 fExUpdate = other.fExUpdate;
263 }
264 return *this;
265 }
266
268 Package* pack = new Package(this);
269 pack->AddObject(new ParameterInt("Cuts", fCuts));
270 pack->AddObject(new ParameterInt("CollectionID", fCollectionID));
271 pack->AddObject(new ParameterInt("UpdateRatio", (Int_t) fUpdateRatio));
272 pack->AddObject(new ParameterInt("ExclusiveUpdate", (Int_t) fExUpdate));
273
274 if (!ObjMonitor()) {
275 TH1* hP = (TH1*) fHistoPassed->Clone();
276 TH1* hF = (TH1*) fHistoFailed->Clone();
277 pack->AddObject(hP);
278 pack->AddObject(hF);
279 if (fAxisNo >= 1) {
280 std::vector<std::pair<TString, Double_t>> labels = fCut[0]->GetBinLabels(fOptionAxis[0]);
281 if (labels.size() > 0) {
282 for (auto iLabel : labels) {
283 Int_t bin = hP->GetXaxis()->FindBin(iLabel.second);
284 if (bin != 0) {
285 hP->GetXaxis()->SetBinLabel(bin, iLabel.first);
286 hF->GetXaxis()->SetBinLabel(bin, iLabel.first);
287 }
288 }
289 }
290 pack->AddObject(new ParameterString("AxisX", fCut[0]->GetUnit(fOptionAxis[0])));
291 pack->AddObject(new ParameterString("CutXName", fCut[0]->CutName()));
292 pack->AddObject(new ParameterInt("CutXAxis", fOptionAxis[0]));
293 pack->AddObject(new ParameterDouble("CutXMin", fCut[0]->GetMin(fOptionAxis[0])));
294 pack->AddObject(new ParameterDouble("CutXMax", fCut[0]->GetMax(fOptionAxis[0])));
295 pack->AddObject(new ParameterInt("CutXCollection", fCut[0]->GetCollectionID()));
296 }
297 if (fAxisNo >= 2) {
298 std::vector<std::pair<TString, Double_t>> labels = fCut[1]->GetBinLabels(fOptionAxis[1]);
299 if (labels.size() > 0) {
300 for (auto iLabel : labels) {
301 Int_t bin = hP->GetYaxis()->FindBin(iLabel.second);
302 if (bin != 0) {
303 hP->GetYaxis()->SetBinLabel(bin, iLabel.first);
304 hF->GetYaxis()->SetBinLabel(bin, iLabel.first);
305 }
306 }
307 }
308 pack->AddObject(new ParameterString("AxisY", fCut[1]->GetUnit(fOptionAxis[1])));
309 pack->AddObject(new ParameterString("CutYName", fCut[1]->CutName()));
310 pack->AddObject(new ParameterInt("CutYAxis", fOptionAxis[1]));
311 pack->AddObject(new ParameterDouble("CutYMin", fCut[1]->GetMin(fOptionAxis[1])));
312 pack->AddObject(new ParameterDouble("CutYMax", fCut[1]->GetMax(fOptionAxis[1])));
313 pack->AddObject(new ParameterInt("CutYCollection", fCut[1]->GetCollectionID()));
314 }
315 if (fAxisNo >= 3) {
316 std::vector<std::pair<TString, Double_t>> labels = fCut[2]->GetBinLabels(fOptionAxis[2]);
317 if (labels.size() > 0) {
318 for (auto iLabel : labels) {
319 Int_t bin = hP->GetZaxis()->FindBin(iLabel.second);
320 if (bin != 0) {
321 hP->GetZaxis()->SetBinLabel(bin, iLabel.first);
322 hF->GetZaxis()->SetBinLabel(bin, iLabel.first);
323 }
324 }
325 }
326 pack->AddObject(new ParameterString("AxisZ", fCut[2]->GetUnit(fOptionAxis[2])));
327 pack->AddObject(new ParameterString("CutZName", fCut[2]->CutName()));
328 pack->AddObject(new ParameterInt("CutZAxis", fOptionAxis[2]));
329 pack->AddObject(new ParameterDouble("CutZMin", fCut[2]->GetMin(fOptionAxis[2])));
330 pack->AddObject(new ParameterDouble("CutZMax", fCut[2]->GetMax(fOptionAxis[2])));
331 pack->AddObject(new ParameterInt("CutZCollection", fCut[2]->GetCollectionID()));
332 }
333 } else {
334 pack->AddObject(fHistoPassed->Clone());
335 pack->AddObject(fHistoFailed->Clone());
336 }
337 pack->SetComment(this->ClassName());
338
339 return pack;
340 }
341
342 Bool_t CutMonitor::AreSimilar(CutMonitor* other) const {
343 if (this->fAxisNo != other->fAxisNo) return kFALSE;
344 for (int i = 0; i < fAxisNo; i++) {
345 if (!this->fCutNames[i].EqualTo(other->fCutNames[i])) { return kFALSE; }
346 }
347 return kTRUE;
348 }
349} // namespace Hal
static void PrintInfo(TString text, Hal::EInfo status)
Definition Cout.cxx:370
CutMonitor & operator=(const CutMonitor &other)
void CopySettings(const CutMonitor *original)
CutMonitor(Int_t size=0)
void EnableExclusiveUpdate()
Int_t GetCutParameter(Int_t i) const
void AddCut(TString cut, Int_t parameter_no=0)
TString * fCutNames
Definition CutMonitor.h:79
Double_t * fAxisMax
Definition CutMonitor.h:63
virtual void Update(Bool_t passed, TObject *obj)
Int_t fCollectionID
Definition CutMonitor.h:39
void AddForcedCut(Cut *cut, Int_t no)
void SetYaxis(Int_t bins, Double_t min, Double_t max)
Bool_t AreSimilar(CutMonitor *other) const
Int_t GetCollectionID() const
Definition CutMonitor.h:188
virtual void TrueUpdate(Bool_t passed)
virtual Bool_t Init(Int_t task_id)
void SetZaxis(Int_t bins, Double_t min, Double_t max)
Int_t * fAxisBins
Definition CutMonitor.h:43
const Int_t fAxisNo
Definition CutMonitor.h:31
virtual Bool_t ObjMonitor() const
Definition CutMonitor.h:221
void SetAxis(Int_t bins, Double_t min, Double_t max, Int_t opt)
void SetAxisList(std::initializer_list< Double_t > axis, Char_t opt)
Int_t * fOptionAxis
Definition CutMonitor.h:47
ECutUpdate GetUpdateRatio() const
virtual void CreateHistograms()
virtual CutMonitor * MakeCopy() const
Double_t * fAxisMin
Definition CutMonitor.h:59
void SetCollectionID(Int_t i)
Cut * GetCut(Int_t i) const
void SetXaxis(Int_t bins, Double_t min, Double_t max)
ECutUpdate fUpdateRatio
Definition CutMonitor.h:83
virtual Package * Report() const
Definition Cut.h:40
virtual std::vector< std::pair< TString, Double_t > > GetBinLabels(Int_t par=0) const
Definition Cut.cxx:246
void SetComment(TString name)
Definition Package.cxx:65
void AddObject(TObject *object)
Definition Package.cxx:209