]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONDigitStoreV1.cxx
Legacy implementation of AliMUONVDigitStore (Laurent)
[u/mrichter/AliRoot.git] / MUON / AliMUONDigitStoreV1.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 // $Id$
17
18 #include "AliMUONDigitStoreV1.h"
19
20 /// \class AliMUONDigitStoreV1
21 ///
22 /// (Legacy) Implementation of AliMUONVDigitStore. 
23 /// Called legacy as the internal structure corresponds to what we
24 /// used to write as MUON.(S)Digits.root files, before the switch 
25 /// to data stores.
26 ///
27 // \author Laurent Aphecetche, Subatech
28 ///
29
30 #include "AliLog.h"
31 #include "AliMUONDigit.h"
32 #include "AliMUONDigitStoreV1Iterator.h"
33 #include "AliMUONTOTCAStoreIterator.h"
34 #include "AliMUONTreeManager.h"
35 #include "AliMpConstants.h"
36 #include "AliMpDEManager.h"
37 #include "AliMpDEManager.h"
38 #include "AliMpStationType.h"
39 #include <Riostream.h>
40 #include <TClonesArray.h>
41 #include <TObjArray.h>
42 #include <TTree.h>
43
44 /// \cond CLASSIMP
45 ClassImp(AliMUONDigitStoreV1)
46 /// \endcond
47
48 namespace
49 {
50   TString BaseName(const TString& name)
51   {
52     if ( name == "TreeS" ) return "MUONSDigit";
53     if ( name == "TreeD" ) return "MUONDigit";
54     return "";
55   }
56 }
57
58 //_____________________________________________________________________________
59 AliMUONDigitStoreV1::AliMUONDigitStoreV1()
60 : AliMUONVDigitStore(), 
61 fDigits(new TObjArray(AliMpConstants::NofChambers())),
62 fChamberDigits(0x0)
63 {
64   /// ctor
65   fDigits->SetOwner(kTRUE);
66   for ( Int_t i = 0; i < fDigits->GetSize(); ++i )
67   {
68     TClonesArray* tca = new TClonesArray("AliMUONDigit",100);
69     tca->SetOwner(kTRUE);
70     fDigits->AddAt(tca,i);
71   }
72   Clear();
73   AliDebug(1,"");
74 }
75
76 //_____________________________________________________________________________
77 AliMUONDigitStoreV1::AliMUONDigitStoreV1(const AliMUONDigitStoreV1&)
78 : AliMUONVDigitStore(), 
79 fDigits(0x0),
80 fChamberDigits(0x0)
81 {
82   /// copy ctor
83   AliError("Please implement me");
84 }
85
86 //_____________________________________________________________________________
87 AliMUONDigitStoreV1& 
88 AliMUONDigitStoreV1::operator=(const AliMUONDigitStoreV1&)
89 {
90   /// assignement operator
91   AliError("Please implement me");
92   return *this;
93 }
94
95 //_____________________________________________________________________________
96 AliMUONDigitStoreV1::~AliMUONDigitStoreV1()
97 {
98   /// dtor
99   delete fDigits;
100 }
101
102
103 //_____________________________________________________________________________
104 void 
105 AliMUONDigitStoreV1::Clear(Option_t* /*opt*/)
106 {
107   /// Clear the tclonesarray, but keep the tobjarray's size constant.
108
109   for ( Int_t i = 0; i <= fDigits->GetLast(); ++i ) 
110   {
111     ChamberDigits(i)->Clear("C");
112   }  
113 }
114
115 //_____________________________________________________________________________
116 AliMUONVDigit* 
117 AliMUONDigitStoreV1::Add(const AliMUONVDigit& vdigit, EReplacePolicy replace)
118 {
119   /// Try to add a digit to the store. Return whether the try was successfull
120   /// or not.
121   /// 
122   /// If the digit is already there, the action taken depends on "replace"
123   /// kAllow -> replacement will occur (i.e. return kTRUE)
124   /// kDeny -> replacement will *not* occur (and returned value is kFALSE)
125   /// kMerge -> both digits will be merged into one (return kTRUE)
126   ///
127   
128   const AliMUONDigit* digit = dynamic_cast<const AliMUONDigit*>(&vdigit);
129   
130   if (!digit)
131   {
132     AliError(Form("Digit is not of the expected type (%s vs AliMUONdigit)",
133                   vdigit.ClassName()));
134     return 0x0;
135   }
136   
137   Int_t index(-1);
138   
139   if ( replace != kIgnore ) 
140   {
141     AliMUONVDigit* alreadyThere = Find(*digit,index);
142   
143     if ( alreadyThere ) 
144     {
145       if ( replace == kDeny ) 
146       {
147         return 0x0;
148       }
149       if ( replace == kMerge ) 
150       {
151         alreadyThere->MergeWith(*digit);
152         return alreadyThere;
153       }
154     }
155   }
156   
157   Int_t detElemId = digit->DetElemId();
158   Int_t iChamber = AliMpDEManager::GetChamberId(detElemId);
159   TClonesArray* array = ChamberDigits(iChamber);
160   if ( index < 0 )
161   {
162     index = array->GetLast()+1;
163   }
164   return (new((*array)[index]) AliMUONDigit(*digit));
165 }
166
167 //_____________________________________________________________________________
168 Bool_t
169 AliMUONDigitStoreV1::Connect(TTree& tree, Bool_t alone) const
170 {
171   /// Connect this to the tree.
172   
173   AliMUONTreeManager tman;
174   Bool_t ok(kTRUE);
175   
176   TString baseName(BaseName(tree.GetName()));
177                      
178   // Search for branch MUON(S)Digits1 to know if we need to set branch addresses
179   // or to make them.
180   TBranch* branch = tree.GetBranch(Form("%ss%d",baseName.Data(),1));
181   
182   Bool_t isMaking = (branch==0);
183   
184   if ( isMaking ) 
185   {
186     for ( Int_t i = 0; i < AliMpConstants::NofChambers(); ++i ) 
187     {
188       TString branchName(Form("%ss%d",baseName.Data(),i+1));
189       ok = ok && tman.MakeBranch(tree,ClassName(),"TClonesArray",
190                                  branchName.Data(),ChamberDigitsPtr(i));
191     }
192   }
193   else
194   {
195     if ( alone && baseName != "MUONSDigit" ) 
196     {
197       // TreeS only has digits, so there's not need to play the branch status
198       // game
199       tman.UpdateBranchStatuses(tree,baseName.Data());
200     }
201     for ( Int_t i = 0; i < AliMpConstants::NofChambers(); ++i ) 
202     {
203       TString branchName(Form("%ss%d",baseName.Data(),i+1));
204       ok = ok && tman.SetAddress(tree,branchName.Data(),ChamberDigitsPtr(i));
205     }
206   }
207   
208   return ok;
209 }
210
211 //_____________________________________________________________________________
212 TObject**
213 AliMUONDigitStoreV1::ChamberDigitsPtr(Int_t chamberId) const
214 {
215   /// Get the address of the TClonesArray storing digits for chamberId.
216
217   return fDigits->GetObjectRef(fDigits->UncheckedAt(chamberId));
218
219   TObject* object = fDigits->At(chamberId);
220
221   if (!object)
222   {
223     AliError(Form("Cannot get digits for chamberId=%d",chamberId));
224     return 0x0;
225   }
226   else
227   {
228     return fDigits->GetObjectRef(object);
229   }
230 }
231
232 //_____________________________________________________________________________
233 TClonesArray*
234 AliMUONDigitStoreV1::ChamberDigits(Int_t chamberId)
235 {
236   /// Returns the tclonesarray storing digits for chamberId
237   return static_cast<TClonesArray*>(fDigits->At(chamberId));
238 }
239
240 //_____________________________________________________________________________
241 const TClonesArray*
242 AliMUONDigitStoreV1::ChamberDigits(Int_t chamberId) const
243 {
244   /// Returns the tclonesarray storing digits for chamberId
245   return static_cast<TClonesArray*>(fDigits->At(chamberId));
246 }
247
248 //_____________________________________________________________________________
249 AliMUONVDigit* 
250 AliMUONDigitStoreV1::CreateDigit(Int_t detElemId, Int_t manuId,
251                                  Int_t manuChannel, Int_t cathode) const
252 {
253   return new AliMUONDigit(detElemId,manuId,manuChannel,cathode);
254 }
255
256 //_____________________________________________________________________________
257 AliMUONVDigit* 
258 AliMUONDigitStoreV1::Remove(AliMUONVDigit& digit)
259 {
260   /// Remove one digit, and returns it, thus returning 0x0 if digit
261   /// is not present.
262   
263   Int_t index;
264   AliMUONVDigit* d(0x0);
265   if ( ( d = FindIndex(digit.DetElemId(),digit.ManuId(),digit.ManuChannel(),index) ) )
266   {
267     Int_t iChamber = AliMpDEManager::GetChamberId(digit.DetElemId());
268     TClonesArray* array = ChamberDigits(iChamber);
269     array->RemoveAt(index);
270     array->Compress();
271   }
272   return d;
273 }
274
275 //_____________________________________________________________________________
276 AliMUONVDigit* 
277 AliMUONDigitStoreV1::FindObject(Int_t detElemId, Int_t manuId, Int_t manuChannel) const
278 {
279   /// Find a digit indexed by the triplet (de,manu,channel)
280   Int_t index;
281   return FindIndex(detElemId,manuId,manuChannel,index);
282 }
283
284 //_____________________________________________________________________________
285 AliMUONVDigit* 
286 AliMUONDigitStoreV1::Find(const AliMUONVDigit& digit, Int_t& index) const
287 {
288   /// Find a digit, and return its index.
289   if ( digit.IsTrigger() )
290   {
291     return FindIndex(digit.DetElemId(),digit.ManuId(),digit.ManuChannel(),digit.Cathode(),index);
292   }
293   else
294   {
295     return FindIndex(digit.DetElemId(),digit.ManuId(),digit.ManuChannel(),index);
296   }
297 }
298
299 //_____________________________________________________________________________
300 AliMUONVDigit* 
301 AliMUONDigitStoreV1::FindObject(Int_t detElemId, Int_t localBoardId, 
302                                 Int_t localBoardChannel, Int_t cathode) const
303 {
304   /// Find a (trigger) digit
305   Int_t index;
306   return FindIndex(detElemId,localBoardId,localBoardChannel,cathode,index);
307 }
308
309 //_____________________________________________________________________________
310 AliMUONVDigit* 
311 AliMUONDigitStoreV1::FindIndex(Int_t detElemId, Int_t localBoardId, 
312                                Int_t localBoardChannel, Int_t cathode, Int_t& index) const
313 {
314   /// Find and return the index of a (trigger) digit
315   
316   AliMp::StationType stationType = AliMpDEManager::GetStationType(detElemId);
317   if ( stationType != AliMp::kStationTrigger ) 
318   {
319     AliError("This method is reserved for trigger detection elements");
320     return 0x0;
321   }
322   Int_t iChamber = AliMpDEManager::GetChamberId(detElemId);
323   const TClonesArray* array = ChamberDigits(iChamber);
324   if (!array) return 0x0;
325   TIter next(array);
326   AliMUONVDigit* digit;
327   index=0;
328   while ( ( digit = static_cast<AliMUONVDigit*>(next()) ) )
329   {
330     if ( digit->DetElemId() == detElemId &&
331          digit->ManuId() == localBoardId &&
332          digit->ManuChannel() == localBoardChannel &&
333          digit->Cathode() == cathode ) 
334     {
335       return digit;
336     }
337     ++index;
338   }
339   return 0x0;
340   
341 }
342
343 //_____________________________________________________________________________
344 AliMUONVDigit* 
345 AliMUONDigitStoreV1::FindIndex(Int_t detElemId, Int_t manuId, Int_t manuChannel, Int_t& index) const
346 {
347   /// Find and return the index of a (tracker) digit
348   Int_t iChamber = AliMpDEManager::GetChamberId(detElemId);
349   const TClonesArray* array = ChamberDigits(iChamber);
350   if (!array) return 0x0;
351   TIter next(array);
352   AliMUONVDigit* digit;
353   index = 0;
354   while ( ( digit = static_cast<AliMUONVDigit*>(next()) ) )
355   {
356     if ( digit->DetElemId() == detElemId &&
357          digit->ManuId() == manuId &&
358          digit->ManuChannel() == manuChannel ) 
359     {
360       return digit;
361     }
362     ++index;
363   }
364   return 0x0;
365 }
366
367 //_____________________________________________________________________________
368 TIterator* 
369 AliMUONDigitStoreV1::CreateIterator() const
370 {
371   /// Return an iterator on the full store
372   return new AliMUONTOTCAStoreIterator(fDigits,0,13);
373 }
374
375 //_____________________________________________________________________________
376 TIterator* 
377 AliMUONDigitStoreV1::CreateTrackerIterator() const
378 {
379   /// Return an iterator on the tracker part of the store
380   return new AliMUONTOTCAStoreIterator(fDigits,0,9);
381 }
382
383 //_____________________________________________________________________________
384 TIterator* 
385 AliMUONDigitStoreV1::CreateTriggerIterator() const
386 {
387   /// Return an iterator on the trigger part of the store
388   return new AliMUONTOTCAStoreIterator(fDigits,10,13);
389 }
390
391 //_____________________________________________________________________________
392 TIterator* 
393 AliMUONDigitStoreV1::CreateIterator(Int_t firstDetElemId, Int_t lastDetElemId,
394                                     Int_t cathode) const
395 {
396   /// Return an iterator on part of the store
397   return new AliMUONDigitStoreV1Iterator(fDigits,firstDetElemId,lastDetElemId,cathode);
398 }
399
400 //_____________________________________________________________________________
401 Int_t
402 AliMUONDigitStoreV1::GetSize() const
403 {
404   /// Return the number of digits we store
405   Int_t n(0);
406   
407   for ( Int_t i = 0; i <= fDigits->GetLast(); ++i ) 
408   {
409     n += ChamberDigits(i)->GetEntries();
410   }  
411   return n;
412 }