AliPhysics  1a228f7 (1a228f7)
 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 
37  public:
39  AliEmcalCorrectionComponent(const char * name);
41 
42  // Virtual functions to be overloaded
43  virtual Bool_t Initialize();
44  virtual void UserCreateOutputObjects();
45  virtual void ExecOnce();
46  virtual Bool_t Run();
47  virtual Bool_t UserNotify();
48 
49  void GetEtaPhiDiff(const AliVTrack *t, const AliVCluster *v, Double_t &phidiff, Double_t &etadiff);
50  void UpdateCells();
52  void GetPass();
53  void FillCellQA(TH1F* h);
54 
57  AliVCaloCells * GetCaloCells() { return fCaloCells; }
58  TList * GetOutputList() { return fOutput; }
59 
62  void SetCaloCells(AliVCaloCells * cells) { fCaloCells = cells; }
63  void SetRecoUtils(AliEMCALRecoUtils *ru) { fRecoUtils = ru; }
64 
65  void SetEvent(AliVEvent * event) { fEvent = event; }
66  void SetMCEvent(AliMCEvent * mcevent) { fMCEvent = mcevent; }
67 
68  void SetEMCALGeometry(AliEMCALGeometry * geometry ) { fGeom = geometry; }
69  void SetCentralityBin(Int_t bin) { fCentBin = bin; }
70  void SetCentrality(Double_t cent) { fCent = cent; }
72  void SetIsESD(Bool_t isESD) {fEsdMode = isESD; }
73 
74 #if !(defined(__CINT__) || defined(__MAKECINT__))
75  void SetUserConfiguration(YAML::Node & node) { fUserConfiguration = node; }
77  void SetDefaultConfiguration(YAML::Node & node) { fDefaultConfiguration = node; }
78 
80  template<typename T> bool GetProperty(std::string propertyName, T & property, bool requiredProperty = true, std::string correctionName = "");
81 
83  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 = "");
84 #endif
85  static bool IsSharedValue(std::string & value);
86 
87  protected:
88 
89 #if !(defined(__CINT__) || defined(__MAKECINT__))
90  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);
91  template<typename T> static bool GetPropertyFromNode(const YAML::Node & node, std::string propertyName, T & property);
92 
93  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);
94  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);
95 
96  YAML::Node fUserConfiguration;
98 #endif
99 
104  AliVEvent *fEvent;
106  AliMCEvent *fMCEvent;
114  AliEMCALGeometry *fGeom;
119  AliVCaloCells *fCaloCells;
120  AliEMCALRecoUtils *fRecoUtils;
122 
124 
125  private:
126  AliEmcalCorrectionComponent(const AliEmcalCorrectionComponent &); // Not implemented
128 
130  ClassDef(AliEmcalCorrectionComponent, 1); // EMCal correction component
132 };
133 
134 #if !(defined(__CINT__) || defined(__MAKECINT__))
135 
139 template<typename T>
140 bool AliEmcalCorrectionComponent::GetProperty(std::string propertyName, T & property, bool requiredProperty, std::string correctionName)
141 {
142  // Get proper correction name
143  if (correctionName == "")
144  {
145  correctionName = GetName();
146  }
147  bool result = GetProperty(propertyName, property, fUserConfiguration, fDefaultConfiguration, requiredProperty, correctionName);
148 
149  return result;
150 }
151 
156 template<typename T>
157 bool AliEmcalCorrectionComponent::GetProperty(std::string propertyName, T & property, const YAML::Node & userConfiguration, const YAML::Node & defaultConfiguration, bool requiredProperty, std::string correctionName)
158 {
159  // Remove AliEmcalCorrection if in name
160  std::size_t prefixStringLocation = correctionName.find("AliEmcalCorrection");
161  if (prefixStringLocation != std::string::npos)
162  {
163  // AliEmcalCorrection is 18 characters
164  correctionName.erase(prefixStringLocation, prefixStringLocation + 18);
165  }
166 
167  bool setProperty = false;
168  // IsNull checks is a node is empty. A node is empty if it is created.
169  // IsDefined checks if the node that was requested was not actually created.
170  // In this case, the user configuration node is always created. If it IsNull, then we ignore it.
171  if (userConfiguration.IsNull() != true)
172  {
173  AliDebugClass(2, "Looking for parameter in user configuration");
174  YAML::Node sharedParameters = userConfiguration["sharedParameters"];
175  //std::cout << std::endl << "User Node: " << fUserConfiguration << std::endl;
176  setProperty = AliEmcalCorrectionComponent::GetPropertyFromNodes(userConfiguration, sharedParameters, propertyName, property, correctionName, "user");
177  }
178 
179  if (setProperty != true)
180  {
181  AliDebugClass(2, "Looking for parameter in default configuration");
182  YAML::Node sharedParameters = defaultConfiguration["sharedParameters"];
183  //std::cout << std::endl << "Default Node: " << fDefaultConfiguration << std::endl;
184  setProperty = AliEmcalCorrectionComponent::GetPropertyFromNodes(defaultConfiguration, sharedParameters, propertyName, property, correctionName, "default");
185  }
186 
187  if (setProperty != true && requiredProperty == true)
188  {
189  std::stringstream message;
190  message << "Failed to retrieve property \""
191  << (correctionName != "" ? correctionName + ":" : "")
192  << propertyName << "\" from default config!" << std::endl;
193  AliFatalClass(message.str().c_str());
194  }
195 
196  // Return whether the value was actually set
197  return setProperty;
198 }
199 
204 template<typename T>
205 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)
206 {
207  // Used as a buffer for printing complicated messages
208  std::stringstream tempMessage;
209 
210  bool returnValue = false;
211  if (nodesDeep > 2)
212  {
213  // Ensure that we do not go past two levels
214  tempMessage.str("");
215  tempMessage << "Went too many levels of recursion. Bailing out on \"" << correctionName << ":" << propertyName << "\" from " << configurationType << " config at level " << nodesDeep;
216  AliDebugClass(2, TString::Format("%s", tempMessage.str().c_str()));
217  return false;
218  }
219 
220  // Only want to print once to ensure the user is not overwhelmed!
221  if (nodesDeep == 0)
222  {
223  tempMessage.str("");
224  tempMessage << "Retreiving property \""
225  << (correctionName != "" ? correctionName + ":" : "")
226  << propertyName << "\" from " << configurationType << " config at level " << nodesDeep;
227  AliDebugClass(1, TString::Format("%s", tempMessage.str().c_str()));
228  }
229 
230  if (node.IsDefined() == true)
231  {
232  //std::cout << "Node: " << node << std::endl;
233  if (node[propertyName])
234  {
235  bool isShared = false;
236  // Will contain the name of the property in the sharedParameters section that should be retrieved
237  // if it is requested through the user configuration.
238  std::string sharedValueName = "";
239  // Necessary because it fails on vectors and other complex objects that are YAML sequences.
240  if (std::is_arithmetic<T>::value || std::is_same<T, std::string>::value || std::is_same<T, bool>::value)
241  {
242  // Retrieve value as string to check for shared value
243  sharedValueName = node[propertyName].as<std::string>();
244  // Check for a shared value
245  isShared = AliEmcalCorrectionComponent::IsSharedValue(sharedValueName);
246  }
247 
248  tempMessage.str("");
249  tempMessage << "property \""
250  << (nodesDeep > 0 ? correctionName + ":" : "")
251  << propertyName
252  << "\" using " << ( isShared ? "\"sharedParameters:" + sharedValueName + "\" in " : "" )
253  << "values from the " << configurationType
254  << " configuration at level " << nodesDeep;
255 
256  AliDebugClass(2, TString::Format("Retrieveing %s", tempMessage.str().c_str()));
257 
258  bool retrievalResult = false;
259  if (isShared == true)
260  {
261  retrievalResult = GetPropertyFromNode(sharedParametersNode, sharedValueName, property);
262  }
263  else
264  {
265  retrievalResult = GetPropertyFromNode(node, propertyName, property);
266  }
267 
268  // Inform about the result
269  if (retrievalResult == true)
270  {
271  // Add the retrieved value to the message (only if trivially printable)
272  PrintRetrievedPropertyValue(property, tempMessage);
273  AliDebugClassStream(2) << "Succeeded in retrieveing " << tempMessage.str() << std::endl;
274  returnValue = true;
275  }
276  else
277  {
278  // Only fatal if we have exhausted our last option, the default
279  if (configurationType == "default")
280  {
281  AliFatalClass(TString::Format("Failed to retrieve %s", tempMessage.str().c_str()));
282  }
283  else
284  {
285  AliDebugClass(2, TString::Format("Failed to retrieve %s", tempMessage.str().c_str()));
286  }
287  returnValue = false;
288  }
289  }
290  else
291  {
292  // Go one node deeper using recursion
293  // Must create a new node, since we took the original node by reference
294  YAML::Node deeperNode = node[correctionName];
295  nodesDeep++;
296  AliDebugClass(2, TString::Format("Going a node deeper with \"%s\" to level %i", correctionName.c_str(), nodesDeep));
297  returnValue = GetPropertyFromNodes(deeperNode, sharedParametersNode, propertyName, property, correctionName, configurationType, nodesDeep);
298 
299  // If we didn't find it, next try a substring
300  if (returnValue == false)
301  {
302  // Don't increment nodesDeep, because we are about to go another node deep anyway
303  std::size_t splitLocation = correctionName.find("_");
304  // We only want to look at the split if we are only one node deep and
305  // if the split is actually meaningful
306  if (splitLocation != std::string::npos && nodesDeep == 1)
307  {
308  // Split from start to underscore
309  std::string subCorrectionName = correctionName.substr(0, splitLocation);
310  AliDebugClass(2, TString::Format("Attempting to retrieve property \"%s\" from base correction \"%s\" at level %i", propertyName.c_str(), subCorrectionName.c_str(), nodesDeep));
311  // Retrieve the base correction node
312  // Need to create new node! Otherwise it will assign the correctionName node to subCorrectionName node!
313  YAML::Node subCorrectionNode = node[subCorrectionName];
314  returnValue = GetPropertyFromNodes(subCorrectionNode, sharedParametersNode, propertyName, property, subCorrectionName, configurationType, nodesDeep);
315  }
316  }
317  }
318  }
319  else
320  {
321  tempMessage.str("");
322  tempMessage << "Node is undefined for \""
323  << (correctionName != "" ? correctionName + ":" : "")
324  << propertyName << "\" from " << configurationType << " config at level " << nodesDeep;
325  AliDebugClass(2, TString::Format("%s", tempMessage.str().c_str()));
326 
327  returnValue = false;
328  }
329 
330  return returnValue;
331 }
332 
338 template<typename T>
339 typename std::enable_if<std::is_arithmetic<T>::value || std::is_same<T, std::string>::value || std::is_same<T, bool>::value>::type
340 AliEmcalCorrectionComponent::PrintRetrievedPropertyValue(T & property, std::stringstream & tempMessage)
341 {
342  //AliDebugClassStream(2) << " with value " << property;
343  tempMessage << " with value \"" << property << "\"";
344 }
345 
350 template<typename T>
351 typename std::enable_if<!std::is_arithmetic<T>::value && !std::is_same<T, std::string>::value && !std::is_same<T, bool>::value>::type
352 AliEmcalCorrectionComponent::PrintRetrievedPropertyValue(T & property, std::stringstream & tempMessage)
353 {
354  // Cannot easily print these types, so just note that is the case!
355  tempMessage << " with a value that cannot be trivially printed";
356 }
357 
362 template<typename T>
363 bool AliEmcalCorrectionComponent::GetPropertyFromNode(const YAML::Node & node, std::string propertyName, T & property)
364 {
365  if (node[propertyName])
366  {
367  property = node[propertyName].as<T>();
368  return true;
369  }
370  return false;
371 }
372 
373 #endif /* Hide yaml from CINT */
374 
375 // Below is based on: https://stackoverflow.com/a/582456 , and editted for our purposes.
376 //
377 // Template for creating a new component. Used to register the component.
378 template<typename T> AliEmcalCorrectionComponent * createT() { return new T; }
379 
380 // Factory to create and keep track of new components
382 {
383  public:
385 
386  typedef std::map<std::string, AliEmcalCorrectionComponent*(*)()> map_type;
387 
388  // Creates an instance of an object based on the name if the name is registered in the map.
389  static AliEmcalCorrectionComponent * createInstance(std::string const& s)
390  {
391  map_type::iterator it = getMap()->find(s);
392  if(it == getMap()->end())
393  return 0;
394  // Initializes the function with empty arguments (?)
395  return it->second();
396  }
397 
398  protected:
399  // Creates and access the component map
400  static map_type * getMap() {
401  // We never delete the map (until program termination) because we cannot guarantee
402  // correct destruction order
403  if(!componentMap) { componentMap = new map_type; }
404  return componentMap;
405  }
406 
407  private:
408  // Contains the map to all of the components
410 };
411 
412 // Allows registration of new components into the factory map.
413 template<typename T>
415 {
416  public:
417  RegisterCorrectionComponent(std::string const& s)
418  {
419  getMap()->insert(std::make_pair(s, &createT<T>));
420  }
421 };
422 
423 #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)
void SetClusterContainer(AliClusterContainer *cont)
void SetEMCALGeometry(AliEMCALGeometry *geometry)
void SetRecoUtils(AliEMCALRecoUtils *ru)
void SetMCEvent(AliMCEvent *mcevent)
AliEmcalCorrectionComponent * createT()
Int_t fCentBin
!event centrality bin
AliVCaloCells * fCaloCells
! pointer to calo cells
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 reco utils
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)
Int_t fMinMCLabel
minimum MC label value for the tracks/clusters being considered MC particles
std::map< std::string, AliEmcalCorrectionComponent *(*)()> map_type
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
void SetDefaultConfiguration(YAML::Node &node)
Double_t fMinBinPt
min pt in histograms
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)
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