AliPhysics  d497547 (d497547)
 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 
15 class AliMCEvent;
16 class AliEMCALRecoUtils;
17 class AliVCaloCells;
18 class AliVTrack;
19 class AliVCluster;
20 class AliVEvent;
21 #include <AliLog.h>
22 #include "AliEmcalContainerUtils.h"
23 #include "AliParticleContainer.h"
24 #include "AliMCParticleContainer.h"
25 #include "AliTrackContainer.h"
26 #include "AliClusterContainer.h"
27 #include "AliEMCALGeometry.h"
29 
48  public:
50  AliEmcalCorrectionComponent(const char * name);
52 
53  // Virtual functions to be overloaded
54  virtual Bool_t Initialize();
55  virtual void UserCreateOutputObjects();
56  virtual void ExecOnce();
57  virtual Bool_t Run();
58  virtual Bool_t UserNotify();
59  virtual Bool_t CheckIfRunChanged();
60 
61  void GetEtaPhiDiff(const AliVTrack *t, const AliVCluster *v, Double_t &phidiff, Double_t &etadiff);
62  void UpdateCells();
63  void GetPass();
64  void FillCellQA(TH1F* h);
66 
67  // Containers and cells
68  AliParticleContainer *AddParticleContainer(const char *n) { return AliEmcalContainerUtils::AddContainer<AliParticleContainer>(n, fParticleCollArray); }
69  AliTrackContainer *AddTrackContainer(const char *n) { return AliEmcalContainerUtils::AddContainer<AliTrackContainer>(n, fParticleCollArray); }
70  AliMCParticleContainer *AddMCParticleContainer(const char *n) { return AliEmcalContainerUtils::AddContainer<AliMCParticleContainer>(n, fParticleCollArray); }
71  AliClusterContainer *AddClusterContainer(const char *n) { return AliEmcalContainerUtils::AddContainer<AliClusterContainer>(n, fClusterCollArray); }
76  AliParticleContainer *GetParticleContainer(Int_t i=0) const { return AliEmcalContainerUtils::GetContainer<AliParticleContainer>(i, fParticleCollArray); }
77  AliParticleContainer *GetParticleContainer(const char* name) const { return AliEmcalContainerUtils::GetContainer<AliParticleContainer>(name, fParticleCollArray); }
78  AliClusterContainer *GetClusterContainer(Int_t i=0) const { return AliEmcalContainerUtils::GetContainer<AliClusterContainer>(i, fClusterCollArray); }
79  AliClusterContainer *GetClusterContainer(const char* name) const { return AliEmcalContainerUtils::GetContainer<AliClusterContainer>(name, fClusterCollArray); }
81  AliMCParticleContainer *GetMCParticleContainer(const char* name) const { return dynamic_cast<AliMCParticleContainer*>(GetParticleContainer(name)); }
83  AliTrackContainer *GetTrackContainer(const char* name) const { return dynamic_cast<AliTrackContainer*>(GetParticleContainer(name)) ; }
84  void RemoveParticleContainer(Int_t i=0) { fParticleCollArray.RemoveAt(i) ; }
85  void RemoveClusterContainer(Int_t i=0) { fClusterCollArray.RemoveAt(i) ; }
86  AliVCaloCells *GetCaloCells() const { return fCaloCells; }
87  TList *GetOutputList() const { return fOutput; }
88 
89  void SetCaloCells(AliVCaloCells * cells) { fCaloCells = cells; }
90  void SetRecoUtils(AliEMCALRecoUtils *ru) { fRecoUtils = ru; }
91 
92  void SetInputEvent(AliVEvent * event) { fEventManager.SetInputEvent(event); }
93  void SetMCEvent(AliMCEvent * mcevent) { fMCEvent = mcevent; }
101 
102  void SetEMCALGeometry(AliEMCALGeometry * geometry ) { fGeom = geometry; }
103  void SetCentralityBin(Int_t bin) { fCentBin = bin; }
104  void SetCentrality(Double_t cent) { fCent = cent; }
106  void SetVertex(Double_t * vertex) { fVertex[0] = vertex[0]; fVertex[1] = vertex[1]; fVertex[2] = vertex[2]; }
107  void SetIsESD(Bool_t isESD) {fEsdMode = isESD; }
108 
109 #if !(defined(__CINT__) || defined(__MAKECINT__))
110  void SetUserConfiguration(YAML::Node & node) { fUserConfiguration = node; }
112  void SetDefaultConfiguration(YAML::Node & node) { fDefaultConfiguration = node; }
113 
115  template<typename T> bool GetProperty(std::string propertyName, T & property, bool requiredProperty = true, std::string correctionName = "");
116 
118  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 = "");
119 #endif
120  static bool IsSharedValue(std::string & value);
121 
122  protected:
123 
124 #if !(defined(__CINT__) || defined(__MAKECINT__))
125  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);
126  template<typename T> static bool GetPropertyFromNode(const YAML::Node & node, std::string propertyName, T & property);
127 
128  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);
129  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);
130 
131  YAML::Node fUserConfiguration;
133 #endif
134 
141  AliMCEvent *fMCEvent;
149  AliEMCALGeometry *fGeom;
154  AliVCaloCells *fCaloCells;
155  AliEMCALRecoUtils *fRecoUtils;
157 
159 
160  private:
161  AliEmcalCorrectionComponent(const AliEmcalCorrectionComponent &); // Not implemented
163 
165  ClassDef(AliEmcalCorrectionComponent, 3); // EMCal correction component
167 };
168 
169 #if !(defined(__CINT__) || defined(__MAKECINT__))
170 
182 template<typename T>
183 bool AliEmcalCorrectionComponent::GetProperty(std::string propertyName, T & property, bool requiredProperty, std::string correctionName)
184 {
185  // Get proper correction name
186  if (correctionName == "")
187  {
188  correctionName = GetName();
189  }
190  bool result = GetProperty(propertyName, property, fUserConfiguration, fDefaultConfiguration, requiredProperty, correctionName);
191 
192  return result;
193 }
194 
209 template<typename T>
210 bool AliEmcalCorrectionComponent::GetProperty(std::string propertyName, T & property, const YAML::Node & userConfiguration, const YAML::Node & defaultConfiguration, bool requiredProperty, std::string correctionName)
211 {
212  // Remove AliEmcalCorrection if in name
213  std::size_t prefixStringLocation = correctionName.find("AliEmcalCorrection");
214  if (prefixStringLocation != std::string::npos)
215  {
216  // AliEmcalCorrection is 18 characters
217  correctionName.erase(prefixStringLocation, prefixStringLocation + 18);
218  }
219 
220  bool setProperty = false;
221  // IsNull checks is a node is empty. A node is empty if it is created.
222  // IsDefined checks if the node that was requested was not actually created.
223  // In this case, the user configuration node is always created. If it IsNull, then we ignore it.
224  if (userConfiguration.IsNull() != true)
225  {
226  AliDebugClass(2, "Looking for parameter in user configuration");
227  YAML::Node sharedParameters = userConfiguration["sharedParameters"];
228  //std::cout << std::endl << "User Node: " << fUserConfiguration << std::endl;
229  setProperty = AliEmcalCorrectionComponent::GetPropertyFromNodes(userConfiguration, sharedParameters, propertyName, property, correctionName, "user");
230  }
231 
232  if (setProperty != true)
233  {
234  AliDebugClass(2, "Looking for parameter in default configuration");
235  YAML::Node sharedParameters = defaultConfiguration["sharedParameters"];
236  //std::cout << std::endl << "Default Node: " << fDefaultConfiguration << std::endl;
237  setProperty = AliEmcalCorrectionComponent::GetPropertyFromNodes(defaultConfiguration, sharedParameters, propertyName, property, correctionName, "default");
238  }
239 
240  if (setProperty != true && requiredProperty == true)
241  {
242  std::stringstream message;
243  message << "Failed to retrieve property \""
244  << (correctionName != "" ? correctionName + ":" : "")
245  << propertyName << "\" from default config!" << std::endl;
246  AliFatalClass(message.str().c_str());
247  }
248 
249  // Return whether the value was actually set
250  return setProperty;
251 }
252 
278 template<typename T>
279 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)
280 {
281  // Used as a buffer for printing complicated messages
282  std::stringstream tempMessage;
283 
284  bool returnValue = false;
285  if (nodesDeep > 2)
286  {
287  // Ensure that we do not go past two levels
288  tempMessage.str("");
289  tempMessage << "Went too many levels of recursion. Bailing out on \"" << correctionName << ":" << propertyName << "\" from " << configurationType << " config at level " << nodesDeep;
290  AliDebugClass(2, TString::Format("%s", tempMessage.str().c_str()));
291  return false;
292  }
293 
294  // Only want to print once to ensure the user is not overwhelmed!
295  if (nodesDeep == 0)
296  {
297  tempMessage.str("");
298  tempMessage << "Retreiving property \""
299  << (correctionName != "" ? correctionName + ":" : "")
300  << propertyName << "\" from " << configurationType << " config at level " << nodesDeep;
301  AliDebugClass(1, TString::Format("%s", tempMessage.str().c_str()));
302  }
303 
304  if (node.IsDefined() == true)
305  {
306  //std::cout << "Node: " << node << std::endl;
307  if (node[propertyName])
308  {
309  bool isShared = false;
310  // Will contain the name of the property in the sharedParameters section that should be retrieved
311  // if it is requested through the user configuration.
312  std::string sharedValueName = "";
313  // Necessary because it fails on vectors and other complex objects that are YAML sequences.
314  if (std::is_arithmetic<T>::value || std::is_same<T, std::string>::value || std::is_same<T, bool>::value)
315  {
316  // Retrieve value as string to check for shared value
317  sharedValueName = node[propertyName].as<std::string>();
318  // Check for a shared value
319  isShared = AliEmcalCorrectionComponent::IsSharedValue(sharedValueName);
320  }
321 
322  tempMessage.str("");
323  tempMessage << "property \""
324  << (nodesDeep > 0 ? correctionName + ":" : "")
325  << propertyName
326  << "\" using " << ( isShared ? "\"sharedParameters:" + sharedValueName + "\" in " : "" )
327  << "values from the " << configurationType
328  << " configuration at level " << nodesDeep;
329 
330  AliDebugClass(2, TString::Format("Retrieveing %s", tempMessage.str().c_str()));
331 
332  bool retrievalResult = false;
333  if (isShared == true)
334  {
335  retrievalResult = GetPropertyFromNode(sharedParametersNode, sharedValueName, property);
336  }
337  else
338  {
339  retrievalResult = GetPropertyFromNode(node, propertyName, property);
340  }
341 
342  // Inform about the result
343  if (retrievalResult == true)
344  {
345  // Add the retrieved value to the message (only if trivially printable)
346  PrintRetrievedPropertyValue(property, tempMessage);
347  AliDebugClassStream(2) << "Succeeded in retrieveing " << tempMessage.str() << std::endl;
348  returnValue = true;
349  }
350  else
351  {
352  // Only fatal if we have exhausted our last option, the default
353  if (configurationType == "default")
354  {
355  AliFatalClass(TString::Format("Failed to retrieve %s", tempMessage.str().c_str()));
356  }
357  else
358  {
359  AliDebugClass(2, TString::Format("Failed to retrieve %s", tempMessage.str().c_str()));
360  }
361  returnValue = false;
362  }
363  }
364  else
365  {
366  // Go one node deeper using recursion
367  // Must create a new node, since we took the original node by reference
368  YAML::Node deeperNode = node[correctionName];
369  nodesDeep++;
370  AliDebugClass(2, TString::Format("Going a node deeper with \"%s\" to level %i", correctionName.c_str(), nodesDeep));
371  returnValue = GetPropertyFromNodes(deeperNode, sharedParametersNode, propertyName, property, correctionName, configurationType, nodesDeep);
372 
373  // If we didn't find it, next try a substring
374  if (returnValue == false)
375  {
376  // Don't increment nodesDeep, because we are about to go another node deep anyway
377  std::size_t splitLocation = correctionName.find("_");
378  // We only want to look at the split if we are only one node deep and
379  // if the split is actually meaningful
380  if (splitLocation != std::string::npos && nodesDeep == 1)
381  {
382  // Split from start to underscore
383  std::string subCorrectionName = correctionName.substr(0, splitLocation);
384  AliDebugClass(2, TString::Format("Attempting to retrieve property \"%s\" from base correction \"%s\" at level %i", propertyName.c_str(), subCorrectionName.c_str(), nodesDeep));
385  // Retrieve the base correction node
386  // Need to create new node! Otherwise it will assign the correctionName node to subCorrectionName node!
387  YAML::Node subCorrectionNode = node[subCorrectionName];
388  returnValue = GetPropertyFromNodes(subCorrectionNode, sharedParametersNode, propertyName, property, subCorrectionName, configurationType, nodesDeep);
389  }
390  }
391  }
392  }
393  else
394  {
395  tempMessage.str("");
396  tempMessage << "Node is undefined for \""
397  << (correctionName != "" ? correctionName + ":" : "")
398  << propertyName << "\" from " << configurationType << " config at level " << nodesDeep;
399  AliDebugClass(2, TString::Format("%s", tempMessage.str().c_str()));
400 
401  returnValue = false;
402  }
403 
404  return returnValue;
405 }
406 
417 template<typename T>
418 typename std::enable_if<std::is_arithmetic<T>::value || std::is_same<T, std::string>::value || std::is_same<T, bool>::value>::type
419 AliEmcalCorrectionComponent::PrintRetrievedPropertyValue(T & property, std::stringstream & tempMessage)
420 {
421  //AliDebugClassStream(2) << " with value " << property;
422  tempMessage << " with value \"" << property << "\"";
423 }
424 
433 template<typename T>
434 typename std::enable_if<!std::is_arithmetic<T>::value && !std::is_same<T, std::string>::value && !std::is_same<T, bool>::value>::type
435 AliEmcalCorrectionComponent::PrintRetrievedPropertyValue(T & property, std::stringstream & tempMessage)
436 {
437  // Cannot easily print these types, so just note that is the case!
438  tempMessage << " with a value that cannot be trivially printed";
439 }
440 
448 template<typename T>
449 bool AliEmcalCorrectionComponent::GetPropertyFromNode(const YAML::Node & node, std::string propertyName, T & property)
450 {
451  if (node[propertyName])
452  {
453  property = node[propertyName].as<T>();
454  return true;
455  }
456  return false;
457 }
458 
459 #endif /* Hide yaml from CINT */
460 
479 template<typename T> AliEmcalCorrectionComponent * createT() { return new T; }
481 
482 // Factory to create and keep track of new components
484 {
485  public:
487 
488  typedef std::map<std::string, AliEmcalCorrectionComponent*(*)()> map_type;
489 
491  static AliEmcalCorrectionComponent * createInstance(std::string const& s)
492  {
493  map_type::iterator it = getMap()->find(s);
494  if(it == getMap()->end())
495  return 0;
496  // Initializes the function with empty arguments (?)
497  return it->second();
498  }
499 
500  protected:
502  static map_type * getMap() {
503  // We never delete the map (until program termination) because we cannot guarantee
504  // correct destruction order
505  if(!componentMap) { componentMap = new map_type; }
506  return componentMap;
507  }
508 
509  private:
512 };
513 
526 template<typename T>
528 {
529  public:
531  RegisterCorrectionComponent(std::string const& s)
532  {
533  getMap()->insert(std::make_pair(s, &createT<T>));
534  }
535 };
536 
537 #endif /* ALIEMCALCORRECTIONCOMPONENT_H */
Int_t fNcentBins
How many centrality bins (this member copied from AliAnalysisTaskEmcal)
void AdoptClusterContainer(AliClusterContainer *cont)
AliMCParticleContainer * GetMCParticleContainer(Int_t i=0) const
AliParticleContainer * GetParticleContainer(Int_t i=0) const
AliEMCALGeometry * fGeom
! Geometry object
double Double_t
Definition: External.C:58
AliTrackContainer * GetTrackContainer(Int_t i=0) const
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 SetEMCALGeometry(AliEMCALGeometry *geometry)
AliVCaloCells * GetCaloCells() const
Class to manage the current event for correction components in the EMCal correction framework...
void SetRecoUtils(AliEMCALRecoUtils *ru)
void SetMCEvent(AliMCEvent *mcevent)
Container with name, TClonesArray and cuts for particles.
Registers EMCal correction components in the factory map.
AliEmcalCorrectionComponent * createT()
Template function for creating a new component. Used to register the component.
AliParticleContainer * GetParticleContainer(const char *name) const
Int_t fCentBin
! Event centrality bin
AliClusterContainer * AddClusterContainer(const char *n)
AliVCaloCells * fCaloCells
! Pointer to CaloCells
Container for particles within the EMCAL framework.
TObjArray fParticleCollArray
Particle/track collection array.
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.
TObjArray fClusterCollArray
Cluster collection array.
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)
AliMCParticleContainer * AddMCParticleContainer(const char *n)
YAML::Node fDefaultConfiguration
! Default YAML configuration
void AdoptParticleContainer(AliParticleContainer *cont)
AliParticleContainer * AddParticleContainer(const char *n)
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.
void AdoptMCParticleContainer(AliMCParticleContainer *cont)
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.
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
AliTrackContainer * GetTrackContainer(const char *name) const
AliClusterContainer * GetClusterContainer(const char *name) const
AliTrackContainer * AddTrackContainer(const char *n)
void SetDefaultConfiguration(YAML::Node &node)
Double_t fMinBinPt
Min pt in histograms.
AliEmcalCorrectionEventManager fEventManager
Minimal task which inherits from AliAnalysisTaskSE and manages access to the event.
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...
bool Bool_t
Definition: External.C:53
AliClusterContainer * GetClusterContainer(Int_t i=0) const
void AdoptTrackContainer(AliTrackContainer *cont)
Container structure for EMCAL clusters.
Container for MC-true particles within the EMCAL framework.
TString fFilepass
Input data pass number.
AliMCParticleContainer * GetMCParticleContainer(const char *name) const
bool GetProperty(std::string propertyName, T &property, bool requiredProperty=true, std::string correctionName="")
Retrieve property.
Bool_t fIsEmbedded
Trigger, embedded signal.