AliPhysics  ba8894a (ba8894a)
AliEmcalCopyCollection.cxx
Go to the documentation of this file.
1 
3 
4 #include <string>
5 
6 #include <TString.h>
7 #include <TClonesArray.h>
8 
9 #include "AliVEvent.h"
10 #include "AliAnalysisManager.h"
11 #include "AliVEventHandler.h"
12 #include "AliVCaloCells.h"
13 #include "AliESDCaloCells.h"
14 #include "AliAODCaloCells.h"
15 #include "AliAODCaloCluster.h"
16 #include "AliESDCaloCluster.h"
17 #include "AliAODTrack.h"
18 #include "AliESDtrack.h"
19 
21 ClassImp(AliEmcalCopyCollection);
23 
25  AliAnalysisTaskSE("AliEmcalCopyCollection"),
26  fInputObjectType(AliEmcalContainerUtils::kNoDefinedInputObject),
27  fCollectionToCopyName(""),
28  fNewCollectionName(""),
29  fIsEmbedding(false),
30  fEventInitialized(false),
31  fIsEsd(false),
32  fEvent(0)
33 {
34 }
35 
36 AliEmcalCopyCollection::AliEmcalCopyCollection(std::string name, AliEmcalContainerUtils::InputObject_t inputObjectType, std::string collectionToCopyName, std::string newCollectionName, bool isEmbedding):
37  AliAnalysisTaskSE(name.c_str()),
38  fInputObjectType(inputObjectType),
39  fCollectionToCopyName(collectionToCopyName),
40  fNewCollectionName(newCollectionName),
41  fIsEmbedding(isEmbedding),
42  fEventInitialized(false),
43  fIsEsd(false),
44  fEvent(0)
45 {
46 }
47 
52 {
53  // Determine event type from AnalysisManager
54  AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
55  if (mgr) {
56  AliVEventHandler *evhand = mgr->GetInputEventHandler();
57  if (evhand) {
58  if (evhand->InheritsFrom("AliESDInputHandler")) {
59  fIsEsd = true;
60  }
61  else {
62  fIsEsd = false;
63  }
64  }
65  else {
66  AliError("Event handler not found!");
67  }
68  }
69  else {
70  AliError("Analysis manager not found!");
71  }
72 }
73 
79 {
80  // Initialize the event if not intialized
81  if (!fEventInitialized)
83 
84  // Only continue if we are initialized successfully
85  if (!fEventInitialized)
86  return;
87 
88  // Copy cells and cluster to new branches if desired
90 }
91 
97 {
99  if (!fEvent) {
100  AliError("Could not retrieve event! Returning!");
101  return;
102  }
103 
104  // Check that the input or output objects are not empty
105  if (fCollectionToCopyName == "") {
106  AliFatal("Collection to copy name is empty! Please set a valid name!");
107  }
108 
109  if (fNewCollectionName == "") {
110  AliFatal("New collection name is empty! Please set a valid name!");
111  }
112 
113  // Determine use default name if applicable
114  if (fCollectionToCopyName == "usedefault") {
116  }
117  if (fNewCollectionName == "usedefault") {
118  // This name is almost certainly going to conflict, but checked to be certain
120  }
121 
125  AliFatal(TString::Format("Unrecognized input object type %d", fInputObjectType));
126  }
127 
128  // Create new branch
129  NewBranch();
130 
131  fEventInitialized = true;
132 }
133 
142 {
143  // Check to ensure that we are not trying to create a new branch on top of the old branch
144  TObject * existingObject = fEvent->FindListObject(fNewCollectionName.c_str());
145  if (existingObject) {
146  AliFatal(TString::Format("Attempted to create a new %d branch, \"%s\", with the same name as an existing branch! Check your configuration! Perhaps \"usedefault\" was used incorrectly?", fInputObjectType, fNewCollectionName.c_str()));
147  }
148  //TClonesArray * existingArray = dynamic_cast<TClonesArray *>(existingObject);
149 
151  {
152  // Cells
153  // Create new cells branch and add it to the event
154  AliVCaloCells * newCells = 0;
155  if (fIsEsd) {
156  newCells = new AliESDCaloCells(fNewCollectionName.c_str(), fNewCollectionName.c_str(), AliVCaloCells::kEMCALCell);
157  }
158  else {
159  newCells = new AliAODCaloCells(fNewCollectionName.c_str(), fNewCollectionName.c_str(), AliVCaloCells::kEMCALCell);
160  }
161 
162  // Add to event
163  fEvent->AddObject(newCells);
164  }
166  // Clusters and tracks
167 
168  // Determine relevant values to able to properly create the branch
169  // Branch object type name (the type which is going to be created in the TClonesArray)
170  std::string newObjectTypeName = AliEmcalContainerUtils::DetermineUseDefaultName(fInputObjectType, fIsEsd, true);
171 
172  // Create new branch and add it to the event
173  TClonesArray * newArray = new TClonesArray(newObjectTypeName.c_str());
174  newArray->SetName(fNewCollectionName.c_str());
175  fEvent->AddObject(newArray);
176  }
177  else {
178  // Shouldn't ever get here, but included for completeness
179  AliFatal(TString::Format("Unrecognized input object type %d", fInputObjectType));
180  }
181 
182 }
183 
190 {
191  // Cells
193  {
194  AliVCaloCells * cellsToCopy = dynamic_cast<AliVCaloCells *>(fEvent->FindListObject(fCollectionToCopyName.c_str()));
195  AliVCaloCells * newCells = dynamic_cast<AliVCaloCells *>(fEvent->FindListObject(fNewCollectionName.c_str()));
196  AliDebug(3, Form("Number of old cells: %d", cellsToCopy->GetNumberOfCells()));
197  // Copies from cellsToCopy to newCells!
198  cellsToCopy->Copy(*newCells);
199  // The name was changed to the currentCells name, but we want it to be newCells, so we restore it
200  newCells->SetName(fNewCollectionName.c_str());
201  newCells->SetTitle(fNewCollectionName.c_str());
202 
203  AliDebug(3, Form("Number of old cells: %d \tNumber of new cells: %d", cellsToCopy->GetNumberOfCells(), newCells->GetNumberOfCells() ));
204  if (cellsToCopy->GetNumberOfCells() != newCells->GetNumberOfCells()) {
205  AliFatal(Form("Number of old cell: %d != number of new cells %d", cellsToCopy->GetNumberOfCells(), newCells->GetNumberOfCells()));
206  }
207  }
208 
209  // Clusters
211  {
212  TClonesArray * newClusters = dynamic_cast<TClonesArray *>(InputEvent()->FindListObject(fNewCollectionName.c_str()));
213  TClonesArray * currentClusters = dynamic_cast<TClonesArray *>(InputEvent()->FindListObject(fCollectionToCopyName.c_str()));
214  AliDebug(3, Form("before copy:\t currentClusters->GetEntries(): %d \t newClusters->GetEntries(): %d", currentClusters->GetEntries(), newClusters->GetEntries()));
215  CopyClusters(currentClusters, newClusters);
216  AliDebug(3, Form("after copy:\t currentClusters->GetEntries(): %d \t newClusters->GetEntries(): %d", currentClusters->GetEntries(), newClusters->GetEntries()));
217  }
218 
219  // Tracks
221  {
222  TClonesArray * newTracks = dynamic_cast<TClonesArray *>(InputEvent()->FindListObject(fNewCollectionName.c_str()));
223  TClonesArray * currentTracks = dynamic_cast<TClonesArray *>(InputEvent()->FindListObject(fCollectionToCopyName.c_str()));
224  AliDebug(3, Form("before copy:\t currentTracks->GetEntries(): %d \t newTracks->GetEntries(): %d", currentTracks->GetEntries(), newTracks->GetEntries()));
225  for (Int_t i = 0; i < currentTracks->GetEntriesFast(); i++)
226  {
227  if (fIsEsd)
228  {
229  AliESDtrack *currentTrack = dynamic_cast<AliESDtrack *>(currentTracks->At(i));
230  // Calls copy constructor to create a new track at position i in newTracks
231  AliESDtrack *newTrack = dynamic_cast<AliESDtrack *>(new ((*newTracks)[i]) AliESDtrack(*currentTrack));
232 
233  // Reset properties of the track to fix TRefArray errors later.
234  // Particular combination is from AODTrackFilterTask
235  // Resetting the TProcessID of the track is not sufficient!
236  newTrack->SetUniqueID(0);
237  newTrack->ResetBit(TObject::kHasUUID);
238  newTrack->ResetBit(TObject::kIsReferenced);
239  }
240  else
241  {
242  AliAODTrack *currentTrack = dynamic_cast<AliAODTrack *>(currentTracks->At(i));
243  // Calls copy constructor to create a new track at position i in newTracks
244  AliAODTrack *newTrack = dynamic_cast<AliAODTrack *>(new ((*newTracks)[i]) AliAODTrack(*currentTrack));
245 
246  // Reset properties of the track to fix TRefArray errors later.
247  // Particular combination is from AODTrackFilterTask
248  // Resetting the TProcessID of the track is not sufficient!
249  newTrack->SetUniqueID(0);
250  newTrack->ResetBit(TObject::kHasUUID);
251  newTrack->ResetBit(TObject::kIsReferenced);
252  }
253  }
254  AliDebug(3, Form("after copy:\t currentTracks->GetEntries(): %d \t newTracks->GetEntries(): %d", currentTracks->GetEntries(), newTracks->GetEntries()));
255  }
256 }
257 
264 void AliEmcalCopyCollection::CopyClusters(TClonesArray *orig, TClonesArray *dest)
265 {
266  const Int_t Ncls = orig->GetEntries();
267 
268  for(Int_t i=0; i < Ncls; ++i) {
269  AliVCluster *oc = static_cast<AliVCluster*>(orig->At(i));
270 
271  if (!oc)
272  continue;
273 
274  // We likely want to include PHOS cells as well, so the copy we make should avoid this check
275  //if (!oc->IsEMCAL())
276  // continue;
277 
278  AliVCluster *dc = static_cast<AliVCluster*>(dest->New(i));
279  //dc->SetType(AliVCluster::kEMCALClusterv1);
280  dc->SetType(oc->GetType());
281  dc->SetE(oc->E());
282  Float_t pos[3] = {0};
283  oc->GetPosition(pos);
284  dc->SetPosition(pos);
285  dc->SetNCells(oc->GetNCells());
286  dc->SetCellsAbsId(oc->GetCellsAbsId());
287  dc->SetCellsAmplitudeFraction(oc->GetCellsAmplitudeFraction());
288  dc->SetID(oc->GetID());
289  dc->SetDispersion(oc->GetDispersion());
290  dc->SetEmcCpvDistance(-1);
291  dc->SetChi2(-1);
292  dc->SetTOF(oc->GetTOF()); //time-of-flight
293  dc->SetNExMax(oc->GetNExMax()); //number of local maxima
294  dc->SetM02(oc->GetM02());
295  dc->SetM20(oc->GetM20());
296  dc->SetDistanceToBadChannel(oc->GetDistanceToBadChannel());
297  dc->SetMCEnergyFraction(oc->GetMCEnergyFraction());
298 
299  //MC
300  UInt_t nlabels = oc->GetNLabels();
301  Int_t *labels = oc->GetLabels();
302 
303  if (nlabels == 0 || !labels)
304  continue;
305 
306  AliESDCaloCluster *esdClus = dynamic_cast<AliESDCaloCluster*>(dc);
307  if (esdClus) {
308  TArrayI parents(nlabels, labels);
309  esdClus->AddLabels(parents);
310  }
311  else {
312  AliAODCaloCluster *aodClus = dynamic_cast<AliAODCaloCluster*>(dc);
313  if (aodClus)
314  aodClus->SetLabel(labels, nlabels);
315  }
316  }
317 }
318 
320  const char * collectionToCopyName,
321  const char * newCollectionName,
322  bool isEmbedding)
323 
324 {
325  // Get the pointer to the existing analysis manager via the static access method.
326  //==============================================================================
327  AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
328  if (!mgr)
329  {
330  ::Error("AddTaskEmcalCopyCollection", "No analysis manager to connect to.");
331  return 0;
332  }
333 
334  // Check the analysis type using the event handlers connected to the analysis manager.
335  //==============================================================================
336  AliVEventHandler* handler = mgr->GetInputEventHandler();
337  if (!handler)
338  {
339  ::Error("AddTaskEmcalCopyCollection", "This task requires an input event handler");
340  return 0;
341  }
342 
343  TString name = "AliEmcalCopyCollection";
344  name += TString::Format("_%s_%s", collectionToCopyName, newCollectionName);
345  if (isEmbedding) {
346  name += "_Embedded";
347  }
348 
349  AliEmcalCopyCollection* mgrTask = static_cast<AliEmcalCopyCollection *>(mgr->GetTask(name.Data()));
350  if (mgrTask) return mgrTask;
351 
352  // Create the task that manages copying collections
353  AliEmcalCopyCollection* copyCollection = new AliEmcalCopyCollection(name.Data(),
354  inputObjectType,
355  collectionToCopyName,
356  newCollectionName,
357  isEmbedding);
358 
359  //-------------------------------------------------------
360  // Final settings, pass to manager and set the containers
361  //-------------------------------------------------------
362 
363  mgr->AddTask(copyCollection);
364 
365  // Create containers for input
366  AliAnalysisDataContainer* cInput = mgr->GetCommonInputContainer();
367  mgr->ConnectInput(copyCollection, 0, cInput);
368 
369  return copyCollection;
370 }
static const AliVEvent * GetEvent(const AliVEvent *inputEvent, bool isEmbedding=false)
void UserExec(Option_t *option)
Copies cell, cluster, or track collections for use in the EMCal framework.Often, it is necessary to c...
AliVEvent * fEvent
! Points to the event to copy from and store the new bracnh in
Helper functions related to AliEmcalContainer derived objects.
bool fIsEmbedding
Denotes whether the collection is embedded (and therefore should be copied and stored in the external...
int Int_t
Definition: External.C:63
InputObject_t
Type of input object to be created
unsigned int UInt_t
Definition: External.C:33
float Float_t
Definition: External.C:68
AliEmcalContainerUtils::InputObject_t fInputObjectType
Type of collection to copy.
void CopyClusters(TClonesArray *orig, TClonesArray *dest)
static AliEmcalCopyCollection * AddTaskEmcalCopyCollection(AliEmcalContainerUtils::InputObject_t inputObjectType=AliEmcalContainerUtils::kNoDefinedInputObject, const char *collectionToCopyName="", const char *newCollectionName="", bool isEmbedding=false)
bool fIsEsd
! Denotes whether the input is ESD
static std::string DetermineUseDefaultName(InputObject_t objType)
const char Option_t
Definition: External.C:48
std::string fNewCollectionName
Name of the collection bracnh where it will be copied.
bool fEventInitialized
! Denotes whether the event is initialized
std::string fCollectionToCopyName
Name of the collection branch to copy.