source: trunk/MagicSoft/Mars/mfbase/MFEventSelector.cc@ 9242

Last change on this file since 9242 was 7804, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 6.2 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 // Calculate selection probability
153 fSelRatio = (Double_t)fNumSelectEvts/fNumTotalEvts;
154
155 *fLog << inf << "MFEventSelector: Selection probability = " << fNumSelectEvts;
156 *fLog << "/" << fNumTotalEvts << " = " << Form("%.2f", fSelRatio) << endl;
157
158 return kTRUE;
159}
160
161// --------------------------------------------------------------------------
162//
163// Process all filters, FIXME: Make it fit the requested number of events
164// exactly like it is done in MFEventSelector2. This can be done by merging
165// both classes!
166//
167Int_t MFEventSelector::Process()
168{
169 fResult = gRandom->Uniform() < fSelRatio;
170
171 if (fResult)
172 fNumSelectedEvts++;
173
174 return kTRUE;
175}
176
177// --------------------------------------------------------------------------
178//
179// Postprocess all filters.
180//
181Int_t MFEventSelector::PostProcess()
182{
183 //---------------------------------
184 if (GetNumExecutions() != 0)
185 {
186 const Double_t sel = (Double_t)fNumSelectedEvts/GetNumExecutions();
187 const UInt_t non = GetNumExecutions()-fNumSelectedEvts;
188
189 *fLog << inf << dec << setfill(' ') << endl;
190 *fLog << GetDescriptor() << " execution statistics:" << endl;
191
192 *fLog << " " << setw(7) << non << " (" << setw(3);
193 *fLog << (int)(100*(1-sel)) << "%) Events not selected" << endl;
194
195 *fLog << " " << setw(7) << fNumSelectedEvts << " (";
196 *fLog << (int)(100*sel) << "%) Events selected!" << endl;
197 *fLog << endl;
198 }
199
200 //---------------------------------
201 if (TestBit(kNumTotalFromFile))
202 {
203 fNumTotalEvts = -1;
204 if (fNumSelectEvts>0)
205 fSelRatio = -1;
206 }
207
208 return kTRUE;
209}
Note: See TracBrowser for help on using the repository browser.