FairRoot/PandaRoot
FairFilteredPrimaryGenerator.cxx
Go to the documentation of this file.
2 
3 #include "FairGenerator.h" // for FairGenerator
4 #include "FairGenericStack.h" // for FairGenericStack
5 #include "FairLogger.h" // for FairLogger, MESSAGE_ORIGIN
6 #include "FairMCEventHeader.h" // for FairMCEventHeader
7 
8 #include "TDatabasePDG.h" // for TDatabasePDG
9 #include "TF1.h" // for TF1
10 #include "TIterator.h" // for TIterator
11 #include "TMCProcess.h" // for TMCProcess::kPPrimary
12 #include "TMath.h" // for Sqrt
13 #include "TObject.h" // for TObject
14 #include "TParticlePDG.h" // for TParticlePDG
15 #include "TRandom.h" // for TRandom, gRandom
16 #include "TString.h" // for TString
17 #include "RhoFactory.h"
18 
19 #include <stddef.h> // for NULL
20 #include <iostream> // for operator<<, basic_ostream, etc
21 
22 using std::cout;
23 using std::cerr;
24 using std::endl;
25 
26 
27 // ----- Default constructor -------------------------------------------
29 : FairPrimaryGenerator(),
30 
31  fVetoFilterList(new TObjArray()),
32  fVetoFilterIter(fVetoFilterList->MakeIterator()),
33  fFilterList(new TObjArray()),
34  fFilterIter(fFilterList->MakeIterator()),
35  fEvtFilterStat(FairEvtFilterParams()),
36  fVerbose(3),
37  fEventVetoFilterActive(kFALSE),
38  fEventFilterActive(kFALSE),
39  filterAcceptEvent(),fLogicalFilterOperation(),fFilterNegation(),
40  fEventNrFiltered(0),
41  fEventPrintFreq(100)
42 {
43 }
44 // -------------------------------------------------------------------------
45 
46 
47 
48 // ----- Constructor with title and name -------------------------------
50 : FairPrimaryGenerator(name,title),
51 
52  fVetoFilterList(new TObjArray()),
53  fVetoFilterIter(fVetoFilterList->MakeIterator()),
54  fFilterList(new TObjArray()),
55  fFilterIter(fFilterList->MakeIterator()),
56  fEvtFilterStat(FairEvtFilterParams()),
57  fVerbose(3),
58  fEventVetoFilterActive(kFALSE),
59  fEventFilterActive(kFALSE),
60  filterAcceptEvent(),fLogicalFilterOperation(),fFilterNegation(),
61  fEventNrFiltered(0),
62  fEventPrintFreq(100)
63 {
64 }
65 // -------------------------------------------------------------------------
66 
68 {
70  // Initialize list of veto filters
71  for(Int_t k=0; k<fVetoFilterList->GetEntries(); ++k ) {
72  FairEvtFilter* vetoFilter= (FairEvtFilter*) fVetoFilterList->At(k);
73  if(vetoFilter) { vetoFilter->Init(); }
74  }
75  // Initialize list of event filters
76  for(Int_t k=0; k<fFilterList->GetEntries(); ++k ) {
77  FairEvtFilter* filter= (FairEvtFilter*) fFilterList->At(k);
78  if(filter) { filter->Init(); }
79  }
80  return kTRUE;
81 }
82 
83 
84 // ----- Destructor ----------------------------------------------------
86 {
87  fVetoFilterList->Delete();
88  delete fVetoFilterList;
89  delete fVetoFilterIter;
90 
91  fFilterList->Delete();
92  delete fFilterList;
93  delete fFilterIter;
94 
95  //cout<<" Leave Destructor of FairFilteredPrimaryGenerator"<<endl;
96 }
97 // -------------------------------------------------------------------------
98 
99 
100 
101 // ----- Public method GenerateEvent -----------------------------------
103 {
105 
106  Int_t iTry=0; // number of attempts to find the next event that suits your filter
107  Bool_t acceptEvent = kFALSE; // is kTRUE if the event is finally accepted
108 
109  if(kTRUE == fEventFilterActive){
110  // sanity check settings for logical combinations of event filters
111  if((int)fFilterNegation.size()!=fFilterList->GetEntriesFast()){
112  std::cout << "\n\n\n -WARNING from FairFilteredPrimaryGenerator: size of the negation vector has to be equal to the number of added filters. Check your FilterNegation call!\n\n\n";
113  return kFALSE;
114  }
115  if((int)fLogicalFilterOperation.size()!=fFilterList->GetEntriesFast()-1){
116  std::cout << "\n\n\n -WARNING from FairFilteredPrimaryGenerator: size of the operation vector has to be equal to the number of added filters minus one. Check your LogicalFilterOperation call!\n\n\n";
117  return kFALSE;
118  }
119  }
120 
121  do{
122  ++iTry; // count how often we run the generators before we accept an event
123  ++fEvtFilterStat.fGeneratedEvents; // total number of generated events
124  Bool_t vetoed = kFALSE; // is kTRUE if event matches any of the veto filters (we will skip such events)
125 
126 
127 
128  pStack->Reset(); // Clean the stack
129  FairPrimaryGenerator::GenerateEvent(pStack); // fill the stack
130  fEventNr = fEventNrFiltered; // Fix event numbering in FairPrimaryGenerator (otherwise fRun will stop too early...)
131 
132 
133  if(kTRUE == fEventVetoFilterActive){// skip veto filter checking if no veto filters are set
134  // check veto filters. skip event if the event satisfies any of the veto filters' conditions
135  // Call the FilterAccept methods for all veto filters
136  fVetoFilterIter->Reset();
137  TObject* vetoFilterObject = 0;
138  FairEvtFilter* vetoFilter = 0;
139  while( (vetoFilterObject = fVetoFilterIter->Next()) ) {
140  vetoFilter = dynamic_cast<FairEvtFilter*> (vetoFilterObject);
141  if ( ! vetoFilter ) { // this should never happen
142  cout << " \n\n\n -FATAL ERROR from FairFilteredPrimaryGenerator: Veto filter cast did not work!\n\n\n" << endl;
143  return kFALSE;
144  }
145  if( !( vetoFilter->FilterActive() ) ){
146  cout << " \n\n\n -WARNING from FairFilteredPrimaryGenerator: There are no filter settings for filter " << vetoFilter->GetName() << " : " << vetoFilter->GetTitle() << ". Execution is halted!\n\n\n" << endl;
147  return kFALSE;
148  }
149  Bool_t setParticlesOk = vetoFilter->SetListOfParticles(pStack->GetListOfParticles());//committing the list of particles to be checked
150  if( ! setParticlesOk ) {
151  cout << " \n\n\n -WARNING from FairFilteredPrimaryGenerator: Particles were not pushed correctly to veto filter.\n\n\n" << endl;
152  return kFALSE;
153  }
154  if ( vetoFilter->EventMatches(fEvtFilterStat.fGeneratedEvents) ) {
155  // if event matches veto filter, skip event
156  if( 3 < fVerbose ){
157  cout << "\n Event is NOT accepted because it matches a veto filter \n";
158  }
159  vetoed = kTRUE;
160  break; // no need to check other veto filters as event will be skipped anyway
161  }
162  } // end while
163  } // if veto filtering
164 
165  if ( vetoed ) { continue; }; // skip event if it matches at least one veto filter
166 
167  if ( kFALSE == fEventFilterActive ){ // skip event filtering (i.e. accept all events) if filtering is not requested
168  acceptEvent = kTRUE;
169  if( 3 < fVerbose ){
170  cout << "\n Event is accepted because event filtering is not requested.\n\n";
171  }
172  } else { // do the event filtering (i.e. decide whether to accept or not accept the generated events) if filtering is requested
173  Int_t iCheckFilter=0;
174  // Call the FilterAccept methods for all registered (non-veto) filters
175  fFilterIter->Reset();
176  TObject* eventFilterObject = 0;
177  FairEvtFilter* eventFilter = 0;
178  filterAcceptEvent.clear();
179  while( (eventFilterObject = fFilterIter->Next()) ) {
180  eventFilter = dynamic_cast<FairEvtFilter*> (eventFilterObject);
181  if ( ! eventFilter ) {
182  return kFALSE; } // this should never happen
183  if( !( eventFilter->FilterActive() ) ){
184  cout << " \n\n\n -WARNING from FairFilteredPrimaryGenerator: There are no filter settings for filter " << eventFilter->GetName() << " : " << eventFilter->GetTitle() << ". Execution is halted!\n\n\n" << endl;
185  return kFALSE;
186  }
187  Bool_t setParticlesOk = eventFilter->SetListOfParticles(pStack->GetListOfParticles());//committing the list of particles to be checked
188  if( ! setParticlesOk ) {
189  return kFALSE;
190  }
192  // check whether the simulated event matches the individual event filter
193  iCheckFilter++;
194  }
195 
196  //----------FilterNegation
197  // negate output from the event filters which should be negated
198  for(UInt_t iFil=0; iFil<fFilterNegation.size(); iFil++){
199  if(kTRUE == fFilterNegation[iFil]){
200  filterAcceptEvent[iFil]=(!filterAcceptEvent[iFil]);
201  }
202  }
203 
204 
205  //----------LogicalFilterOperation
206  // combine (possibly negated) outputs from individual event filters using the logical connections stored in fLogicalFilterOperation
207  // in order to check whether the event is accepted
208  acceptEvent=filterAcceptEvent[0];
209  for(UInt_t iOper=0; iOper<fLogicalFilterOperation.size();iOper++){
211  acceptEvent=acceptEvent && filterAcceptEvent[iOper+1];
212  }else{
214  acceptEvent=acceptEvent || filterAcceptEvent[iOper+1];
215  }else{
216  std::cout << "\n\n\n -WARNING from FairFilteredPrimaryGenerator: the entries of the operation vector have to be kAnd or kOr. Check your LogicalFilterOperation call!\n\n\n";
217  return kFALSE;
218  }
219  }
220  }
221  if( !acceptEvent && 3 < fVerbose ){
222  cout << "\n Event is NOT accepted after combining the event filters.\n";
223  }
224  } // if (non-veto) event filtering
225  }while(! acceptEvent && iTry < fEvtFilterStat.fFilterMaxTries);
226 
227 
228  // Set the event number ALWAYS when filtering
230  fEvent->SetEventID(fEventNrFiltered);
231 
232  if ( !acceptEvent && ( fEventFilterActive||fEventVetoFilterActive ) ){
234  cout << "\n -E FairFilteredPrimaryGenerator: No event was found within " << iTry << " tries which satisfies your event filter.\n ";
235  cout << "I accept a random event as evtNr " << fEventNrFiltered << " to avoid infinite loops. \n";
236  cout << "Try increasing the max. number of tries or change your filter\n\n";
237  if (fVerbose > 3 ){
238  if(fFilterList->GetEntriesFast() > 0){cout << "\n Event is accepted after combining the appointed filters.\n";}
239  cout << iTry << " events simulated until I found a good one.\n";
240  cout << fEvtFilterStat.fGeneratedEvents << " events generated for finding " << fEventNr << " accepted events.\n";
241  cout << fEvtFilterStat.fFailedFilterEvents << " unsuccessful attempts in total to find an event that suits your filters\n\n";
242  }
243  }
244 
245  if (0 < fVerbose && (fEventNrFiltered%fEventPrintFreq)==0) cout << fEventNrFiltered << " of " << fEvtFilterStat.fGeneratedEvents << " generated events accepted.\n";
246 
247  return kTRUE;
248 }
249 // -------------------------------------------------------------------------
250 
251 
252 
int fVerbose
Definition: poormantracks.C:24
virtual Bool_t Init()
Initialize the event generator(s) and the event (veto) filter(s).
Bool_t fEventFilterActive
returns kTRUE if any non-veto event filter is registerd.
std::vector< UInt_t > fLogicalFilterOperation
vector containing the logical operations with which the outputs of the non-veto event filters should ...
TObjArray * fVetoFilterList
List of registered veto filters.
static void Reset()
Definition: RhoFactory.cxx:28
Int_t fEventNrFiltered
Event number (Set by the filtered primary generator.
TObjArray * fFilterList
List of registered filters.
Bool_t Init()
Definition: FairEvtFilter.h:64
Primary generator with added event filtering capabilities.
std::vector< Bool_t > fFilterNegation
vector determining whether the output of a non-veto event filter should be negated or not...
virtual Bool_t EventMatches(Int_t evtNr)=0
FairFilteredPrimaryGenerator()
Default constructor.
FairEvtFilterParams fEvtFilterStat
Contains the statistics of the event filtering process.
TIterator * fVetoFilterIter
Iterator over veto filter list.
static RhoFactory * Instance()
Definition: RhoFactory.cxx:34
Int_t fVerbose
Level of commenting output, 0 means no output, higher gives more output.
TString name
Int_t fEventPrintFreq
Print frequency for filtered events.
Bool_t SetListOfParticles(TClonesArray *ParticleList)
Definition: FairEvtFilter.h:57
TIterator * fFilterIter
Iterator over filter list.
fRun Init()
Definition: NHitsPerEvent.C:20
virtual Bool_t GenerateEvent(FairGenericStack *pStack)
Calls event generators and the event filters.
ClassImp(PndAnaContFact)
Bool_t fEventVetoFilterActive
returns kTRUE if any event veto filter is registered.
virtual Bool_t FilterActive()=0
std::vector< Bool_t > filterAcceptEvent
Vector containing the results of the EventMatches methods for every registered non-veto event filter ...
virtual ~FairFilteredPrimaryGenerator()
Destructor.