AliPhysics  008a5da (008a5da)
 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 AliVEvent;
23 #include "AliLog.h"
24 #include "AliEMCALGeometry.h"
25 
44  public:
46  AliEmcalCorrectionComponent(const char * name);
48 
49  // Virtual functions to be overloaded
50  virtual Bool_t Initialize();
51  virtual void UserCreateOutputObjects();
52  virtual void ExecOnce();
53  virtual Bool_t Run();
54  virtual Bool_t UserNotify();
55  virtual Bool_t CheckIfRunChanged();
56 
57  void GetEtaPhiDiff(const AliVTrack *t, const AliVCluster *v, Double_t &phidiff, Double_t &etadiff);
58  void UpdateCells();
59  void GetPass();
60  void FillCellQA(TH1F* h);
62 
65  AliVCaloCells * GetCaloCells() { return fCaloCells; }
66  TList * GetOutputList() { return fOutput; }
67 
70  void SetCaloCells(AliVCaloCells * cells) { fCaloCells = cells; }
71  void SetRecoUtils(AliEMCALRecoUtils *ru) { fRecoUtils = ru; }
72 
73  void SetEvent(AliVEvent * event) { fEvent = event; }
74  void SetMCEvent(AliMCEvent * mcevent) { fMCEvent = mcevent; }
75 
76  void SetEMCALGeometry(AliEMCALGeometry * geometry ) { fGeom = geometry; }
77  void SetCentralityBin(Int_t bin) { fCentBin = bin; }
78  void SetCentrality(Double_t cent) { fCent = cent; }
80  void SetIsESD(Bool_t isESD) {fEsdMode = isESD; }
81 
82 #if !(defined(__CINT__) || defined(__MAKECINT__))
83  void SetUserConfiguration(YAML::Node & node) { fUserConfiguration = node; }
85  void SetDefaultConfiguration(YAML::Node & node) { fDefaultConfiguration = node; }
86 
88  template<typename T> bool GetProperty(std::string propertyName, T & property, bool requiredProperty = true, std::string correctionName = "");
89 
91  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 = "");
92 #endif
93  static bool IsSharedValue(std::string & value);
94 
95  protected:
96 
97 #if !(defined(__CINT__) || defined(__MAKECINT__))
98  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);
99  template<typename T> static bool GetPropertyFromNode(const YAML::Node & node, std::string propertyName, T & property);
100 
101  template<typename T> static typename std::enable_if<!std::is_arithmetic<T>::value && !std::is_same<T, std::string>::value && !std::is_same<T, bool>::value>::type PrintRetrievedPropertyValue(T & property, std::stringstream & tempMessage);
102  template<typename T> static typename std::enable_if<std::is_arithmetic<T>::value || std::is_same<T, std::string>::value || std::is_same<T, bool>::value>::type PrintRetrievedPropertyValue(T & property, std::stringstream & tempMessage);
103 
104  YAML::Node fUserConfiguration;
106 #endif
107 
112  AliVEvent *fEvent;
114  AliMCEvent *fMCEvent;
122  AliEMCALGeometry *fGeom;
127  AliVCaloCells *fCaloCells;
128  AliEMCALRecoUtils *fRecoUtils;
130 
132 
133  private:
134  AliEmcalCorrectionComponent(const AliEmcalCorrectionComponent &); // Not implemented
136 
138  ClassDef(AliEmcalCorrectionComponent, 1); // EMCal correction component
140 };
141 
142 #if !(defined(__CINT__) || defined(__MAKECINT__))
143 
155 template<typename T>
156 bool AliEmcalCorrectionComponent::GetProperty(std::string propertyName, T & property, bool requiredProperty, std::string correctionName)
157 {
158  // Get proper correction name
159  if (correctionName == "")
160  {
161  correctionName = GetName();
162  }
163  bool result = GetProperty(propertyName, property, fUserConfiguration, fDefaultConfiguration, requiredProperty, correctionName);
164 
165  return result;
166 }
167 
182 template<typename T>
183 bool AliEmcalCorrectionComponent::GetProperty(std::string propertyName, T & property, const YAML::Node & userConfiguration, const YAML::Node & defaultConfiguration, bool requiredProperty, std::string correctionName)
184 {
185  // Remove AliEmcalCorrection if in name
186  std::size_t prefixStringLocation = correctionName.find("AliEmcalCorrection");
187  if (prefixStringLocation != std::string::npos)
188  {
189  // AliEmcalCorrection is 18 characters
190  correctionName.erase(prefixStringLocation, prefixStringLocation + 18);
191  }
192 
193  bool setProperty = false;
194  // IsNull checks is a node is empty. A node is empty if it is created.
195  // IsDefined checks if the node that was requested was not actually created.
196  // In this case, the user configuration node is always created. If it IsNull, then we ignore it.
197  if (userConfiguration.IsNull() != true)
198  {
199  AliDebugClass(2, "Looking for parameter in user configuration");
200  YAML::Node sharedParameters = userConfiguration["sharedParameters"];
201  //std::cout << std::endl << "User Node: " << fUserConfiguration << std::endl;
202  setProperty = AliEmcalCorrectionComponent::GetPropertyFromNodes(userConfiguration, sharedParameters, propertyName, property, correctionName, "user");
203  }
204 
205  if (setProperty != true)
206  {
207  AliDebugClass(2, "Looking for parameter in default configuration");
208  YAML::Node sharedParameters = defaultConfiguration["sharedParameters"];
209  //std::cout << std::endl << "Default Node: " << fDefaultConfiguration << std::endl;
210  setProperty = AliEmcalCorrectionComponent::GetPropertyFromNodes(defaultConfiguration, sharedParameters, propertyName, property, correctionName, "default");
211  }
212 
213  if (setProperty != true && requiredProperty == true)
214  {
215  std::stringstream message;
216  message << "Failed to retrieve property \""
217  << (correctionName != "" ? correctionName + ":" : "")
218  << propertyName << "\" from default config!" << std::endl;
219  AliFatalClass(message.str().c_str());
220  }
221 
222  // Return whether the value was actually set
223  return setProperty;
224 }
225 
251 template<typename T>
252 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)
253 {
254  // Used as a buffer for printing complicated messages
255  std::stringstream tempMessage;
256 
257  bool returnValue = false;
258  if (nodesDeep > 2)
259  {
260  // Ensure that we do not go past two levels
261  tempMessage.str("");
262  tempMessage << "Went too many levels of recursion. Bailing out on \"" << correctionName << ":" << propertyName << "\" from " << configurationType << " config at level " << nodesDeep;
263  AliDebugClass(2, TString::Format("%s", tempMessage.str().c_str()));
264  return false;
265  }
266 
267  // Only want to print once to ensure the user is not overwhelmed!
268  if (nodesDeep == 0)
269  {
270  tempMessage.str("");
271  tempMessage << "Retreiving property \""
272  << (correctionName != "" ? correctionName + ":" : "")
273  << propertyName << "\" from " << configurationType << " config at level " << nodesDeep;
274  AliDebugClass(1, TString::Format("%s", tempMessage.str().c_str()));
275  }
276 
277  if (node.IsDefined() == true)
278  {
279  //std::cout << "Node: " << node << std::endl;
280  if (node[propertyName])
281  {
282  bool isShared = false;
283  // Will contain the name of the property in the sharedParameters section that should be retrieved
284  // if it is requested through the user configuration.
285  std::string sharedValueName = "";
286  // Necessary because it fails on vectors and other complex objects that are YAML sequences.
287  if (std::is_arithmetic<T>::value || std::is_same<T, std::string>::value || std::is_same<T, bool>::value)
288  {
289  // Retrieve value as string to check for shared value
290  sharedValueName = node[propertyName].as<std::string>();
291  // Check for a shared value
292  isShared = AliEmcalCorrectionComponent::IsSharedValue(sharedValueName);
293  }
294 
295  tempMessage.str("");
296  tempMessage << "property \""
297  << (nodesDeep > 0 ? correctionName + ":" : "")
298  << propertyName
299  << "\" using " << ( isShared ? "\"sharedParameters:" + sharedValueName + "\" in " : "" )
300  << "values from the " << configurationType
301  << " configuration at level " << nodesDeep;
302 
303  AliDebugClass(2, TString::Format("Retrieveing %s", tempMessage.str().c_str()));
304 
305  bool retrievalResult = false;
306  if (isShared == true)
307  {
308  retrievalResult = GetPropertyFromNode(sharedParametersNode, sharedValueName, property);
309  }
310  else
311  {
312  retrievalResult = GetPropertyFromNode(node, propertyName, property);
313  }
314 
315  // Inform about the result
316  if (retrievalResult == true)
317  {
318  // Add the retrieved value to the message (only if trivially printable)
319  PrintRetrievedPropertyValue(property, tempMessage);
320  AliDebugClassStream(2) << "Succeeded in retrieveing " << tempMessage.str() << std::endl;
321  returnValue = true;
322  }
323  else
324  {
325  // Only fatal if we have exhausted our last option, the default
326  if (configurationType == "default")
327  {
328  AliFatalClass(TString::Format("Failed to retrieve %s", tempMessage.str().c_str()));
329  }
330  else
331  {
332  AliDebugClass(2, TString::Format("Failed to retrieve %s", tempMessage.str().c_str()));
333  }
334  returnValue = false;
335  }
336  }
337  else
338  {
339  // Go one node deeper using recursion
340  // Must create a new node, since we took the original node by reference
341  YAML::Node deeperNode = node[correctionName];
342  nodesDeep++;
343  AliDebugClass(2, TString::Format("Going a node deeper with \"%s\" to level %i", correctionName.c_str(), nodesDeep));
344  returnValue = GetPropertyFromNodes(deeperNode, sharedParametersNode, propertyName, property, correctionName, configurationType, nodesDeep);
345 
346  // If we didn't find it, next try a substring
347  if (returnValue == false)
348  {
349  // Don't increment nodesDeep, because we are about to go another node deep anyway
350  std::size_t splitLocation = correctionName.find("_");
351  // We only want to look at the split if we are only one node deep and
352  // if the split is actually meaningful
353  if (splitLocation != std::string::npos && nodesDeep == 1)
354  {
355  // Split from start to underscore
356  std::string subCorrectionName = correctionName.substr(0, splitLocation);
357  AliDebugClass(2, TString::Format("Attempting to retrieve property \"%s\" from base correction \"%s\" at level %i", propertyName.c_str(), subCorrectionName.c_str(), nodesDeep));
358  // Retrieve the base correction node
359  // Need to create new node! Otherwise it will assign the correctionName node to subCorrectionName node!
360  YAML::Node subCorrectionNode = node[subCorrectionName];
361  returnValue = GetPropertyFromNodes(subCorrectionNode, sharedParametersNode, propertyName, property, subCorrectionName, configurationType, nodesDeep);
362  }
363  }
364  }
365  }
366  else
367  {
368  tempMessage.str("");
369  tempMessage << "Node is undefined for \""
370  << (correctionName != "" ? correctionName + ":" : "")
371  << propertyName << "\" from " << configurationType << " config at level " << nodesDeep;
372  AliDebugClass(2, TString::Format("%s", tempMessage.str().c_str()));
373 
374  returnValue = false;
375  }
376 
377  return returnValue;
378 }
379 
390 template<typename T>
391 typename std::enable_if<std::is_arithmetic<T>::value || std::is_same<T, std::string>::value || std::is_same<T, bool>::value>::type
392 AliEmcalCorrectionComponent::PrintRetrievedPropertyValue(T & property, std::stringstream & tempMessage)
393 {
394  //AliDebugClassStream(2) << " with value " << property;
395  tempMessage << " with value \"" << property << "\"";
396 }
397 
406 template<typename T>
407 typename std::enable_if<!std::is_arithmetic<T>::value && !std::is_same<T, std::string>::value && !std::is_same<T, bool>::value>::type
408 AliEmcalCorrectionComponent::PrintRetrievedPropertyValue(T & property, std::stringstream & tempMessage)
409 {
410  // Cannot easily print these types, so just note that is the case!
411  tempMessage << " with a value that cannot be trivially printed";
412 }
413 
421 template<typename T>
422 bool AliEmcalCorrectionComponent::GetPropertyFromNode(const YAML::Node & node, std::string propertyName, T & property)
423 {
424  if (node[propertyName])
425  {
426  property = node[propertyName].as<T>();
427  return true;
428  }
429  return false;
430 }
431 
432 #endif /* Hide yaml from CINT */
433 
452 template<typename T> AliEmcalCorrectionComponent * createT() { return new T; }
454 
455 // Factory to create and keep track of new components
457 {
458  public:
460 
461  typedef std::map<std::string, AliEmcalCorrectionComponent*(*)()> map_type;
462 
464  static AliEmcalCorrectionComponent * createInstance(std::string const& s)
465  {
466  map_type::iterator it = getMap()->find(s);
467  if(it == getMap()->end())
468  return 0;
469  // Initializes the function with empty arguments (?)
470  return it->second();
471  }
472 
473  protected:
475  static map_type * getMap() {
476  // We never delete the map (until program termination) because we cannot guarantee
477  // correct destruction order
478  if(!componentMap) { componentMap = new map_type; }
479  return componentMap;
480  }
481 
482  private:
485 };
486 
499 template<typename T>
501 {
502  public:
504  RegisterCorrectionComponent(std::string const& s)
505  {
506  getMap()->insert(std::make_pair(s, &createT<T>));
507  }
508 };
509 
510 #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
static std::enable_if<!std::is_arithmetic< T >::value &&!std::is_same< T, std::string >::value &&!std::is_same< T, bool >::value >::type PrintRetrievedPropertyValue(T &property, std::stringstream &tempMessage)
RegisterCorrectionComponent(std::string const &s)
Registers the name of the component to map to a function that can create the component.
void SetClusterContainer(AliClusterContainer *cont)
void SetEMCALGeometry(AliEMCALGeometry *geometry)
void SetRecoUtils(AliEMCALRecoUtils *ru)
void SetMCEvent(AliMCEvent *mcevent)
Registers EMCal correction components in the factory map.
AliEmcalCorrectionComponent * createT()
Template function for creating a new component. Used to register the component.
Int_t fCentBin
! Event centrality bin
AliVCaloCells * fCaloCells
! Pointer to CaloCells
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 RecoUtils.
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)
YAML::Node fDefaultConfiguration
! Default YAML configuration
Int_t fMinMCLabel
Minimum MC label value for the tracks/clusters being considered MC particles.
static map_type * componentMap
Contains the map to all of the components.
static map_type * getMap()
Creates and access the component map.
std::map< std::string, AliEmcalCorrectionComponent *(*)()> map_type
Double_t fVertex[3]
! Event vertex
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.
Double_t fCent
! Event centrality
void SetDefaultConfiguration(YAML::Node &node)
Double_t fMinBinPt
Min pt in histograms.
Factory for correction components in the EMCal correction framework.
YAML::Node fUserConfiguration
! User YAML configuration
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)
Creates an instance of an object based on the name if the name is registered in the map...
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.