AliPhysics  cdeda5a (cdeda5a)
 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 AliEMCALGeometry;
21 class AliVEvent;
22 #include <AliLog.h>
23 #include "AliEmcalContainerUtils.h"
24 #include "AliParticleContainer.h"
25 #include "AliMCParticleContainer.h"
26 #include "AliTrackContainer.h"
27 #include "AliClusterContainer.h"
28 
47  public:
49  AliEmcalCorrectionComponent(const char * name);
51 
52  // Virtual functions to be overloaded
53  virtual Bool_t Initialize();
54  virtual void UserCreateOutputObjects();
55  virtual void ExecOnce();
56  virtual Bool_t Run();
57  virtual Bool_t UserNotify();
58 
59  void GetEtaPhiDiff(const AliVTrack *t, const AliVCluster *v, Double_t &phidiff, Double_t &etadiff);
60  void UpdateCells();
62  void GetPass();
63  void FillCellQA(TH1F* h);
64 
65  // Containers and cells
66  AliParticleContainer *AddParticleContainer(const char *n) { return AliEmcalContainerUtils::AddContainer<AliParticleContainer>(n, fParticleCollArray); }
67  AliTrackContainer *AddTrackContainer(const char *n) { return AliEmcalContainerUtils::AddContainer<AliTrackContainer>(n, fParticleCollArray); }
68  AliMCParticleContainer *AddMCParticleContainer(const char *n) { return AliEmcalContainerUtils::AddContainer<AliMCParticleContainer>(n, fParticleCollArray); }
69  AliClusterContainer *AddClusterContainer(const char *n) { return AliEmcalContainerUtils::AddContainer<AliClusterContainer>(n, fClusterCollArray); }
74  AliParticleContainer *GetParticleContainer(Int_t i=0) const { return AliEmcalContainerUtils::GetContainer<AliParticleContainer>(i, fParticleCollArray); }
75  AliParticleContainer *GetParticleContainer(const char* name) const { return AliEmcalContainerUtils::GetContainer<AliParticleContainer>(name, fParticleCollArray); }
76  AliClusterContainer *GetClusterContainer(Int_t i=0) const { return AliEmcalContainerUtils::GetContainer<AliClusterContainer>(i, fClusterCollArray); }
77  AliClusterContainer *GetClusterContainer(const char* name) const { return AliEmcalContainerUtils::GetContainer<AliClusterContainer>(name, fClusterCollArray); }
79  AliMCParticleContainer *GetMCParticleContainer(const char* name) const { return dynamic_cast<AliMCParticleContainer*>(GetParticleContainer(name)); }
81  AliTrackContainer *GetTrackContainer(const char* name) const { return dynamic_cast<AliTrackContainer*>(GetParticleContainer(name)) ; }
82  void RemoveParticleContainer(Int_t i=0) { fParticleCollArray.RemoveAt(i) ; }
83  void RemoveClusterContainer(Int_t i=0) { fClusterCollArray.RemoveAt(i) ; }
84  AliVCaloCells *GetCaloCells() const { return fCaloCells; }
85  TList *GetOutputList() const { return fOutput; }
86 
87  void SetCaloCells(AliVCaloCells * cells) { fCaloCells = cells; }
88  void SetRecoUtils(AliEMCALRecoUtils *ru) { fRecoUtils = ru; }
89 
90  void SetEvent(AliVEvent * event) { fEvent = event; }
91  void SetMCEvent(AliMCEvent * mcevent) { fMCEvent = mcevent; }
92 
93  void SetEMCALGeometry(AliEMCALGeometry * geometry ) { fGeom = geometry; }
94  void SetCentralityBin(Int_t bin) { fCentBin = bin; }
95  void SetCentrality(Double_t cent) { fCent = cent; }
97  void SetIsESD(Bool_t isESD) {fEsdMode = isESD; }
98 
99 #if !(defined(__CINT__) || defined(__MAKECINT__))
100  void SetUserConfiguration(YAML::Node & node) { fUserConfiguration = node; }
102  void SetDefaultConfiguration(YAML::Node & node) { fDefaultConfiguration = node; }
103 
105  template<typename T> bool GetProperty(std::string propertyName, T & property, bool requiredProperty = true, std::string correctionName = "");
106 
108  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 = "");
109 #endif
110  static bool IsSharedValue(std::string & value);
111 
112  protected:
113 
114 #if !(defined(__CINT__) || defined(__MAKECINT__))
115  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);
116  template<typename T> static bool GetPropertyFromNode(const YAML::Node & node, std::string propertyName, T & property);
117 
118  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);
119  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);
120 
121  YAML::Node fUserConfiguration;
123 #endif
124 
129  AliVEvent *fEvent;
131  AliMCEvent *fMCEvent;
139  AliEMCALGeometry *fGeom;
144  AliVCaloCells *fCaloCells;
145  AliEMCALRecoUtils *fRecoUtils;
147 
149 
150  private:
151  AliEmcalCorrectionComponent(const AliEmcalCorrectionComponent &); // Not implemented
153 
155  ClassDef(AliEmcalCorrectionComponent, 2); // EMCal correction component
157 };
158 
159 #if !(defined(__CINT__) || defined(__MAKECINT__))
160 
172 template<typename T>
173 bool AliEmcalCorrectionComponent::GetProperty(std::string propertyName, T & property, bool requiredProperty, std::string correctionName)
174 {
175  // Get proper correction name
176  if (correctionName == "")
177  {
178  correctionName = GetName();
179  }
180  bool result = GetProperty(propertyName, property, fUserConfiguration, fDefaultConfiguration, requiredProperty, correctionName);
181 
182  return result;
183 }
184 
199 template<typename T>
200 bool AliEmcalCorrectionComponent::GetProperty(std::string propertyName, T & property, const YAML::Node & userConfiguration, const YAML::Node & defaultConfiguration, bool requiredProperty, std::string correctionName)
201 {
202  // Remove AliEmcalCorrection if in name
203  std::size_t prefixStringLocation = correctionName.find("AliEmcalCorrection");
204  if (prefixStringLocation != std::string::npos)
205  {
206  // AliEmcalCorrection is 18 characters
207  correctionName.erase(prefixStringLocation, prefixStringLocation + 18);
208  }
209 
210  bool setProperty = false;
211  // IsNull checks is a node is empty. A node is empty if it is created.
212  // IsDefined checks if the node that was requested was not actually created.
213  // In this case, the user configuration node is always created. If it IsNull, then we ignore it.
214  if (userConfiguration.IsNull() != true)
215  {
216  AliDebugClass(2, "Looking for parameter in user configuration");
217  YAML::Node sharedParameters = userConfiguration["sharedParameters"];
218  //std::cout << std::endl << "User Node: " << fUserConfiguration << std::endl;
219  setProperty = AliEmcalCorrectionComponent::GetPropertyFromNodes(userConfiguration, sharedParameters, propertyName, property, correctionName, "user");
220  }
221 
222  if (setProperty != true)
223  {
224  AliDebugClass(2, "Looking for parameter in default configuration");
225  YAML::Node sharedParameters = defaultConfiguration["sharedParameters"];
226  //std::cout << std::endl << "Default Node: " << fDefaultConfiguration << std::endl;
227  setProperty = AliEmcalCorrectionComponent::GetPropertyFromNodes(defaultConfiguration, sharedParameters, propertyName, property, correctionName, "default");
228  }
229 
230  if (setProperty != true && requiredProperty == true)
231  {
232  std::stringstream message;
233  message << "Failed to retrieve property \""
234  << (correctionName != "" ? correctionName + ":" : "")
235  << propertyName << "\" from default config!" << std::endl;
236  AliFatalClass(message.str().c_str());
237  }
238 
239  // Return whether the value was actually set
240  return setProperty;
241 }
242 
268 template<typename T>
269 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)
270 {
271  // Used as a buffer for printing complicated messages
272  std::stringstream tempMessage;
273 
274  bool returnValue = false;
275  if (nodesDeep > 2)
276  {
277  // Ensure that we do not go past two levels
278  tempMessage.str("");
279  tempMessage << "Went too many levels of recursion. Bailing out on \"" << correctionName << ":" << propertyName << "\" from " << configurationType << " config at level " << nodesDeep;
280  AliDebugClass(2, TString::Format("%s", tempMessage.str().c_str()));
281  return false;
282  }
283 
284  // Only want to print once to ensure the user is not overwhelmed!
285  if (nodesDeep == 0)
286  {
287  tempMessage.str("");
288  tempMessage << "Retreiving property \""
289  << (correctionName != "" ? correctionName + ":" : "")
290  << propertyName << "\" from " << configurationType << " config at level " << nodesDeep;
291  AliDebugClass(1, TString::Format("%s", tempMessage.str().c_str()));
292  }
293 
294  if (node.IsDefined() == true)
295  {
296  //std::cout << "Node: " << node << std::endl;
297  if (node[propertyName])
298  {
299  bool isShared = false;
300  // Will contain the name of the property in the sharedParameters section that should be retrieved
301  // if it is requested through the user configuration.
302  std::string sharedValueName = "";
303  // Necessary because it fails on vectors and other complex objects that are YAML sequences.
304  if (std::is_arithmetic<T>::value || std::is_same<T, std::string>::value || std::is_same<T, bool>::value)
305  {
306  // Retrieve value as string to check for shared value
307  sharedValueName = node[propertyName].as<std::string>();
308  // Check for a shared value
309  isShared = AliEmcalCorrectionComponent::IsSharedValue(sharedValueName);
310  }
311 
312  tempMessage.str("");
313  tempMessage << "property \""
314  << (nodesDeep > 0 ? correctionName + ":" : "")
315  << propertyName
316  << "\" using " << ( isShared ? "\"sharedParameters:" + sharedValueName + "\" in " : "" )
317  << "values from the " << configurationType
318  << " configuration at level " << nodesDeep;
319 
320  AliDebugClass(2, TString::Format("Retrieveing %s", tempMessage.str().c_str()));
321 
322  bool retrievalResult = false;
323  if (isShared == true)
324  {
325  retrievalResult = GetPropertyFromNode(sharedParametersNode, sharedValueName, property);
326  }
327  else
328  {
329  retrievalResult = GetPropertyFromNode(node, propertyName, property);
330  }
331 
332  // Inform about the result
333  if (retrievalResult == true)
334  {
335  // Add the retrieved value to the message (only if trivially printable)
336  PrintRetrievedPropertyValue(property, tempMessage);
337  AliDebugClassStream(2) << "Succeeded in retrieveing " << tempMessage.str() << std::endl;
338  returnValue = true;
339  }
340  else
341  {
342  // Only fatal if we have exhausted our last option, the default
343  if (configurationType == "default")
344  {
345  AliFatalClass(TString::Format("Failed to retrieve %s", tempMessage.str().c_str()));
346  }
347  else
348  {
349  AliDebugClass(2, TString::Format("Failed to retrieve %s", tempMessage.str().c_str()));
350  }
351  returnValue = false;
352  }
353  }
354  else
355  {
356  // Go one node deeper using recursion
357  // Must create a new node, since we took the original node by reference
358  YAML::Node deeperNode = node[correctionName];
359  nodesDeep++;
360  AliDebugClass(2, TString::Format("Going a node deeper with \"%s\" to level %i", correctionName.c_str(), nodesDeep));
361  returnValue = GetPropertyFromNodes(deeperNode, sharedParametersNode, propertyName, property, correctionName, configurationType, nodesDeep);
362 
363  // If we didn't find it, next try a substring
364  if (returnValue == false)
365  {
366  // Don't increment nodesDeep, because we are about to go another node deep anyway
367  std::size_t splitLocation = correctionName.find("_");
368  // We only want to look at the split if we are only one node deep and
369  // if the split is actually meaningful
370  if (splitLocation != std::string::npos && nodesDeep == 1)
371  {
372  // Split from start to underscore
373  std::string subCorrectionName = correctionName.substr(0, splitLocation);
374  AliDebugClass(2, TString::Format("Attempting to retrieve property \"%s\" from base correction \"%s\" at level %i", propertyName.c_str(), subCorrectionName.c_str(), nodesDeep));
375  // Retrieve the base correction node
376  // Need to create new node! Otherwise it will assign the correctionName node to subCorrectionName node!
377  YAML::Node subCorrectionNode = node[subCorrectionName];
378  returnValue = GetPropertyFromNodes(subCorrectionNode, sharedParametersNode, propertyName, property, subCorrectionName, configurationType, nodesDeep);
379  }
380  }
381  }
382  }
383  else
384  {
385  tempMessage.str("");
386  tempMessage << "Node is undefined for \""
387  << (correctionName != "" ? correctionName + ":" : "")
388  << propertyName << "\" from " << configurationType << " config at level " << nodesDeep;
389  AliDebugClass(2, TString::Format("%s", tempMessage.str().c_str()));
390 
391  returnValue = false;
392  }
393 
394  return returnValue;
395 }
396 
407 template<typename T>
408 typename std::enable_if<std::is_arithmetic<T>::value || std::is_same<T, std::string>::value || std::is_same<T, bool>::value>::type
409 AliEmcalCorrectionComponent::PrintRetrievedPropertyValue(T & property, std::stringstream & tempMessage)
410 {
411  //AliDebugClassStream(2) << " with value " << property;
412  tempMessage << " with value \"" << property << "\"";
413 }
414 
423 template<typename T>
424 typename std::enable_if<!std::is_arithmetic<T>::value && !std::is_same<T, std::string>::value && !std::is_same<T, bool>::value>::type
425 AliEmcalCorrectionComponent::PrintRetrievedPropertyValue(T & property, std::stringstream & tempMessage)
426 {
427  // Cannot easily print these types, so just note that is the case!
428  tempMessage << " with a value that cannot be trivially printed";
429 }
430 
438 template<typename T>
439 bool AliEmcalCorrectionComponent::GetPropertyFromNode(const YAML::Node & node, std::string propertyName, T & property)
440 {
441  if (node[propertyName])
442  {
443  property = node[propertyName].as<T>();
444  return true;
445  }
446  return false;
447 }
448 
449 #endif /* Hide yaml from CINT */
450 
469 template<typename T> AliEmcalCorrectionComponent * createT() { return new T; }
471 
472 // Factory to create and keep track of new components
474 {
475  public:
477 
478  typedef std::map<std::string, AliEmcalCorrectionComponent*(*)()> map_type;
479 
481  static AliEmcalCorrectionComponent * createInstance(std::string const& s)
482  {
483  map_type::iterator it = getMap()->find(s);
484  if(it == getMap()->end())
485  return 0;
486  // Initializes the function with empty arguments (?)
487  return it->second();
488  }
489 
490  protected:
492  static map_type * getMap() {
493  // We never delete the map (until program termination) because we cannot guarantee
494  // correct destruction order
495  if(!componentMap) { componentMap = new map_type; }
496  return componentMap;
497  }
498 
499  private:
502 };
503 
516 template<typename T>
518 {
519  public:
521  RegisterCorrectionComponent(std::string const& s)
522  {
523  getMap()->insert(std::make_pair(s, &createT<T>));
524  }
525 };
526 
527 #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
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.
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
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.
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.