]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/MUONmapping/AliMpDDLStore.cxx
Fixes for object target dependencies
[u/mrichter/AliRoot.git] / MUON / MUONmapping / AliMpDDLStore.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 // $MpId: AliMpDDLStore.cxx,v 1.4 2006/05/24 13:58:34 ivana Exp $
18 // Category: management
19
20 //-----------------------------------------------------------------------------
21 // Class AliMpDDLStore
22 // --------------------
23 // The top container class for DDLs, det elements and bus patched
24 // It provides acces to DDL, det element and bus patches objects
25 // via various characteristics.
26 // Authors: Ivana Hrivnacova, IPN Orsay
27 //          Christian Finck, SUBATECH Nantes
28 //-----------------------------------------------------------------------------
29
30 #include <cstdlib>
31 #include "AliMpDDLStore.h"
32 #include "AliMpExMapIterator.h"
33 #include "AliMpConstants.h"
34 #include "AliMpDEStore.h"
35 #include "AliMpFrtCrocusConstants.h"
36 #include "AliMpDDL.h"
37 #include "AliMpFiles.h"
38 #include "AliMpDataStreams.h"
39 #include "AliMpHelper.h"
40 #include "AliMpDEManager.h"
41 #include "AliMpManuStore.h"
42 #include "AliMpDetElement.h"
43 #include "AliMpBusPatch.h"
44 #include "AliMpTriggerCrate.h"
45 #include "AliMpLocalBoard.h"
46 #include "AliMpSegmentation.h"
47 #include "AliMpVSegmentation.h"
48 #include "AliMpStringObjMap.h"
49 #include "AliMpEncodePair.h"
50 #include "AliMpIntPair.h"
51
52 #include "AliLog.h"
53
54 #include <Riostream.h>
55 #include <TList.h>
56 #include <TObjArray.h>
57 #include <TString.h>
58 #include <TObjString.h>
59 #include <TClass.h>
60
61 /// \cond CLASSIMP
62 ClassImp(AliMpDDLStore)
63 /// \endcond
64
65 AliMpDDLStore* AliMpDDLStore::fgInstance = 0;
66 const Int_t    AliMpDDLStore::fgkNofDDLs = 20;
67 const Int_t    AliMpDDLStore::fgkNofTriggerDDLs = 2;
68
69 //
70 // static methods
71 //
72
73 //______________________________________________________________________________
74 const TString&  AliMpDDLStore::GetRevertKeyword()
75 {
76   /// A keyword for ReadBusPatchSpecial()
77   static const TString kRevertKeyword = "REVERT"; 
78   return kRevertKeyword;
79 }  
80
81 //______________________________________________________________________________
82 const TString&  AliMpDDLStore::GetExplicitKeyword()
83 {
84   /// A keyword for ReadBusPatchSpecial()
85   static const TString  kExplicitKeyword = "EXPLICIT";
86   return kExplicitKeyword;
87 }  
88
89 //______________________________________________________________________________
90 AliMpDDLStore* AliMpDDLStore::Instance(Bool_t warn) 
91 {
92     /// Create the DDL store if it does not yet exist
93     /// and return its instance
94
95     if ( ! fgInstance && warn  ) {
96         AliWarningClass("DDL Store has not been loaded");
97     }
98
99     return fgInstance;
100 }
101
102 //______________________________________________________________________________
103 AliMpDDLStore* AliMpDDLStore::ReadData(const AliMpDataStreams& dataStreams,
104                                        Bool_t warn) 
105 {
106     /// Load the DDL store from ASCII data files
107     /// and return its instance
108
109     if ( fgInstance ) {
110         if ( warn )
111             AliWarningClass("DDL Store has been already loaded");
112         return fgInstance;
113     }
114
115     if ( dataStreams.GetReadFromFiles() )
116       AliInfoClass("Reading DDL Store from ASCII files.");
117
118     fgInstance = new AliMpDDLStore(dataStreams);
119     return fgInstance;
120 }
121
122 //
123 // ctors, dtor
124 //
125
126 //______________________________________________________________________________
127 AliMpDDLStore::AliMpDDLStore(const AliMpDataStreams& dataStreams)
128         : TObject(),
129         fDDLs(fgkNofDDLs+fgkNofTriggerDDLs), // FIXEME
130         fBusPatches(),
131         fManuList12(),
132         fManuBridge2(),
133         fRegionalTrigger()
134 {
135   /// Standard constructor
136   
137   AliDebug(1,"");
138   fDDLs.SetOwner(true);
139   fBusPatches.SetOwner(true);
140   fBusPatches.SetSize(900);
141
142   // Load segmentation & DE store data
143   if ( ! AliMpSegmentation::Instance(false) )
144     AliMpSegmentation::ReadData(dataStreams, true);
145     
146   // Create all detection elements
147   ReadDDLs(dataStreams);
148   ReadTrigger(dataStreams);
149   SetTriggerDDLs();
150   SetManus();
151   ReadBusPatchSpecial(dataStreams);
152   SetPatchModules();
153   ReadBusPatchInfo(dataStreams);
154 }
155
156 //______________________________________________________________________________
157 AliMpDDLStore::AliMpDDLStore(TRootIOCtor* ioCtor)
158         : TObject(),
159         fDDLs(),
160         fBusPatches(ioCtor),
161         fRegionalTrigger(ioCtor)
162 {
163     /// Constructor for I0
164
165     AliDebug(1,"");
166
167     fgInstance = this;
168 }
169
170
171 //______________________________________________________________________________
172 AliMpDDLStore::~AliMpDDLStore() 
173 {
174     /// Destructor
175
176     AliDebug(1,"");
177
178     // DDL objects are deleted with fDDLs
179     // Bus patches objects are deleted with fBusPatches
180
181     fgInstance = 0;
182 }
183
184 //
185 // private methods
186 //
187
188 //______________________________________________________________________________
189 Int_t  AliMpDDLStore::GetManuListIndex(Int_t detElemId) const 
190 {
191     /// Return the index of the manu list for given detElemId
192
193     return AliMpDEManager::GetChamberId(detElemId)*4 + (detElemId % 100);
194 }
195
196
197 //______________________________________________________________________________
198 Int_t AliMpDDLStore::GetBusPatchIndex(Int_t detElemId, Int_t manuId) const 
199 {
200     /// Calculate the index of the buspatch from manuId
201
202     Int_t pos = 0;
203     AliMp::StationType stationType = AliMpDEManager::GetStationType(detElemId);
204     static Int_t manuMask = AliMpConstants::ManuMask(AliMp::kNonBendingPlane) - 1;
205
206     if( stationType == AliMp::kStation345) {
207         pos = (manuId & manuMask)/100;
208     } else {
209         Int_t idx = GetManuListIndex(detElemId);
210
211         // using array defined from DetElemIdToBusPatch.dat file
212         for (pos = fManuList12[idx].GetSize()-1; pos >= 0; --pos)
213             if ( manuId >= fManuList12[idx].At(pos))
214                 break;
215     }
216
217     return pos;
218 }
219
220 //______________________________________________________________________________
221 Bool_t AliMpDDLStore::ReadDDLs(const AliMpDataStreams& dataStreams)
222 {
223     /// Read ddl <-> bus patch file
224
225     istream& in 
226       = dataStreams.
227           CreateDataStream(AliMpFiles::BusPatchFilePath());
228
229     char line[255];
230
231     while ( in.getline(line,255) ) {
232
233         if ( line[0] == '#' )
234             continue;
235
236         TString tmp(AliMpHelper::Normalize(line));
237
238         TObjArray* stringList = tmp.Tokenize(TString(" "));
239
240         TString sDE = ((TObjString*)stringList->At(0))->GetString();
241         Int_t idDE  = atoi(sDE.Data());
242
243         if ( ! AliMpDEManager::IsValidDetElemId(idDE, false) ) {
244             AliErrorStream() << "DetElemId "<< idDE << " not valid." << endl;
245             delete stringList;
246             return false;
247         }
248
249         TString busPatch = ((TObjString*)stringList->At(1))->GetString();
250
251
252         TString sDDL = ((TObjString*)stringList->At(2))->GetString();
253         Int_t  iDDL  = atoi(sDDL.Data());
254
255         if ( iDDL < 0 || iDDL >= fgkNofDDLs ) {
256             AliErrorStream() << "DDL id "<< iDDL << " outside limits." << endl;
257             delete stringList;
258             return false;
259         }
260
261         AliDebugStream(3)
262         << "idDE " << idDE << " buspatch " << busPatch.Data() << " iDDL " << iDDL
263         << endl;
264
265         // reading 1st manu Id for each bus patch (station 1 & 2)
266         if(AliMpDEManager::GetStationType(idDE) != AliMp::kStation345) {
267
268             TString sManu = ((TObjString*)stringList->At(3))->GetString();
269             AliMpHelper::DecodeName(sManu,',',fManuList12[GetManuListIndex(idDE)]);
270
271             if(AliMpDEManager::GetStation12Type(idDE) == AliMq::kStation2) {
272                 TString sManuBridge = ((TObjString*)stringList->At(4))->GetString();
273                 AliMpHelper::DecodeName(sManuBridge,',',fManuBridge2[GetManuListIndex(idDE)]);
274             }
275
276         }
277
278         delete stringList;
279
280         AliMpDDL* ddl = GetDDL(iDDL, false);
281         if ( !ddl) {
282             ddl = new AliMpDDL(iDDL);
283             fDDLs.AddAt(ddl, iDDL);
284         }
285         ddl->AddDE(idDE);
286
287         TArrayI busPatchList;
288         // decoding range of buspatch
289         AliMpHelper::DecodeName(busPatch,';',busPatchList);
290
291         // Update DE
292         AliMpDetElement* de = AliMpDEManager::GetDetElement(idDE);
293         de->SetDdlId(iDDL);
294         // filling buspatch -> idDE
295         for (Int_t i = 0; i < busPatchList.GetSize(); i++) {
296             fBusPatches.Add(busPatchList[i],
297                             new AliMpBusPatch(busPatchList[i], idDE, iDDL));
298             de->AddBusPatch(busPatchList[i]);
299         }
300     }
301
302     // Fill bus patch Ids array in DDLs now
303     for ( Int_t i=0; i<fDDLs.GetEntriesFast(); i++ ) {
304         AliMpDDL* ddl = (AliMpDDL*) fDDLs.At(i);
305         ddl->FillBusPatchIds();
306     }
307
308     delete &in;
309     return true;
310 }
311
312 //______________________________________________________________________________
313 Bool_t  AliMpDDLStore::ReadTrigger(const AliMpDataStreams& dataStreams)
314 {
315     /// create trigger DDL object and Global crate object
316   
317   if ( ! fRegionalTrigger.ReadData(dataStreams) ) return false;
318
319   return true;
320 }
321
322 //______________________________________________________________________________
323 Bool_t  
324 AliMpDDLStore::SetTriggerDDLs() 
325 {
326   /// Create trigger DDLs and set DDL Ids in the regional trigger
327
328   Int_t iDDL = -1;
329   TIter next(fRegionalTrigger.CreateCrateIterator());
330   AliMpTriggerCrate* crate;
331   
332   while ( ( crate = static_cast<AliMpTriggerCrate*>(next()) ) )
333   {
334     TString crateName = crate->GetName();
335
336     // determine ddl number vs crate side
337     if (crateName.Contains("R"))
338       iDDL = fgkNofDDLs; // starts where tracker ends
339     else
340       iDDL = fgkNofDDLs + 1;
341
342     // Create DDL if it does not yet exist and set it to the crate
343     AliMpDDL* ddl = (AliMpDDL*)fDDLs.At(iDDL);
344     if ( !ddl) {
345       ddl = new AliMpDDL(iDDL);
346       fDDLs.AddAt(ddl, iDDL);
347     }
348     crate->SetDdlId(iDDL);
349     
350     
351     // Add trigger crate number for given ddl if not present
352     if ( !ddl->HasTriggerCrateId(crate->GetId()) )
353       ddl->AddTriggerCrate(crate->GetId());
354     
355     
356     // Loop over local boards in this crate
357
358     for ( Int_t j=0; j<crate->GetNofLocalBoards(); ++j ) 
359     {
360       Int_t localBoardId = crate->GetLocalBoardId(j);
361       AliMpLocalBoard* localBoard 
362         = fRegionalTrigger.FindLocalBoard(localBoardId);
363       if (!localBoard ) {
364         AliFatalClass("Cannot find local board.");
365         return kFALSE;
366       }   
367       
368       // Loop over DEs in this localBoard 
369         
370       for ( Int_t k=0; k<localBoard->GetNofDEs(); ++k ) 
371       {
372     
373         Int_t deId = localBoard->GetDEId(k);
374         AliMpDetElement* de = AliMpDEManager::GetDetElement(deId);
375
376         if ( de->GetDdlId() == -1 ) de->SetDdlId(iDDL);
377
378         if ( ! ddl->HasDEId(deId) ) ddl->AddDE(deId);
379       }   
380     }
381   }
382   return kTRUE;
383 }
384
385 //______________________________________________________________________________
386 Bool_t AliMpDDLStore::SetManus() 
387 {
388     /// Set manus for each bus patch
389
390     Int_t manuMask = AliMpConstants::ManuMask(AliMp::kNonBendingPlane) - 1;
391
392     // loop over DDL
393     for (Int_t iDDL = 0; iDDL < fgkNofDDLs; ++iDDL) {
394
395         AliDebug(3, Form("DDL # %d\n", iDDL));
396
397         AliMpDDL* ddl = GetDDL(iDDL);
398
399         // loop over DE in the given DDL
400         for (Int_t detElemIdx = 0; detElemIdx < ddl->GetNofDEs(); ++detElemIdx) {
401
402             Int_t detElemId = ddl->GetDEId(detElemIdx);
403
404             AliMpDetElement* detElement = GetDetElement(detElemId);
405
406             AliMp::StationType stationType = AliMpDEManager::GetStationType(detElemId);
407
408
409             // list of manu per DE on both cathode
410             TList manuList;
411             for ( Int_t cath = 0; cath < 2 ; ++cath ) {
412                 const AliMpVSegmentation* seg
413                 = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(cath));
414
415                 AliMp::PlaneType planeType = detElement->GetPlaneType(AliMp::GetCathodType(cath));
416
417                 TArrayI manus;
418                 seg->GetAllElectronicCardIDs(manus);
419
420                 // filling TList manu
421                 for ( Int_t im = 0; im < manus.GetSize(); ++im ) {
422
423                     AliMpIntPair* manu = 0x0;
424                     if( stationType == AliMp::kStation345)
425                         manu = new AliMpIntPair((manus[im] & manuMask), planeType, kTRUE); //remove offset for NB
426                     else
427                         manu = new AliMpIntPair(manus[im], planeType, kTRUE); //keep offset for NB
428
429                     manuList.Add(manu);
430
431                     detElement->AddManu(manus[im]);
432                 }
433             }// cathode
434
435             manuList.Sort(); // sort regardless B or NB plane
436
437             // filling manu to the corresponding buspatch
438             for (Int_t iEntry = 0; iEntry < manuList.GetEntries(); ++iEntry) {
439
440                 AliMpIntPair* manuPtr = (AliMpIntPair*)manuList.At(iEntry);
441
442                 Int_t manuId = manuPtr->GetFirst();
443                 Int_t pos    = GetBusPatchIndex(detElemId, manuId);
444
445                 if (pos > detElement->GetNofBusPatches()) {
446                     AliError(Form("pos greater %d than size %d manuId %d detElemId %d \n",
447                                   pos, detElement->GetNofBusPatches(), manuId, detElemId));
448                     return false;
449                 }
450
451                 // get buspatch and fill manus
452                 Int_t busPatchId = detElement->GetBusPatchId(pos);
453                 AliMpBusPatch* busPatch = GetBusPatch(busPatchId);
454
455                 if( stationType == AliMp::kStation345) {
456
457                     if (manuPtr->GetSecond())
458                         busPatch->AddManu(manuId+manuMask+1); // add offset again after sorted
459                     else
460                         busPatch->AddManu(manuId);
461
462                 } else {
463
464                     busPatch->AddManu(manuId);
465
466                 }
467             }
468
469             manuList.Delete();
470
471             if (AliDebugLevel() == 3) {
472
473                 // print out for checking
474                 for(Int_t pos = 0; pos < detElement->GetNofBusPatches(); ++pos) {
475                     Int_t busPatchId = detElement->GetBusPatchId(pos);
476                     AliMpBusPatch* busPatch = GetBusPatch(busPatchId);
477                     printf("BusPatch: %d\n", busPatch->GetId());
478                     for (Int_t iEntry = 0; iEntry < busPatch->GetNofManus(); ++iEntry)
479                         printf("manu Id: %d\n", busPatch->GetManuId(iEntry));
480                 }
481             }
482
483         } // detection element loop
484     }// DDL loop
485
486     return true;
487 }
488
489 //______________________________________________________________________________
490 Bool_t AliMpDDLStore::ReadBusPatchSpecial(const AliMpDataStreams& dataStreams)
491 {
492 /// Read file with bus patches with a special order of manus
493 /// and reset the manus arrays filled via SetManu function
494
495   istream& in 
496     = dataStreams.
497         CreateDataStream(AliMpFiles::BusPatchSpecialFilePath());
498
499   char line[255];
500
501   while ( in.getline(line,255) ) {
502
503     if ( line[0] == '#' ) continue;
504
505     TString tmp(AliMpHelper::Normalize(line));
506     TObjArray* stringList = tmp.Tokenize(TString(" "));
507
508     TString sKey = ((TObjString*)stringList->At(0))->GetString();
509     
510     TString sDDL = ((TObjString*)stringList->At(1))->GetString();
511     TArrayI ddlList;
512     AliMpHelper::DecodeName(sDDL,';',ddlList);
513
514     TString sBusPatch = ((TObjString*)stringList->At(2))->GetString();
515     TArrayI busPatchList;
516     AliMpHelper::DecodeName(sBusPatch,',',busPatchList);
517     
518     // Loop over DDL and Bus Patch
519     for (Int_t iDDL = 0; iDDL < ddlList.GetSize(); ++iDDL ) {
520       for (Int_t iBusPatch = 0; iBusPatch < busPatchList.GetSize(); ++iBusPatch) {
521         // Global bus patch ID
522         Int_t busPatchID 
523           = AliMpBusPatch::GetGlobalBusID(
524               busPatchList.At(iBusPatch), ddlList.At(iDDL));
525         
526         // Get this bus patch
527         AliMpBusPatch* busPatch = GetBusPatch(busPatchID);
528         if ( ! busPatch ) {
529           AliErrorStream() << "Bus patch " << busPatchID << " does not exist." << endl;
530           delete stringList;
531           return kFALSE;
532         }
533      
534         if ( sKey == GetRevertKeyword() ) {
535           AliDebugStream(3)
536             << "Reverting readout of bus patch " << busPatchID  << endl;
537         
538           // Now revert the manus in this bus patch
539           busPatch->RevertReadout();     
540         }
541         else if ( sKey == GetExplicitKeyword() ) {
542         
543           busPatch->ResetReadout();
544
545           TString sManus = ((TObjString*)stringList->At(3))->GetString();
546           TArrayI manuList;
547           AliMpHelper::DecodeName(sManus,',',manuList);
548
549           AliDebugStream(3)
550             << "Reseting readout of bus patch " << busPatchID  
551             << "  manus: " << sManus.Data() << endl;
552
553           for (Int_t i = 0; i < manuList.GetSize(); i++) {
554             busPatch->AddManu(manuList.At(i));
555           }
556         }
557         else {             
558           AliErrorStream() << "Unrecognized key." << endl;
559           delete stringList;
560           return kFALSE;
561         }
562       }
563     }
564     delete stringList;
565   }
566   
567   delete &in;
568   
569   return kTRUE;
570 }    
571  
572
573 //______________________________________________________________________________
574 Bool_t AliMpDDLStore::SetPatchModules() 
575 {
576     /// Compute the number of manu per PCB for each buspatch
577
578     AliMpDEIterator it;
579     Bool_t result = true;
580
581     for ( it.First(); !it.IsDone(); it.Next() ) {
582
583         AliMpDetElement* detElement = it.CurrentDE();
584
585         for (Int_t i = 0; i < detElement->GetNofBusPatches(); ++i) {
586             AliMpBusPatch* busPatch = GetBusPatch(detElement->GetBusPatchId(i));
587             Bool_t newResult = false;
588             Int_t idDE = busPatch->GetDEId();
589
590             if (AliMpDEManager::GetStation12Type(idDE) == AliMq::kStation2 )
591                 newResult = busPatch->SetNofManusPerModule(fManuBridge2[GetManuListIndex(idDE)].At(i));
592             else
593                 newResult = busPatch->SetNofManusPerModule();
594             result = result && newResult;
595         }
596     }
597
598     return result;
599 }
600
601 //______________________________________________________________________________
602 Bool_t AliMpDDLStore::ReadBusPatchInfo(const AliMpDataStreams& dataStreams)
603 {
604     /// read the buspatch info file and set buspatch info
605
606     istream& in 
607       = dataStreams.
608           CreateDataStream(AliMpFiles::BusPatchInfoFilePath());
609
610     char line[255];
611
612     for (Int_t iDDL = 0; iDDL < fgkNofDDLs; ++iDDL ) {
613         AliMpDDL* ddl = GetDDL(iDDL);
614
615         for (Int_t iBusPatch = 0; iBusPatch < ddl->GetNofBusPatches(); ++iBusPatch) {
616
617             do {
618                 if (!in.getline(line,255)) {
619                     AliWarning(Form("Wrong size in bus patch length file; index %d DDL %d",
620                                     iBusPatch, iDDL));
621                     return false;
622                 }
623             } while(line[0] == '#');
624
625             TString tmp(AliMpHelper::Normalize(line));
626
627             TObjArray* stringList = tmp.Tokenize(TString(" "));
628
629             // Crocus label
630             TString crLabel    = ((TObjString*)stringList->At(0))->GetString();
631             Int_t pos          = crLabel.First('-');
632             tmp                = crLabel(pos-2, crLabel.Length()-pos+2);
633             TArrayI list;
634             AliMpHelper::DecodeName(tmp.Data(), '-', list);
635             
636             Int_t localDDLId  = list[0];
637             Int_t frtId       = list[1] - 1; // begin at zero ! 
638             Int_t localBusId  = list[2];
639
640             // Add FRT number for given ddl if not present
641             if ( !ddl->HasFrtId(frtId) )
642               ddl->AddFrt(frtId);
643
644             // BP & translator label
645             TString label      = ((TObjString*)stringList->At(1))->GetString();
646             TString transLabel = ((TObjString*)stringList->At(2))->GetString();
647
648             // BP length
649             TString sLength    = ((TObjString*)stringList->At(3))->GetString();
650             Float_t length     = sLength.Atof();
651
652             delete stringList;
653                        
654             if (localBusId != iBusPatch + 1)
655                AliWarning(Form("Wrong local buspatch id %d instead of %d", iBusPatch+1, localBusId));
656                
657             if(localDDLId != ddl->GetId()+1)
658                 AliWarning(Form("Wrong local DDL id %d instead of %d", ddl->GetId()+1, localDDLId));
659
660             Int_t busPatchId = ddl->GetBusPatchId(iBusPatch);
661             AliMpBusPatch* busPatch = GetBusPatch(busPatchId);
662             busPatch->SetCableLength(length);
663             busPatch->SetCableLabel(label);
664             busPatch->SetTranslatorLabel(transLabel);
665             busPatch->SetFrtId(frtId);
666
667         }
668     }
669     
670     delete &in;
671
672     return true;
673 }
674
675
676 //________________________________________________________________
677 Int_t AliMpDDLStore::GetLocalBoardId(TString name) const {
678     /// return the first board with a given side and line
679
680   TIter next(fRegionalTrigger.CreateLocalBoardIterator());
681   AliMpLocalBoard* local;
682   
683   while ( ( local = static_cast<AliMpLocalBoard*>(next()) ) )
684   {
685         TString tmp(&local->GetName()[4], 2);
686         if (name.Contains(tmp))
687             if (name[0] == local->GetName()[0])
688                 return local->GetId();
689     }
690
691     return 0;
692 }
693
694 //
695 // public methods
696 //
697
698
699 //______________________________________________________________________________
700 AliMpDDL* AliMpDDLStore::GetDDL(Int_t ddlId, Bool_t warn) const {
701     /// Return DDL for given ddlId
702
703     AliMpDDL* ddl
704     = (AliMpDDL*)fDDLs.At(ddlId);
705
706     if ( ! ddl && warn ) {
707         AliErrorStream()
708         << "DDL with Id = " << ddlId << " not defined." << endl;
709     }
710
711     return ddl;
712 }
713
714 //______________________________________________________________________________
715 AliMpDetElement*  AliMpDDLStore::GetDetElement(Int_t detElemId, Bool_t warn) const {
716     /// Return detection element with given detElemId
717
718     if ( ! AliMpDEStore::Instance() ) {
719         AliFatal("DE Store has not been loaded.");
720         return 0;
721     }
722
723     return AliMpDEStore::Instance()->GetDetElement(detElemId, warn);
724 }
725
726 //______________________________________________________________________________
727 AliMpBusPatch* AliMpDDLStore::GetBusPatch(Int_t busPatchId, Bool_t warn) const {
728     /// Return bus patch with given Id
729
730     AliMpBusPatch* busPatch
731     = (AliMpBusPatch*) fBusPatches.GetValue(busPatchId);
732
733     if ( ! busPatch && warn ) {
734         AliErrorStream()
735         << "Bus patch with Id = " << busPatchId << " not defined." << endl;
736     }
737
738     return busPatch;
739 }
740
741
742 //______________________________________________________________________________
743 AliMpLocalBoard* AliMpDDLStore::GetLocalBoard(Int_t localBoardId, Bool_t warn) const {
744     /// Return bus patch with given Id
745
746     return fRegionalTrigger.FindLocalBoard(localBoardId, warn);
747 }
748
749 //______________________________________________________________________________
750 AliMpTriggerCrate* AliMpDDLStore::GetTriggerCrate(TString name, Bool_t warn) const  {
751     /// Return trigger crate with given name
752
753     return fRegionalTrigger.FindTriggerCrate(name, warn);
754 }
755
756 //______________________________________________________________________________
757 AliMpTriggerCrate* AliMpDDLStore::GetTriggerCrate(Int_t ddlId, Int_t index, Bool_t warn) const  {
758     /// Return trigger crate with given ddl and index crate
759
760     if (ddlId == 0 || ddlId == 1)
761         ddlId += fgkNofDDLs;
762
763     AliMpDDL* ddl = GetDDL(ddlId, warn);
764     if ( ! ddl )
765         return 0;
766
767     if ( index >= ddl->GetNofTriggerCrates() ) {
768         AliError(Form("crate id %d greater than array[%d]", index, ddl->GetNofTriggerCrates()));
769         return 0;
770     }
771
772     TString name = AliMpTriggerCrate::GenerateName(index, ddlId, fgkNofDDLs);
773
774     return GetTriggerCrate(name, warn);
775 }
776
777 //______________________________________________________________________________
778 Int_t  AliMpDDLStore::GetDEfromBus(Int_t busPatchId) const {
779     /// Return detection element Id for given busPatchId
780
781     AliMpBusPatch* busPatch = GetBusPatch(busPatchId);
782
783     if ( ! busPatch ) {
784         AliErrorStream()
785         << "Bus patch with Id = " << busPatchId << " not defined." << endl;
786         return 0;
787     }
788
789     return busPatch->GetDEId();
790 }
791
792 //______________________________________________________________________________
793 Int_t  AliMpDDLStore::GetDEfromLocalBoard(Int_t localBoardId, Int_t chamberId) const {
794     /// Return detElemId for local board Id and chamber id.
795
796     AliMpLocalBoard* localBoard = GetLocalBoard(localBoardId);
797
798     if ( ! localBoard ) {
799         AliErrorStream()
800         << "Loacl board with Id = " << localBoardId << " not defined." << endl;
801         return 0;
802     }
803
804     return localBoard->GetDEIdByChamber(chamberId);
805 }
806
807 //______________________________________________________________________________
808 Int_t  AliMpDDLStore::GetDDLfromBus(Int_t busPatchId) const {
809     /// Return DDL Id for given busPatchId
810
811     AliMpBusPatch* busPatch = GetBusPatch(busPatchId);
812
813     if ( ! busPatch ) {
814         AliErrorStream()
815         << "Bus patch with Id = " << busPatchId << " not defined." << endl;
816         return 0;
817     }
818
819     return busPatch->GetDdlId();
820 }
821
822 //______________________________________________________________________________
823 Int_t AliMpDDLStore::GetBusPatchId(Int_t detElemId, Int_t manuId) const {
824     /// Return bus patch for a given manuId
825
826     AliMpDetElement* detElement = GetDetElement(detElemId);
827     Int_t pos = GetBusPatchIndex(detElemId, manuId);
828
829     if ( pos >= detElement->GetNofBusPatches() ) 
830     {
831         AliErrorStream()
832         << "Pos = " << pos
833         << " greater than the size = " <<  detElement->GetNofBusPatches()
834         << " for detElemId = " << detElemId
835         << " manuId = " << manuId << endl;
836         return -1;
837     }
838
839     return detElement->GetBusPatchId(pos);
840 }
841
842
843 //______________________________________________________________________________
844 Long_t AliMpDDLStore::GetLinkPortId(Int_t busPatchId) const {
845
846     /// Get link port and DSP from busPatch id.
847     /// Return -1 if the value is not valid 
848     /// (the validity has to be tested in the client code)
849
850     AliMpBusPatch* busPatch = GetBusPatch(busPatchId);
851     Int_t ddlId = busPatch->GetDdlId();
852         
853     Int_t localBusPatchId = AliMpBusPatch::GetLocalBusID(busPatchId, ddlId) - 1; // begin at zero
854
855     Int_t pos = (localBusPatchId % AliMpFrtCrocusConstants::GetNofBusPatches()); 
856     
857     return AliMpFrtCrocusConstants::GetLinkPortId(pos);
858
859 }
860
861 //______________________________________________________________________________
862 void AliMpDDLStore::PrintAllManu() const {
863     /// Print all manu Ids and their serial numbers sorted by detection element
864     /// and bus patch.                                                            \n
865     /// As serial manu numbers are filled in a different way than manu Ids this
866     /// printing allows to check that both ways are consistent
867
868     // Loop over DE
869     AliMpDEIterator it;
870     for ( it.First(); ! it.IsDone(); it.Next() ) {
871         AliMpDetElement* de = it.CurrentDE();
872         cout << "DE: " << de->GetId() << endl;
873
874         // Loop over bus patches in this DE
875         for ( Int_t i=0; i< de->GetNofBusPatches(); ++i ) {
876
877             AliMpBusPatch* busPatch = GetBusPatch(de->GetBusPatchId(i));
878             cout << "  busPatch: " << busPatch->GetId() << endl;
879
880             cout << "    Manu       : ";
881             for ( Int_t j=0; j<busPatch->GetNofManus(); ++j ) {
882                 cout << std::setw(6) << busPatch->GetManuId(j) << " ";
883             }
884             cout << endl;
885
886             if ( AliMpManuStore::Instance(kFALSE) ) {
887               cout << "    Manu serial: ";
888               for ( Int_t k=0; k<busPatch->GetNofManus(); ++k ) {
889                 cout << std::setw(6) 
890                      << AliMpManuStore::Instance()
891                         ->GetManuSerial(de->GetId(), busPatch->GetManuId(k)) << " ";
892               }
893               cout << endl;
894             }  
895         }
896     }
897 }
898
899 //______________________________________________________________________________
900 Int_t  AliMpDDLStore::GetNextDEfromLocalBoard(Int_t localBoardId, Int_t chamberId ) const {
901     /// return the next detection element in line
902
903     AliMpLocalBoard* localBoard  =  GetLocalBoard(localBoardId);
904
905     TString name(localBoard->GetName());
906
907     Int_t line = AliMp::PairFirst(localBoard->GetPosition());
908     ++line;
909
910     name.Replace(4,1,Form("%d", line));
911
912     Int_t nextLocalId;
913     if ((nextLocalId = GetLocalBoardId(name)))
914         return GetDEfromLocalBoard(nextLocalId, chamberId);
915     else
916         return 0;
917
918     return 0;
919 }
920
921 //______________________________________________________________________________
922 Int_t  AliMpDDLStore::GetPreviousDEfromLocalBoard(Int_t localBoardId, Int_t chamberId) const {
923     /// return the previous detection element in line
924
925     AliMpLocalBoard* localBoard  =  GetLocalBoard(localBoardId);
926
927     TString name(localBoard->GetName());
928
929     Int_t line = AliMp::PairFirst(localBoard->GetPosition());
930     --line;
931
932     name.Replace(4,1,Form("%d", line));
933
934     Int_t prevLocalId;
935     if ((prevLocalId = GetLocalBoardId(name)))
936         return GetDEfromLocalBoard(prevLocalId, chamberId);
937     else
938         return 0;
939
940 }
941
942 //______________________________________________________________________________
943 void AliMpDDLStore::SetRegionalTrigger(const AliMpRegionalTrigger& regionalTrigger)
944 {
945 /// Replace the existing regional trigger with the given one
946
947   fRegionalTrigger = regionalTrigger;
948   
949   // Remove the existing trigger DDLsf
950   fDDLs.RemoveAt(fgkNofDDLs+1);
951   fDDLs.RemoveAt(fgkNofDDLs);
952   
953   // Set new trigger DDLs from new regional trigger
954   SetTriggerDDLs();
955 }  
956
957
958 //______________________________________________________________________________
959 TIterator* 
960 AliMpDDLStore::CreateBusPatchIterator() const
961 {
962 /// Create the iterator over bus patches
963
964   return fBusPatches.CreateIterator();
965 }