AliPhysics  63d3444 (63d3444)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AliEmcalCorrectionComponent.h
Go to the documentation of this file.
1 #ifndef ALIEMCALCORRECTIONCOMPONENT_H
2 #define ALIEMCALCORRECTIONCOMPONENT_H
3 
4 #include <map>
5 #include <string>
6 
7 // CINT can't handle the yaml header!
8 #if !(defined(__CINT__) || defined(__MAKECINT__))
9 #include <yaml-cpp/yaml.h>
10 #endif
11 
12 class TH1F;
13 #include <TNamed.h>
14 
17 class AliMCEvent;
18 class AliEMCALRecoUtils;
19 class AliVCaloCells;
20 class AliVTrack;
21 class AliVCluster;
22 class AliEMCALGeometry;
23 class AliVEvent;
24 #include "AliLog.h"
25 
37  public:
39  AliEmcalCorrectionComponent(const char * name);
41 
42  // Virtual functions to be overloaded
43  virtual Bool_t Initialize();
44  virtual void UserCreateOutputObjects();
45  virtual void ExecOnce();
46  virtual Bool_t Run();
47  virtual Bool_t UserNotify();
48 
49  void GetEtaPhiDiff(const AliVTrack *t, const AliVCluster *v, Double_t &phidiff, Double_t &etadiff);
50  void UpdateCells();
52  void GetPass();
53  void FillCellQA(TH1F* h);
54 
57  AliVCaloCells * GetCaloCells() { return fCaloCells; }
58  TList * GetOutputList() { return fOutput; }
59 
62  void SetCaloCells(AliVCaloCells * cells) { fCaloCells = cells; }
63  void SetRecoUtils(AliEMCALRecoUtils *ru) { fRecoUtils = ru; }
64 
65  void SetEvent(AliVEvent * event) { fEvent = event; }
66  void SetMCEvent(AliMCEvent * mcevent) { fMCEvent = mcevent; }
67 
68  void SetEMCALGeometry(AliEMCALGeometry * geometry ) { fGeom = geometry; }
69  void SetCentralityBin(Int_t bin) { fCentBin = bin; }
70  void SetCentrality(Double_t cent) { fCent = cent; }
72  void SetIsESD(Bool_t isESD) {fEsdMode = isESD; }
73 
74 #if !(defined(__CINT__) || defined(__MAKECINT__))
75  void SetUserConfiguration(YAML::Node & node) { fUserConfiguration = node; }
77  void SetDefaultConfiguration(YAML::Node & node) { fDefaultConfiguration = node; }
78 
80  template<typename T> bool GetProperty(std::string propertyName, T & property, bool requiredProperty = true, std::string correctionName = "");
81 
83  template<typename T> static bool GetProperty(std::string propertyName, T & property, const YAML::Node & userConfiguration, const YAML::Node & defaultConfiguration, bool requiredProperty = true, std::string correctionName = "");
84 #endif
85  static bool IsSharedValue(std::string & value);
86 
87  protected:
88 
89 #if !(defined(__CINT__) || defined(__MAKECINT__))
90  template<typename T> static bool GetPropertyFromNodes(const YAML::Node & node, const YAML::Node & sharedParametersNode, std::string propertyName, T & property, const std::string correctionName, const std::string configurationType, int nodesDeep = 0);
91  template<typename T> static bool GetPropertyFromNode(const YAML::Node & node, std::string propertyName, T & property);
92 
93  YAML::Node fUserConfiguration;
95 #endif
96 
101  AliVEvent *fEvent;
103  AliMCEvent *fMCEvent;
111  AliEMCALGeometry *fGeom;
116  AliVCaloCells *fCaloCells;
117  AliEMCALRecoUtils *fRecoUtils;
119 
121 
122  private:
123  AliEmcalCorrectionComponent(const AliEmcalCorrectionComponent &); // Not implemented
125 
127  ClassDef(AliEmcalCorrectionComponent, 1); // EMCal correction component
129 };
130 
131 #if !(defined(__CINT__) || defined(__MAKECINT__))
132 
136 template<typename T>
137 bool AliEmcalCorrectionComponent::GetProperty(std::string propertyName, T & property, bool requiredProperty, std::string correctionName)
138 {
139  // Get proper correction name
140  if (correctionName == "")
141  {
142  correctionName = GetName();
143  }
144  bool result = GetProperty(propertyName, property, fUserConfiguration, fDefaultConfiguration, requiredProperty, correctionName);
145 
146  return result;
147 }
148 
153 template<typename T>
154 bool AliEmcalCorrectionComponent::GetProperty(std::string propertyName, T & property, const YAML::Node & userConfiguration, const YAML::Node & defaultConfiguration, bool requiredProperty, std::string correctionName)
155 {
156  // Remove AliEmcalCorrection if in name
157  std::size_t prefixStringLocation = correctionName.find("AliEmcalCorrection");
158  if (prefixStringLocation != std::string::npos)
159  {
160  // AliEmcalCorrection is 18 characters
161  correctionName.erase(prefixStringLocation, prefixStringLocation + 18);
162  }
163 
164  bool setProperty = false;
165  // IsNull checks is a node is empty. A node is empty if it is created.
166  // IsDefined checks if the node that was requested was not actually created.
167  // In this case, the user configuration node is always created. If it IsNull, then we ignore it.
168  if (userConfiguration.IsNull() != true)
169  {
170  AliDebugClass(2, "Looking for parameter in user configuration");
171  YAML::Node sharedParameters = userConfiguration["sharedParameters"];
172  //std::cout << std::endl << "User Node: " << fUserConfiguration << std::endl;
173  setProperty = AliEmcalCorrectionComponent::GetPropertyFromNodes(userConfiguration, sharedParameters, propertyName, property, correctionName, "user");
174  }
175 
176  if (setProperty != true)
177  {
178  AliDebugClass(2, "Looking for parameter in default configuration");
179  YAML::Node sharedParameters = defaultConfiguration["sharedParameters"];
180  //std::cout << std::endl << "Default Node: " << fDefaultConfiguration << std::endl;
181  setProperty = AliEmcalCorrectionComponent::GetPropertyFromNodes(defaultConfiguration, sharedParameters, propertyName, property, correctionName, "default");
182  }
183 
184  if (setProperty != true && requiredProperty == true)
185  {
186  std::stringstream message;
187  message << "Failed to retrieve property \""
188  << (correctionName != "" ? correctionName + ":" : "")
189  << propertyName << "\" from default config!" << std::endl;
190  AliFatalClass(message.str().c_str());
191  }
192 
193  // Return whether the value was actually set
194  return setProperty;
195 }
196 
201 template<typename T>
202 bool AliEmcalCorrectionComponent::GetPropertyFromNodes(const YAML::Node & node, const YAML::Node & sharedParametersNode, std::string propertyName, T & property, const std::string correctionName, const std::string configurationType, int nodesDeep)
203 {
204  // Used as a buffer for printing complicated messages
205  std::stringstream tempMessage;
206 
207  bool returnValue = false;
208  if (nodesDeep > 2)
209  {
210  // Ensure that we do not go past two levels
211  tempMessage.str("");
212  tempMessage << "Went too many levels of recursion. Bailing out on \"" << correctionName << ":" << propertyName << "\" from " << configurationType << " config at level " << nodesDeep;
213  AliDebugClass(2, TString::Format("%s", tempMessage.str().c_str()));
214  return false;
215  }
216 
217  // Only want to print once to ensure the user is not overwhelmed!
218  if (nodesDeep == 0)
219  {
220  tempMessage.str("");
221  tempMessage << "Retreiving property \""
222  << (correctionName != "" ? correctionName + ":" : "")
223  << propertyName << "\" from " << configurationType << " config at level " << nodesDeep;
224  AliDebugClass(1, TString::Format("%s", tempMessage.str().c_str()));
225  }
226 
227  if (node.IsDefined() == true)
228  {
229  //std::cout << "Node: " << node << std::endl;
230  if (node[propertyName])
231  {
232  bool isShared = false;
233  // Will contain the name of the property in the sharedParameters section that should be retrieved
234  // if it is requested through the user configuration.
235  std::string sharedValueName = "";
236  // Necessary because it fails on vectors and other complex objects that are YAML sequences.
237  if (std::is_arithmetic<T>::value || std::is_same<T, std::string>::value)
238  {
239  // Retrieve value as string to check for shared value
240  sharedValueName = node[propertyName].as<std::string>();
241  // Check for a shared value
242  isShared = AliEmcalCorrectionComponent::IsSharedValue(sharedValueName);
243  }
244 
245  tempMessage.str("");
246  tempMessage << "property \""
247  << (nodesDeep > 0 ? correctionName + ":" : "")
248  << propertyName
249  << "\" using " << ( isShared ? "\"sharedParameters:" + sharedValueName + "\" in " : "" )
250  << "values from the " << configurationType
251  << " configuration at level " << nodesDeep;
252 
253  AliDebugClass(2, TString::Format("Retrieveing %s", tempMessage.str().c_str()));
254 
255  bool retrievalResult = false;
256  if (isShared == true)
257  {
258  retrievalResult = GetPropertyFromNode(sharedParametersNode, sharedValueName, property);
259  }
260  else
261  {
262  retrievalResult = GetPropertyFromNode(node, propertyName, property);
263  }
264 
265  // Inform about the result
266  if (retrievalResult == true)
267  {
268  AliDebugClass(2, TString::Format("Succeeded in retrieveing %s", tempMessage.str().c_str()));
269  returnValue = true;
270  }
271  else
272  {
273  // Only fatal if we have exhausted our last option, the default
274  if (configurationType == "default")
275  {
276  AliFatalClass(TString::Format("Failed to retrieve %s", tempMessage.str().c_str()));
277  }
278  else
279  {
280  AliDebugClass(2, TString::Format("Failed to retrieve %s", tempMessage.str().c_str()));
281  }
282  returnValue = false;
283  }
284  }
285  else
286  {
287  // Go one node deeper using recursion
288  // Must create a new node, since we took the original node by reference
289  YAML::Node deeperNode = node[correctionName];
290  nodesDeep++;
291  AliDebugClass(2, TString::Format("Going a node deeper with \"%s\" to level %i", correctionName.c_str(), nodesDeep));
292  returnValue = GetPropertyFromNodes(deeperNode, sharedParametersNode, propertyName, property, correctionName, configurationType, nodesDeep);
293 
294  // If we didn't find it, next try a substring
295  if (returnValue == false)
296  {
297  // Don't increment nodesDeep, because we are about to go another node deep anyway
298  std::size_t splitLocation = correctionName.find("_");
299  // We only want to look at the split if we are only one node deep and
300  // if the split is actually meaningful
301  if (splitLocation != std::string::npos && nodesDeep == 1)
302  {
303  // Split from start to underscore
304  std::string subCorrectionName = correctionName.substr(0, splitLocation);
305  AliDebugClass(2, TString::Format("Attempting to retrieve property \"%s\" from base correction \"%s\" at level %i", propertyName.c_str(), subCorrectionName.c_str(), nodesDeep));
306  // Retrieve the base correction node
307  // Need to create new node! Otherwise it will assign the correctionName node to subCorrectionName node!
308  YAML::Node subCorrectionNode = node[subCorrectionName];
309  returnValue = GetPropertyFromNodes(subCorrectionNode, sharedParametersNode, propertyName, property, subCorrectionName, configurationType, nodesDeep);
310  }
311  }
312  }
313  }
314  else
315  {
316  tempMessage.str("");
317  tempMessage << "Node is undefined for \""
318  << (correctionName != "" ? correctionName + ":" : "")
319  << propertyName << "\" from " << configurationType << " config at level " << nodesDeep;
320  AliDebugClass(2, TString::Format("%s", tempMessage.str().c_str()));
321 
322  returnValue = false;
323  }
324 
325  return returnValue;
326 }
327 
332 template<typename T>
333 bool AliEmcalCorrectionComponent::GetPropertyFromNode(const YAML::Node & node, std::string propertyName, T & property)
334 {
335  if (node[propertyName])
336  {
337  property = node[propertyName].as<T>();
338  return true;
339  }
340  return false;
341 }
342 
343 #endif /* Hide yaml from CINT */
344 
345 // Below is based on: https://stackoverflow.com/a/582456 , and editted for our purposes.
346 //
347 // Template for creating a new component. Used to register the component.
348 template<typename T> AliEmcalCorrectionComponent * createT() { return new T; }
349 
350 // Factory to create and keep track of new components
352 {
353  public:
355 
356  typedef std::map<std::string, AliEmcalCorrectionComponent*(*)()> map_type;
357 
358  // Creates an instance of an object based on the name if the name is registered in the map.
359  static AliEmcalCorrectionComponent * createInstance(std::string const& s)
360  {
361  map_type::iterator it = getMap()->find(s);
362  if(it == getMap()->end())
363  return 0;
364  // Initializes the function with empty arguments (?)
365  return it->second();
366  }
367 
368  protected:
369  // Creates and access the component map
370  static map_type * getMap() {
371  // We never delete the map (until program termination) because we cannot guarantee
372  // correct destruction order
373  if(!componentMap) { componentMap = new map_type; }
374  return componentMap;
375  }
376 
377  private:
378  // Contains the map to all of the components
380 };
381 
382 // Allows registration of new components into the factory map.
383 template<typename T>
385 {
386  public:
387  RegisterCorrectionComponent(std::string const& s)
388  {
389  getMap()->insert(std::make_pair(s, &createT<T>));
390  }
391 };
392 
393 #endif /* ALIEMCALCORRECTIONCOMPONENT_H */
Int_t fNcentBins
how many centrality bins (this member copied from AliAnalysisTaskEmcal)
AliParticleContainer * GetParticleContainer()
AliEMCALGeometry * fGeom
!geometry object
double Double_t
Definition: External.C:58
RegisterCorrectionComponent(std::string const &s)
void SetClusterContainer(AliClusterContainer *cont)
void SetEMCALGeometry(AliEMCALGeometry *geometry)
void SetRecoUtils(AliEMCALRecoUtils *ru)
void SetMCEvent(AliMCEvent *mcevent)
AliEmcalCorrectionComponent * createT()
Int_t fCentBin
!event centrality bin
AliVCaloCells * fCaloCells
! pointer to calo cells
Container for particles within the EMCAL framework.
UShort_t T(UShort_t m, UShort_t t)
Definition: RingBits.C:60
void SetUserConfiguration(YAML::Node &node)
Make copy to ensure that the nodes do not point to each other (?)
AliEMCALRecoUtils * fRecoUtils
pointer to reco utils
AliClusterContainer * fClusCont
pointer to the cluster container
void SetParticleContainer(AliParticleContainer *cont)
static bool GetPropertyFromNode(const YAML::Node &node, std::string propertyName, T &property)
int Int_t
Definition: External.C:63
Base class for correction components in the EMCal correction framework.
static bool GetPropertyFromNodes(const YAML::Node &node, const YAML::Node &sharedParametersNode, std::string propertyName, T &property, const std::string correctionName, const std::string configurationType, int nodesDeep=0)
Int_t fMinMCLabel
minimum MC label value for the tracks/clusters being considered MC particles
std::map< std::string, AliEmcalCorrectionComponent *(*)()> map_type
TString fBasePath
base folder path to get root files
Double_t fMaxBinPt
max pt in histograms
AliVEvent * fEvent
!pointer to event
TList * fOutput
! list of output histograms
AliEmcalCorrectionComponent & operator=(const AliEmcalCorrectionComponent &)
static bool IsSharedValue(std::string &value)
Bool_t fCreateHisto
flag to make some basic histograms
Bool_t fGetPassFromFileName
get fFilepass from file name
void SetDefaultConfiguration(YAML::Node &node)
Double_t fMinBinPt
min pt in histograms
void SetCaloCells(AliVCaloCells *cells)
void GetEtaPhiDiff(const AliVTrack *t, const AliVCluster *v, Double_t &phidiff, Double_t &etadiff)
static AliEmcalCorrectionComponent * createInstance(std::string const &s)
AliClusterContainer * GetClusterContainer()
bool Bool_t
Definition: External.C:53
AliParticleContainer * fPartCont
pointer to the track/particle container
Container structure for EMCAL clusters.
TString fFilepass
input data pass number
bool GetProperty(std::string propertyName, T &property, bool requiredProperty=true, std::string correctionName="")
Retrieve property.
Bool_t fIsEmbedded
trigger, embedded signal