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