source: branches/removing_cpp11_features/mfbase/MFEventSelector.cc

Last change on this file was 9857, checked in by tbretz, 14 years ago
If the number of selected events in MFEventSelector is negative, select all events.
File size: 6.4 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz, 01/2002 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2003
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MFEventSelector
28//
29// This is a filter to make a selection of events from a file. At the
30// present implementation you can only use a random selection.
31//
32// This filter can be used for a random split...
33//
34// If you want to fill only 50% of your events into a histogram please use:
35// MFEventSelector sel;
36// sel.SetSelectionRatio(0.5);
37// MFillH filler(...);
38// filler.SetFilter(&sel);
39// tlist.AddToList(&sel);
40// tlist.AddToList(&filler);
41//
42// To get around 2000 events from all events use (Remark: This will only
43// work if the parlist has an entry called MTaskList which has a task
44// MRead inheriting from MRead):
45// MFEventSelector sel;
46// sel.SetNumSelectEvts(2000);
47// MFillH filler(...);
48// filler.SetFilter(&sel);
49// tlist.AddToList(&sel);
50// tlist.AddToList(&filler);
51//
52// If you don't have MRead available you have to set the number of
53// total events manually, using sel.SetNumTotalEvts(10732);
54//
55// The random number is generated using gRandom->Uniform(). You may
56// control this procedure using the global object gRandom.
57//
58// Because of the random numbers this works best for huge samples...
59//
60// Don't try to use this filter for the reading task: This won't work!
61//
62// Remark: You can also use the filter together with MContinue
63//
64//
65// FIXME: Merge MFEventSelector and MFEventSelector2
66//
67/////////////////////////////////////////////////////////////////////////////
68#include "MFEventSelector.h"
69
70#include <TRandom.h>
71
72#include "MParList.h"
73#include "MTaskList.h"
74#include "MRead.h"
75
76#include "MLog.h"
77#include "MLogManip.h"
78
79ClassImp(MFEventSelector);
80
81using namespace std;
82
83static const TString gsDefName = "MFEventSelector";
84static const TString gsDefTitle = "Filter to select events";
85
86// --------------------------------------------------------------------------
87//
88// Constructor. For the text describing the filter rule please see
89// the class description above.
90//
91MFEventSelector::MFEventSelector(const char *name, const char *title)
92: fNumTotalEvts(-1), fNumSelectEvts(-1), fSelRatio(-1), fNumSelectedEvts(0)
93{
94 fName = name ? name : gsDefName.Data();
95 fTitle = title ? title : gsDefTitle.Data();
96}
97
98// --------------------------------------------------------------------------
99//
100// Set a probability with which events are selected. Eg, f=0.5
101// will select roughly half of all events.
102//
103void MFEventSelector::SetSelectionRatio(Float_t f)
104{
105 if (f < 0)
106 {
107 *fLog << warn << "MFEventSelector::SetSelectionRatio: WARNING - Probability less than 0... set to 0." << endl;
108 f = 0;
109 }
110
111 if (f > 1)
112 {
113 *fLog << warn << "MFEventSelector::SetSelectionRatio: WARNING - Probability greater than 1... set to 1." << endl;
114 f = 1;
115 }
116 fSelRatio = f;
117}
118
119// --------------------------------------------------------------------------
120//
121// PreProcess all filters.
122//
123Int_t MFEventSelector::PreProcess(MParList *plist)
124{
125 fNumSelectedEvts = 0;
126
127 // In the case a ratio was set by the user we are done.
128 if (fSelRatio>0)
129 return kTRUE;
130
131 // If the number of total events wasn't set try to get it
132 if (fNumTotalEvts<0)
133 {
134 MTaskList *tlist = (MTaskList*)plist->FindObject("MTaskList");
135 if (!tlist)
136 {
137 *fLog << err << "Can't determin total number of events... no MTaskList." << endl;
138 return kFALSE;
139 }
140
141 MRead *read = (MRead*)tlist->FindObject("MRead");
142 if (!read)
143 {
144 *fLog << err << "Can't determin total number of events from 'MRead'." << endl;
145 return kFALSE;
146 }
147 fNumTotalEvts = read->GetEntries();
148
149 SetBit(kNumTotalFromFile);
150 }
151
152 if (fNumSelectEvts<0)
153 {
154 fSelRatio = 1;
155 *fLog << inf << "No selection will be done - all events selected." << endl;
156 return kTRUE;
157 }
158
159 // Calculate selection probability
160 fSelRatio = (Double_t)fNumSelectEvts/fNumTotalEvts;
161
162 *fLog << inf << "Selection probability = " << fNumSelectEvts << "/";
163 *fLog << fNumTotalEvts << " = " << Form("%.2f", fSelRatio) << endl;
164
165 return kTRUE;
166}
167
168// --------------------------------------------------------------------------
169//
170// Process all filters, FIXME: Make it fit the requested number of events
171// exactly like it is done in MFEventSelector2. This can be done by merging
172// both classes!
173//
174Int_t MFEventSelector::Process()
175{
176 fResult = gRandom->Uniform() < fSelRatio;
177
178 if (fResult)
179 fNumSelectedEvts++;
180
181 return kTRUE;
182}
183
184// --------------------------------------------------------------------------
185//
186// Postprocess all filters.
187//
188Int_t MFEventSelector::PostProcess()
189{
190 //---------------------------------
191 if (GetNumExecutions() != 0)
192 {
193 const Double_t sel = (Double_t)fNumSelectedEvts/GetNumExecutions();
194 const UInt_t non = GetNumExecutions()-fNumSelectedEvts;
195
196 *fLog << inf << dec << setfill(' ') << endl;
197 *fLog << GetDescriptor() << " execution statistics:" << endl;
198
199 *fLog << " " << setw(7) << non << " (" << setw(3);
200 *fLog << (int)(100*(1-sel)) << "%) Events not selected" << endl;
201
202 *fLog << " " << setw(7) << fNumSelectedEvts << " (";
203 *fLog << (int)(100*sel) << "%) Events selected!" << endl;
204 *fLog << endl;
205 }
206
207 //---------------------------------
208 if (TestBit(kNumTotalFromFile))
209 {
210 fNumTotalEvts = -1;
211 if (fNumSelectEvts>0)
212 fSelRatio = -1;
213 }
214
215 return kTRUE;
216}
Note: See TracBrowser for help on using the repository browser.