]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGGA/PHOSTasks/ClusterSelection/AliPHOSClusterSelectionLogbackTask.cxx
Merge branch 'feature-movesplit'
[u/mrichter/AliRoot.git] / PWGGA / PHOSTasks / ClusterSelection / AliPHOSClusterSelectionLogbackTask.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 #include "TBits.h"
17 #include "TObjArray.h"
18 #include "TObject.h"
19 #include "TMap.h"
20
21 #include "AliVCluster.h"
22 #include "AliPHOSGeometry.h"
23
24 #include "AliPHOSClusterSelection.h"
25 #include "AliPHOSEventSelection.h"
26 #include "AliPHOSLogbackCluster.h"
27
28 // Analysis task to fill histograms with PHOS ESD or AOD clusters and cells
29 // Authors : Henrik Qvigstad
30 // Date    : 28.05.2011
31 /* $Id$ */
32
33 #include "AliPHOSClusterSelectionLogbackTask.h"
34 ClassImp(AliPHOSClusterSelectionLogbackTask);
35
36
37 AliPHOSClusterSelectionLogbackTask::AliPHOSClusterSelectionLogbackTask(const char* name)
38   : AliPHOSClusterSelectionTask(name),
39     fMapOfEventLists(0x0)
40 {
41   fMapOfEventLists = new TMap;
42   fMapOfEventLists->SetOwnerValue();
43 }
44
45 AliPHOSClusterSelectionLogbackTask::~AliPHOSClusterSelectionLogbackTask()
46 {
47   delete fMapOfEventLists;
48 }
49   
50 void AliPHOSClusterSelectionLogbackTask::UserCreateOutputObjects()
51 {
52   return;
53 }
54
55 void AliPHOSClusterSelectionLogbackTask::UserExec(Option_t *option)
56 {
57   AliPHOSClusterSelectionTask::UserExec(option);
58
59   // initialize fMapOfEventLists
60   if( !fMapOfEventLists ) {
61     fMapOfEventLists = new TMap;
62     fMapOfEventLists->SetOwnerValue();
63   }
64 }
65
66 TObjArray* AliPHOSClusterSelectionLogbackTask::GetPHOSClustersLogback(const AliPHOSEventSelection* eventSelection, UInt_t eventLogbackIndex, const AliPHOSClusterSelection* clusterSelection) const
67 {
68   // Gives an array of clusters for an given event and cluster selection, 
69   // provided that such an selection has been logged.
70   // eventLogbackIndex - How many logged events to go back in time, must be an positive integer 0,1,2...
71   // , an clusterSelection is optional.
72
73   TObject* objEventList = fMapOfEventLists->GetValue(eventSelection);
74   TList* eventList = dynamic_cast<TList*> ( objEventList );
75   if(!eventList)
76     return 0x0;
77   
78   // eventList is a list of eventArray
79   TObject* objEventArray = eventList->At(eventLogbackIndex);
80   TObjArray* eventArray = dynamic_cast<TObjArray*> ( objEventArray );
81   if( !eventArray )
82     return 0x0;
83   
84   // fMapOfEventLists[eventSelection][eventLogbackIndex][0]
85   TObject* objEventClusters = eventArray->At(kCluArray);
86   TObjArray* eventClusters = dynamic_cast<TObjArray*> ( objEventClusters );
87   if( !eventClusters )
88     AliFatal(Form("eventArray should always contain and TObjArray in index %i", kCluArray));
89   
90   // fMapOfEventLists[eventSelection][eventLogbackIndex][1]
91   TObject* objSelectionMap = eventArray->At(kSelMap);
92   TMap* selectionMap = dynamic_cast<TMap*> ( objSelectionMap );
93   if( !selectionMap )
94     AliFatal(Form("eventArray should always contain and TMap in index %i", kSelMap));
95
96   
97   // For the given eventSelection and eventBacklog, we should now have 
98   // eventClusters and selectionMap !!!
99   // which TObjArray we return to the user depends on if an clusterSelection is specified
100   if(clusterSelection) {
101     TObject* objSelectionArray = selectionMap->GetValue(clusterSelection);
102     TObjArray* selectionArray = dynamic_cast<TObjArray*> ( objSelectionArray );
103     return selectionArray;
104   } 
105   else
106     return eventClusters;
107 }
108
109
110 void AliPHOSClusterSelectionLogbackTask::LogEvent(const AliPHOSEventSelection* eventSelection, UInt_t nEventsToLog)
111 {
112   if( nEventsToLog < 1) {
113     AliError("nEventsToLog needs to be >0, or logging does not make sense");
114     return;
115   }
116
117   // Make a copy of the cluster array
118   TObjArray* newCluArray = new TObjArray(fClusters->GetEntriesFast());
119   newCluArray->SetOwner();
120   for(int iclu=0; iclu < fClusters->GetSize(); ++iclu) {
121     AliPHOSLogbackCluster* clu = new AliPHOSLogbackCluster((AliVCluster*)fClusters->At(iclu));
122     newCluArray->Add(clu);
123   }
124   
125   // Make a copy of the selection map
126   // and referance to the new array of clusters
127   TMap* newMap = new TMap;
128   newMap->SetOwnerValue();
129   TMapIter* iter = (TMapIter*) fSelectionMap->MakeIterator();
130   TObject* key = 0x0;
131   while((key = iter->Next())){ // Loop over Cluster Selections 
132     TRefArray* oldSelArray = dynamic_cast<TRefArray*> (fSelectionMap->GetValue(key));
133     TObjArray* newSelArray = new TObjArray(oldSelArray->GetEntriesFast());
134     newSelArray->SetOwner(false);
135     // Referance the new selection array to the new clusters
136     for(int selInd=0; selInd<oldSelArray->GetEntriesFast(); ++selInd){
137       bool matched = false; // false untill mached
138       for(int cluInd=0; cluInd<fClusters->GetEntriesFast(); ++cluInd){
139         TObject* oldSelCluster = oldSelArray->At(selInd);
140         TObject* oldCluster = fClusters->At(cluInd);
141         if(oldSelCluster == oldCluster) { // if old selection matches old cluster,
142           // then new selection should match cluster in the same index as old cluster
143           // at the same old selection index
144           if( newSelArray->At(cluInd) )
145             AliError("should be empty!");
146           newSelArray->AddAt(newCluArray->At(cluInd), selInd);
147
148           matched = true;
149           break;
150         }
151       }
152       if( ! matched )
153         AliError("Should have found a match!");
154     }
155     
156     if( newSelArray->GetEntriesFast() != newSelArray->GetEntries()
157         || newSelArray->GetEntriesFast() != oldSelArray->GetEntriesFast() )
158       AliError("Entries should match!");
159     
160     newMap->Add(key, newSelArray);
161   }// endo of creation of new selMap.
162
163   //make an eventArray to hold clusters and maps
164   TObjArray* eventArray = new TObjArray(kSize);
165   eventArray->SetOwner();
166   eventArray->AddAt(newCluArray, kCluArray);
167   eventArray->AddAt(newMap, kSelMap);
168  
169   // at to list of events
170   TObject* objEventList = fMapOfEventLists->GetValue(eventSelection);
171   TList* eventList = dynamic_cast<TList*> ( objEventList );
172   if(!eventList) {
173     eventList = new TList;
174     eventList->SetOwner();
175   }
176   eventList->AddFirst(eventArray);
177   
178   
179   // remove old events
180   while( eventList->At(nEventsToLog) && nEventsToLog )
181     eventList->RemoveLast();
182 }
183
184
185
186 AliPHOSClusterSelectionLogbackTask* GetTask(const char* name)
187 {
188   AliPHOSClusterSelectionLogbackTask* task = dynamic_cast<AliPHOSClusterSelectionLogbackTask*>( AliPHOSClusterSelectionTask::GetTask(name) );
189   if( !task )
190     Printf( Form("No AliPHOSClusterSelectionLogbackTask with name: %s", name) );
191
192   return task;
193 }