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