AliPhysics  32b88a8 (32b88a8)
 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 // AliEmcalCorrectionComponent
5 //
6 
7 #include <map>
8 // For std::pair
9 #include <utility>
10 #include <string>
11 #include <iostream>
12 
13 // CINT can't handle the yaml header!
14 #if !(defined(__CINT__) || defined(__MAKECINT__))
15 #include <yaml-cpp/yaml.h>
16 #endif
17 
18 #include <Rtypes.h>
19 #include <TNamed.h>
20 #include <TFile.h>
21 
22 #include "AliAnalysisTaskEmcal.h"
23 #include "AliLog.h"
24 #include "AliEMCALRecoUtils.h"
25 #include "AliAODEvent.h"
26 #include "AliESDEvent.h"
27 #include "AliAODCaloCluster.h"
28 #include "AliEmcalParticle.h"
29 #include "AliEMCALGeometry.h"
30 #include "AliAnalysisManager.h"
31 
33  public:
35  kCaloCells = 0,
36  kCluster = 1,
37  kTrack = 2,
38  };
39 
41  AliEmcalCorrectionComponent(const char * name);
43 
44  // Virtual functions to be overloaded
45  virtual Bool_t Initialize();
46  virtual void ExecOnce();
47  virtual Bool_t Run();
48  virtual Bool_t UserNotify();
49 
50  void GetEtaPhiDiff(const AliVTrack *t, const AliVCluster *v, Double_t &phidiff, Double_t &etadiff);
51  void UpdateCells();
53  void GetPass();
54  void FillCellQA(TH1F* h);
55 
58  AliVCaloCells * GetCaloCells() { return fCaloCells; }
59  TList * GetOutputList() { return fOutput; }
60 
63  void SetCaloCells(AliVCaloCells * cells) { fCaloCells = cells; }
64  void SetRecoUtils(AliEMCALRecoUtils *ru) { fRecoUtils = ru; }
65 
66  void SetEvent(AliVEvent * event) { fEvent = event; }
67  void SetMCEvent(AliMCEvent * mcevent) { fMCEvent = mcevent; }
68 
69  void SetEMCALGeometry(AliEMCALGeometry * geometry ) { fGeom = geometry; }
70  void SetCentralityBin(Int_t bin) { fCentBin = bin; }
71  void SetCentrality(Double_t cent) { fCent = cent; }
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> void GetProperty(std::string propertyName, T & property, bool requiredProperty = true, std::string correctionName = "");
81 
83  template<typename T> static void 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  // Determine branch name using the "usedefault" pattern
88  static std::string DetermineUseDefaultName(inputObjectType contType, Bool_t esdMode);
89 
90  protected:
91 
92  void AddContainer(inputObjectType type);
93 
94 #if !(defined(__CINT__) || defined(__MAKECINT__))
95  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);
96  template<typename T> static bool GetPropertyFromNode(const YAML::Node & node, std::string propertyName, T & property);
97 
98  YAML::Node fUserConfiguration;
100 #endif
101 
102  Bool_t fCreateHisto; // flag to make some basic histograms
104  TString fFilepass; // input data pass number
105  Bool_t fGetPassFromFileName; // get fFilepass from file name
106  AliVEvent *fEvent; // pointer to event
107  Bool_t fEsdMode; // flag for ESD
108  AliMCEvent *fMCEvent; // MC
116  AliEMCALGeometry *fGeom;
121  AliVCaloCells *fCaloCells;
122  AliEMCALRecoUtils *fRecoUtils;
124 
125  TString fBasePath; // base folder path to get root files
126 
127  private:
128  AliEmcalCorrectionComponent(const AliEmcalCorrectionComponent &); // Not implemented
130 
131  ClassDef(AliEmcalCorrectionComponent, 1) // EMCal correction component
132 };
133 
134 #if !(defined(__CINT__) || defined(__MAKECINT__))
135 
139 template<typename T>
140 void 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  GetProperty(propertyName, property, fUserConfiguration, fDefaultConfiguration, requiredProperty, correctionName);
148 }
149 
154 template<typename T>
155 void AliEmcalCorrectionComponent::GetProperty(std::string propertyName, T & property, const YAML::Node & userConfiguration, const YAML::Node & defaultConfiguration, bool requiredProperty, std::string correctionName)
156 {
157  // Remove AliEmcalCorrection if in name
158  std::size_t prefixStringLocation = correctionName.find("AliEmcalCorrection");
159  if (prefixStringLocation != std::string::npos)
160  {
161  // AliEmcalCorrection is 18 characters
162  correctionName.erase(prefixStringLocation, prefixStringLocation + 18);
163  }
164 
165  bool setProperty = false;
166  // IsNull checks is a node is empty. A node is empty if it is created.
167  // IsDefined checks if the node that was requested was not actually created.
168  // In this case, the user configuration node is always created. If it IsNull, then we ignore it.
169  if (userConfiguration.IsNull() != true)
170  {
171  AliDebugClass(2, "Looking for parameter in user configuration");
172  YAML::Node sharedParameters = userConfiguration["sharedParameters"];
173  //std::cout << std::endl << "User Node: " << fUserConfiguration << std::endl;
174  setProperty = AliEmcalCorrectionComponent::GetPropertyFromNodes(userConfiguration, sharedParameters, propertyName, property, correctionName, "user");
175  }
176 
177  if (setProperty != true)
178  {
179  AliDebugClass(2, "Looking for parameter in defulat configuration");
180  YAML::Node sharedParameters = defaultConfiguration["sharedParameters"];
181  //std::cout << std::endl << "Default Node: " << fDefaultConfiguration << std::endl;
182  setProperty = AliEmcalCorrectionComponent::GetPropertyFromNodes(defaultConfiguration, sharedParameters, propertyName, property, correctionName, "default");
183  }
184 
185  if (setProperty != true && requiredProperty == true)
186  {
187  std::stringstream message;
188  message << "Failed to retrieve property \""
189  << (correctionName != "" ? correctionName + ":" : "")
190  << propertyName << "\" from default config!" << std::endl;
191  AliFatalClass(message.str().c_str());
192  }
193 }
194 
199 template<typename T>
200 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)
201 {
202  // Used as a buffer for printing complicated messages
203  std::stringstream tempMessage;
204 
205  bool returnValue = false;
206  if (nodesDeep > 2)
207  {
208  // Ensure that we do not go past two levels
209  tempMessage.str("");
210  tempMessage << "Went too many levels of recursion. Bailing out on \"" << correctionName << ":" << propertyName << "\" from " << configurationType << " config at level " << nodesDeep;
211  AliDebugClass(2, TString::Format("%s", tempMessage.str().c_str()));
212  return false;
213  }
214 
215  // Only want to print once to ensure the user is not overwhelmed!
216  if (nodesDeep == 0)
217  {
218  tempMessage.str("");
219  tempMessage << "Retreiving property \""
220  << (correctionName != "" ? correctionName + ":" : "")
221  << propertyName << "\" from " << configurationType << " config at level " << nodesDeep;
222  AliDebugClass(1, TString::Format("%s", tempMessage.str().c_str()));
223  }
224 
225  if (node.IsDefined() == true)
226  {
227  //std::cout << "Node: " << node << std::endl;
228  if (node[propertyName])
229  {
230  bool isShared = false;
231  // Will contain the name of the property in the sharedParameters section that should be retrieved
232  // if it is requested through the user configuration.
233  std::string sharedValueName = "";
234  // Necessary because it fails on vectors and other complex objects that are YAML sequences.
235  if (std::is_arithmetic<T>::value || std::is_same<T, std::string>::value)
236  {
237  // Retrieve value as string to check for shared value
238  sharedValueName = node[propertyName].as<std::string>();
239  // Check for a shared value
240  isShared = AliEmcalCorrectionComponent::IsSharedValue(sharedValueName);
241  }
242 
243  tempMessage.str("");
244  tempMessage << "property \""
245  << (nodesDeep > 0 ? correctionName + ":" : "")
246  << propertyName
247  << "\" using " << ( isShared ? "\"sharedParameters:" + sharedValueName + "\" in " : "" )
248  << "values from the " << configurationType
249  << " configuration at level " << nodesDeep;
250 
251  AliDebugClass(2, TString::Format("Retrieveing %s", tempMessage.str().c_str()));
252 
253  bool retrievalResult = false;
254  if (isShared == true)
255  {
256  retrievalResult = GetPropertyFromNode(sharedParametersNode, sharedValueName, property);
257  }
258  else
259  {
260  retrievalResult = GetPropertyFromNode(node, propertyName, property);
261  }
262 
263  // Inform about the result
264  if (retrievalResult == true)
265  {
266  AliDebugClass(2, TString::Format("Succeeded in retrieveing %s", tempMessage.str().c_str()));
267  returnValue = true;
268  }
269  else
270  {
271  // Only fatal if we have exhausted our last option, the default
272  if (configurationType == "default")
273  {
274  AliFatalClass(TString::Format("Failed to retrieve %s", tempMessage.str().c_str()));
275  }
276  else
277  {
278  AliDebugClass(2, TString::Format("Failed to retrieve %s", tempMessage.str().c_str()));
279  }
280  returnValue = false;
281  }
282  }
283  else
284  {
285  // Go one node deeper using recursion
286  // Must create a new node, since we took the original node by reference
287  YAML::Node deeperNode = node[correctionName];
288  nodesDeep++;
289  AliDebugClass(2, TString::Format("Going a node deeper with \"%s\" to level %i", correctionName.c_str(), nodesDeep));
290  returnValue = GetPropertyFromNodes(deeperNode, sharedParametersNode, propertyName, property, correctionName, configurationType, nodesDeep);
291 
292  // If we didn't find it, next try a substring
293  if (returnValue == false)
294  {
295  // Don't increment nodesDeep, because we are about to go another node deep anyway
296  std::size_t splitLocation = correctionName.find("_");
297  // We only want to look at the split if we are only one node deep and
298  // if the split is actually meaningful
299  if (splitLocation != std::string::npos && nodesDeep == 1)
300  {
301  // Split from start to underscore
302  std::string subCorrectionName = correctionName.substr(0, splitLocation);
303  AliDebugClass(2, TString::Format("Attempting to retrieve property \"%s\" from base correction \"%s\" at level %i", propertyName.c_str(), subCorrectionName.c_str(), nodesDeep));
304  // Retrieve the base correction node
305  // Need to create new node! Otherwise it will assign the correctionName node to subCorrectionName node!
306  YAML::Node subCorrectionNode = node[subCorrectionName];
307  returnValue = GetPropertyFromNodes(subCorrectionNode, sharedParametersNode, propertyName, property, subCorrectionName, configurationType, nodesDeep);
308  }
309  }
310  }
311  }
312  else
313  {
314  tempMessage.str("");
315  tempMessage << "Node is undefined for \""
316  << (correctionName != "" ? correctionName + ":" : "")
317  << propertyName << "\" from " << configurationType << " config at level " << nodesDeep;
318  AliDebugClass(2, TString::Format("%s", tempMessage.str().c_str()));
319 
320  returnValue = false;
321  }
322 
323  return returnValue;
324 }
325 
330 template<typename T>
331 bool AliEmcalCorrectionComponent::GetPropertyFromNode(const YAML::Node & node, std::string propertyName, T & property)
332 {
333  if (node[propertyName])
334  {
335  property = node[propertyName].as<T>();
336  return true;
337  }
338  return false;
339 }
340 
341 #endif /* Hide yaml from CINT */
342 
343 // Below is based on: https://stackoverflow.com/a/582456 , and editted for our purposes.
344 //
345 // Template for creating a new component. Used to register the component.
346 template<typename T> AliEmcalCorrectionComponent * createT() { return new T; }
347 
348 // Factory to create and keep track of new components
350 {
351  public:
353 
354  typedef std::map<std::string, AliEmcalCorrectionComponent*(*)()> map_type;
355 
356  // Creates an instance of an object based on the name if the name is registered in the map.
357  static AliEmcalCorrectionComponent * createInstance(std::string const& s)
358  {
359  map_type::iterator it = getMap()->find(s);
360  if(it == getMap()->end())
361  return 0;
362  // Initializes the function with empty arguments (?)
363  return it->second();
364  }
365 
366  protected:
367  // Creates and access the component map
368  static map_type * getMap() {
369  // We never delete the map (until program termination) because we cannot guarantee
370  // correct destruction order
371  if(!componentMap) { componentMap = new map_type; }
372  return componentMap;
373  }
374 
375  private:
376  // Contains the map to all of the components
378 };
379 
380 // Allows registration of new components into the factory map.
381 template<typename T>
383 {
384  public:
385  RegisterCorrectionComponent(std::string const& s)
386  {
387  getMap()->insert(std::make_pair(s, &createT<T>));
388  }
389 };
390 
391 #endif /* ALIEMCALCORRECTIONCOMPONENT_H */
Int_t fNcentBins
how many centrality bins (this member copied from AliAnalysisTaskEmcal)
void AddContainer(inputObjectType type)
static std::string DetermineUseDefaultName(inputObjectType contType, Bool_t esdMode)
AliParticleContainer * GetParticleContainer()
AliEMCALGeometry * fGeom
!geometry object
double Double_t
Definition: External.C:58
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
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
void GetProperty(std::string propertyName, T &property, bool requiredProperty=true, std::string correctionName="")
Retrieve property.
std::map< std::string, AliEmcalCorrectionComponent *(*)()> map_type
Double_t fMaxBinPt
max pt in histograms
TList * fOutput
! list of output histograms
AliEmcalCorrectionComponent & operator=(const AliEmcalCorrectionComponent &)
static bool IsSharedValue(std::string &value)
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.
Bool_t fIsEmbedded
trigger, embedded signal