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