PandaRoot
PndFilteredPrimaryGenerator.h
Go to the documentation of this file.
1 //****************************************************************************
2 //* This file is part of PandaRoot. *
3 //* *
4 //* PandaRoot is distributed under the terms of the *
5 //* GNU General Public License (GPL) version 3, *
6 //* copied verbatim in the file "LICENSE". *
7 //* *
8 //* Copyright (C) 2006 - 2024 FAIR GmbH and copyright holders of PandaRoot *
9 //* The copyright holders are listed in the file "COPYRIGHTHOLDERS". *
10 //* The authors are listed in the file "AUTHORS". *
11 //****************************************************************************
12 
55 #ifndef PndFilteredPrimaryGenerator_H
56 #define PndFilteredPrimaryGenerator_H
57 
58 #include "FairPrimaryGenerator.h"
59 #include "FairEvtFilterParams.h"
60 #include "FairRunSim.h"
61 #include "FairRootFileSink.h"
62 
63 #include "PndSmpFilt.h"
64 #include "PndSmpCand.h"
65 
66 #include "TFile.h" // for TFile
67 #include "TLorentzVector.h"
68 #include "TString.h"
69 #include "Rtypes.h" // for Double_t, Bool_t, Int_t, etc
70 
71 //#include <iosfwd> // for ostream
72 #include <iostream> // for operator<<, basic_ostream, etc
73 #include <vector>
74 #include <unordered_set>
75 #include <map>
76 
77 class FairGenericStack;
78 class TDatabasePDG;
79 
80 typedef vector<PndSmpFilt> PndSmpFilterSet;
81 typedef std::vector<PndSmpCand> PndSmpCandList;
82 typedef std::vector<TString> StrVec;
83 
84 class PndFilteredPrimaryGenerator : public FairPrimaryGenerator {
85 
86  public:
89 
91  PndFilteredPrimaryGenerator(TString filterset);
92 
95 
97  virtual Bool_t Init();
98 
104  void AddFilter(TString filterStr);
105 
116  virtual Bool_t GenerateEvent(FairGenericStack *pStack);
117 
119  void SetFilterMaxTries(Int_t maxTries = 99999)
120  {
121  if (maxTries > 0) {
122  fEvtFilterStat.fFilterMaxTries = maxTries;
123  std::cout << "PndFilteredPrimaryGenerator: maxTries is now set to " << fEvtFilterStat.fFilterMaxTries << "\n";
124  } else {
125  std::cout << "\n\n\n -WARNING from PndFilteredPrimaryGenerator: maxTries must be a positive number! Check your SetFilterMaxTries call!\n\n\n";
126  }
127  }
128 
131 
135 
137  void SetEventPrintFrequency(int freq) { fEventPrintFreq = freq; }
138 
148 
150  void WriteEvtFilterStatsToRootFile(TFile *outputFile = nullptr)
151  {
152  std::cout << "\n\nGenerated Events = " << GetNumberOfGeneratedEvents() << "\n";
153  if (0 < GetNumberOfFilterFailedEvents()) {
154  std::cout << "WARNING: Number of events where the event filter FAILED " << GetNumberOfFilterFailedEvents() << "\n\n\n";
155  std::cout << "Random events were accepted to avoid infinite loops. \n";
156  std::cout << "Try increasing the max. number of tries or change your filter (maybe the generators do not produce such events as you want).\n\n";
157  }
158  if (outputFile == nullptr)
159  outputFile = ((FairRootFileSink*)FairRunSim::Instance()->GetSink())->GetRootFile();
160  //outputFile = FairRunSim::Instance()->GetOutputFile();
161  outputFile->cd();
162  outputFile->mkdir("FairEvtFilter");
163  outputFile->cd("FairEvtFilter");
164  fEvtFilterStat.Write();
165  outputFile->cd();
166  }
167 
171  void SetVerbose(Int_t verbose = 12)
172  {
173  if (verbose >= 0) {
174  fVerbose = verbose;
175  std::cout << "PndFilteredPrimaryGenerator: fVerbose is now set to " << verbose << "\n";
176  } else {
177  std::cout << "\n\n\n -WARNING from PndFilteredPrimaryGenerator: verbose must be a positive number! Check your SetVerbose call!\n\n\n";
178  }
179  }
180 
181  protected:
183  PndSmpCandList CombineList(int pdg, PndSmpCandList *l0, PndSmpCandList *l1, PndSmpCandList *l2 = nullptr, PndSmpCandList *l3 = nullptr, PndSmpCandList *l4 = nullptr);
184 
186  void PrintSmpCandList(PndSmpCandList l, TString name = "");
187 
189  StrVec SplitString(TString s, TString delim = " ");
190 
192  bool CheckKinematic(const PndSmpFilt &f, const TLorentzVector &p4);
193 
195  int AntiPdgCode(int pdg);
196 
198  void GetRangeDouble(TString s, double &a, double &b, TString delim = ",", bool forceset = false);
199 
201  void GetRangeInt(TString s, int &a, int &b, TString delim = "..");
202 
208  std::vector<PndSmpFilterSet> fFilterSets;
209 
212 
214  Int_t fVerbose;
215 
218 
221 
223  TDatabasePDG *fdbPdg;
224 
226  std::vector<TString> fPartNames;
227 
229  std::vector<int> fNamePdg;
230 
232  std::vector<int> fCombFsPdg;
233 
235  std::unordered_set<int> fSetFsPdg;
236 
238  std::map<TString, int> fNameCodeMap;
239 
241  std::map<int, TString> fCodeNameMap;
242 
243  private:
246 
247  ClassDef(PndFilteredPrimaryGenerator, 1);
248 };
249 
250 #endif
std::vector< PndSmpCand > PndSmpCandList
std::vector< TString > StrVec
StrVec SplitString(TString s, TString delim=" ")
Splits a TString to substrings.
std::unordered_set< int > fSetFsPdg
set to identify particles from MC truth list which can be combined
Int_t GetNumberOfFilterMaxTries()
returns the maximum number of times that this object should try to find an event which suits all even...
Simple container for filter definition (criteria) for PndFilteredPrimaryGenerator.
Definition: PndSmpFilt.h:31
PndFilteredPrimaryGenerator()
Default constructor.
virtual Bool_t GenerateEvent(FairGenericStack *pStack)
Calls event generators and the event filters.
void SetEventPrintFrequency(int freq)
Sets the frequency (accepted events) for printout (verbose>0) of accepted and generated events...
void WriteEvtFilterStatsToRootFile(TFile *outputFile=nullptr)
Writes all relevant event filter information to the output root file.
std::map< TString, int > fNameCodeMap
mapes names to (pdg) codes
bool CheckKinematic(const PndSmpFilt &f, const TLorentzVector &p4)
Checks whether P4 kinematics match the criteria of a PndSmpFilt.
Int_t fEventNrFiltered
Event number (Set by the filtered primary generator.
std::map< int, TString > fCodeNameMap
mapes (pdg) codes to names
TDatabasePDG * fdbPdg
Shortcut to TDatabasePDG.
std::vector< TString > fPartNames
particle names for particles to count (with and w/o charged specification, also simply tracks and neu...
Primary generator with added event filtering capabilities.
Int_t GetNumberOfGeneratedEvents()
returns the total (accepted + rejected) number of events generated by the event generators. If no event filters are used this number is equal to the number of simulated events.
vector< PndSmpFilt > PndSmpFilterSet
PndSmpCandList CombineList(int pdg, PndSmpCandList *l0, PndSmpCandList *l1, PndSmpCandList *l2=nullptr, PndSmpCandList *l3=nullptr, PndSmpCandList *l4=nullptr)
Combines upt to five particle lists of PndSmpCand with overlap and double counting prevention...
virtual ~PndFilteredPrimaryGenerator()
Destructor.
Int_t GetNumberOfFilterFailedEvents()
Returns the number of cases in which no matching event was found within the set max. tries.
FairEvtFilterParams fEvtFilterStat
Contains the statistics of the event filtering process.
void AddFilter(TString filterStr)
Registers a filter as a string to be parsed Each filter set consist of one filter definition or a num...
std::vector< PndSmpFilterSet > fFilterSets
Contains the filter-sets. Each filter set consist of one filter definition or a number of filters con...
float f
Definition: P4_F32vec4.h:32
Int_t fEventPrintFreq
Print frequency for filtered events.
void PrintSmpCandList(PndSmpCandList l, TString name="")
Prints a candidate lits.
void SetVerbose(Int_t verbose=12)
Set the level of commenting output.
void GetRangeInt(TString s, int &a, int &b, TString delim="..")
Turns a string of the form <int><delim><int> (e.g. "3..8") to a range a,b.
virtual Bool_t Init()
Initialize the event generator(s) and the event (veto) filter(s).
Int_t fVerbose
Level of commenting output, 0 means no output, higher gives more output.
std::vector< int > fNamePdg
particle codes for particles to count (with and w/o charged specification, also simply tracks and neu...
void SetFilterMaxTries(Int_t maxTries=99999)
Define the maximum number of times that this object should try to find an event which suits all event...
int AntiPdgCode(int pdg)
Gets anti-pdg code, if exists. If not returns the code itself (particle is its anti-particle) ...
std::vector< int > fCombFsPdg
particle codes for the lists used for combinatorics; !!! the codes are not selected from MC truth...
void GetRangeDouble(TString s, double &a, double &b, TString delim=",", bool forceset=false)
Turns a string of the form <float><delim><float> (e.g. "124.2,178.3") to a range a,b.