]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/mapping/AliMpDDLStore.cxx
51d04613e3d7dd0afa8b9afb046c33f3960d0b1e
[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 "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
50 #include "AliLog.h"
51
52 #include <Riostream.h>
53 #include <TList.h>
54 #include <TObjArray.h>
55 #include <TString.h>
56 #include <TObjString.h>
57 #include <TClass.h>
58
59 /// \cond CLASSIMP
60 ClassImp(AliMpDDLStore)
61 /// \endcond
62
63 AliMpDDLStore* AliMpDDLStore::fgInstance = 0;
64 const Int_t    AliMpDDLStore::fgkNofDDLs = 20;
65 const Int_t    AliMpDDLStore::fgkNofTriggerDDLs = 2;
66 const TString  AliMpDDLStore::fgkRevertKeyword = "REVERT"; 
67 const TString  AliMpDDLStore::fgkExplicitKeyword = "EXPLICIT";
68
69 //
70 // static methods
71 //
72
73 //______________________________________________________________________________
74 AliMpDDLStore* AliMpDDLStore::Instance(Bool_t warn) 
75 {
76     /// Create the DDL store if it does not yet exist
77     /// and return its instance
78
79     if ( ! fgInstance && warn  ) {
80         AliWarningClass("DDL Store has not been loaded");
81     }
82
83     return fgInstance;
84 }
85
86 //______________________________________________________________________________
87 AliMpDDLStore* AliMpDDLStore::ReadData(const AliMpDataStreams& dataStreams,
88                                        Bool_t warn) 
89 {
90     /// Load the DDL store from ASCII data files
91     /// and return its instance
92
93     if ( fgInstance ) {
94         if ( warn )
95             AliWarningClass("DDL Store has been already loaded");
96         return fgInstance;
97     }
98
99     if ( dataStreams.GetReadFromFiles() )
100       AliInfoClass("Reading Manu Store from ASCII files.");
101
102     fgInstance = new AliMpDDLStore(dataStreams);
103     return fgInstance;
104 }
105
106 //
107 // ctors, dtor
108 //
109
110 //______________________________________________________________________________
111 AliMpDDLStore::AliMpDDLStore(const AliMpDataStreams& dataStreams)
112         : TObject(),
113         fkDataStreams(dataStreams),
114         fDDLs(fgkNofDDLs+fgkNofTriggerDDLs), // FIXEME
115         fBusPatches(),
116         fManuList12(),
117         fManuBridge2(),
118         fRegionalTrigger()
119 {
120   /// Standard constructor
121   
122   AliDebug(1,"");
123   fDDLs.SetOwner(true);
124   fBusPatches.SetOwner(true);
125   fBusPatches.SetSize(900);
126
127   // Load segmentation & DE store data
128   if ( ! AliMpSegmentation::Instance(false) )
129     AliMpSegmentation::ReadData(dataStreams, true);
130     
131   // Create all detection elements
132   ReadDDLs();
133   ReadTrigger();
134   SetTriggerDDLs();
135   SetManus();
136   ReadBusPatchSpecial();
137   SetPatchModules();
138   ReadBusPatchInfo();
139 }
140
141 //______________________________________________________________________________
142 AliMpDDLStore::AliMpDDLStore(TRootIOCtor* ioCtor)
143         : TObject(),
144         fkDataStreams(ioCtor),
145         fDDLs(),
146         fBusPatches(ioCtor),
147         fRegionalTrigger(ioCtor)
148 {
149     /// Constructor for I0
150
151     AliDebug(1,"");
152
153     fgInstance = this;
154 }
155
156
157 //______________________________________________________________________________
158 AliMpDDLStore::~AliMpDDLStore() 
159 {
160     /// Destructor
161
162     AliDebug(1,"");
163
164     // DDL objects are deleted with fDDLs
165     // Bus patches objects are deleted with fBusPatches
166
167     fgInstance = 0;
168 }
169
170 //
171 // private methods
172 //
173
174 //______________________________________________________________________________
175 Int_t  AliMpDDLStore::GetManuListIndex(Int_t detElemId) const 
176 {
177     /// Return the index of the manu list for given detElemId
178
179     return AliMpDEManager::GetChamberId(detElemId)*4 + (detElemId % 100);
180 }
181
182
183 //______________________________________________________________________________
184 Int_t AliMpDDLStore::GetBusPatchIndex(Int_t detElemId, Int_t manuId) const 
185 {
186     /// Calculate the index of the buspatch from manuId
187
188     Int_t pos = 0;
189     AliMp::StationType stationType = AliMpDEManager::GetStationType(detElemId);
190     static Int_t manuMask = AliMpConstants::ManuMask(AliMp::kNonBendingPlane) - 1;
191
192     if( stationType == AliMp::kStation345) {
193         pos = (manuId & manuMask)/100;
194     } else {
195         Int_t idx = GetManuListIndex(detElemId);
196
197         // using array defined from DetElemIdToBusPatch.dat file
198         for (pos = fManuList12[idx].GetSize()-1; pos >= 0; --pos)
199             if ( manuId >= fManuList12[idx].At(pos))
200                 break;
201     }
202
203     return pos;
204 }
205
206 //______________________________________________________________________________
207 Bool_t AliMpDDLStore::ReadDDLs() 
208 {
209     /// Read ddl <-> bus patch file
210
211     istream& in 
212       = fkDataStreams.
213           CreateDataStream(AliMpFiles::BusPatchFilePath());
214
215     char line[255];
216
217     while ( in.getline(line,255) ) {
218
219         if ( line[0] == '#' )
220             continue;
221
222         TString tmp(AliMpHelper::Normalize(line));
223
224         TObjArray* stringList = tmp.Tokenize(TString(" "));
225
226         TString sDE = ((TObjString*)stringList->At(0))->GetString();
227         Int_t idDE  = atoi(sDE.Data());
228
229         if ( ! AliMpDEManager::IsValidDetElemId(idDE, false) ) {
230             AliErrorStream() << "DetElemId "<< idDE << " not valid." << endl;
231             return false;
232         }
233
234         TString busPatch = ((TObjString*)stringList->At(1))->GetString();
235
236
237         TString sDDL = ((TObjString*)stringList->At(2))->GetString();
238         Int_t  iDDL  = atoi(sDDL.Data());
239
240         if ( iDDL < 0 || iDDL >= fgkNofDDLs ) {
241             AliErrorStream() << "DDL id "<< iDDL << " outside limits." << endl;
242             return false;
243         }
244
245         AliDebugStream(3)
246         << "idDE " << idDE << " buspatch " << busPatch.Data() << " iDDL " << iDDL
247         << endl;
248
249         // reading 1st manu Id for each bus patch (station 1 & 2)
250         if(AliMpDEManager::GetStationType(idDE) != AliMp::kStation345) {
251
252             TString sManu = ((TObjString*)stringList->At(3))->GetString();
253             AliMpHelper::DecodeName(sManu,',',fManuList12[GetManuListIndex(idDE)]);
254
255             if(AliMpDEManager::GetStation12Type(idDE) == AliMq::kStation2) {
256                 TString sManuBridge = ((TObjString*)stringList->At(4))->GetString();
257                 AliMpHelper::DecodeName(sManuBridge,',',fManuBridge2[GetManuListIndex(idDE)]);
258             }
259
260         }
261
262         delete stringList;
263
264         AliMpDDL* ddl = GetDDL(iDDL, false);
265         if ( !ddl) {
266             ddl = new AliMpDDL(iDDL);
267             fDDLs.AddAt(ddl, iDDL);
268         }
269         ddl->AddDE(idDE);
270
271         TArrayI busPatchList;
272         // decoding range of buspatch
273         AliMpHelper::DecodeName(busPatch,';',busPatchList);
274
275         // Update DE
276         AliMpDetElement* de = AliMpDEManager::GetDetElement(idDE);
277         de->SetDdlId(iDDL);
278         // filling buspatch -> idDE
279         for (Int_t i = 0; i < busPatchList.GetSize(); i++) {
280             fBusPatches.Add(busPatchList[i],
281                             new AliMpBusPatch(busPatchList[i], idDE, iDDL));
282             de->AddBusPatch(busPatchList[i]);
283         }
284     }
285
286     // Fill bus patch Ids array in DDLs now
287     for ( Int_t i=0; i<fDDLs.GetEntriesFast(); i++ ) {
288         AliMpDDL* ddl = (AliMpDDL*) fDDLs.At(i);
289         ddl->FillBusPatchIds();
290     }
291
292     delete &in;
293     return true;
294 }
295
296 //______________________________________________________________________________
297 Bool_t  AliMpDDLStore::ReadTrigger() 
298 {
299     /// create trigger DDL object and Global crate object
300   
301   if ( ! fRegionalTrigger.ReadData(fkDataStreams) ) return false;
302
303   return true;
304 }
305
306 //______________________________________________________________________________
307 Bool_t  
308 AliMpDDLStore::SetTriggerDDLs() 
309 {
310   /// Create trigger DDLs and set DDL Ids in the regional trigger
311
312   Int_t iDDL = -1;
313   TIter next(fRegionalTrigger.CreateCrateIterator());
314   AliMpTriggerCrate* crate;
315   
316   while ( ( crate = static_cast<AliMpTriggerCrate*>(next()) ) )
317   {
318     TString crateName = crate->GetName();
319
320     // determine ddl number vs crate side
321     if (crateName.Contains("R"))
322       iDDL = fgkNofDDLs; // starts where tracker ends
323     else
324       iDDL = fgkNofDDLs + 1;
325
326     // Create DDL if it does not yet exist and set it to the crate
327     AliMpDDL* ddl = (AliMpDDL*)fDDLs.At(iDDL);
328     if ( !ddl) {
329       ddl = new AliMpDDL(iDDL);
330       fDDLs.AddAt(ddl, iDDL);
331     }
332     crate->SetDdlId(iDDL);
333     
334     
335     // Add trigger crate number for given ddl if not present
336     if ( !ddl->HasTriggerCrateId(crate->GetId()) )
337       ddl->AddTriggerCrate(crate->GetId());
338     
339     
340     // Loop over local boards in this crate
341
342     for ( Int_t j=0; j<crate->GetNofLocalBoards(); ++j ) 
343     {
344       Int_t localBoardId = crate->GetLocalBoardId(j);
345       AliMpLocalBoard* localBoard 
346         = fRegionalTrigger.FindLocalBoard(localBoardId);
347       if (!localBoard ) {
348         AliFatalClass("Cannot find local board.");
349         return kFALSE;
350       }   
351       
352       // Loop over DEs in this localBoard 
353         
354       for ( Int_t k=0; k<localBoard->GetNofDEs(); ++k ) 
355       {
356     
357         Int_t deId = localBoard->GetDEId(k);
358         AliMpDetElement* de = AliMpDEManager::GetDetElement(deId);
359
360         if ( de->GetDdlId() == -1 ) de->SetDdlId(iDDL);
361
362         if ( ! ddl->HasDEId(deId) ) ddl->AddDE(deId);
363       }   
364     }
365   }
366   return kTRUE;
367 }
368
369 //______________________________________________________________________________
370 Bool_t AliMpDDLStore::SetManus() 
371 {
372     /// Set manus for each bus patch
373
374     Int_t manuMask = AliMpConstants::ManuMask(AliMp::kNonBendingPlane) - 1;
375
376     // loop over DDL
377     for (Int_t iDDL = 0; iDDL < fgkNofDDLs; ++iDDL) {
378
379         AliDebug(3, Form("DDL # %d\n", iDDL));
380
381         AliMpDDL* ddl = GetDDL(iDDL);
382
383         // loop over DE in the given DDL
384         for (Int_t detElemIdx = 0; detElemIdx < ddl->GetNofDEs(); ++detElemIdx) {
385
386             Int_t detElemId = ddl->GetDEId(detElemIdx);
387
388             AliMpDetElement* detElement = GetDetElement(detElemId);
389
390             AliMp::StationType stationType = AliMpDEManager::GetStationType(detElemId);
391
392
393             // list of manu per DE on both cathode
394             TList manuList;
395             for ( Int_t cath = 0; cath < 2 ; ++cath ) {
396                 const AliMpVSegmentation* seg
397                 = AliMpSegmentation::Instance()->GetMpSegmentation(detElemId,AliMp::GetCathodType(cath));
398
399                 AliMp::PlaneType planeType = detElement->GetPlaneType(AliMp::GetCathodType(cath));
400
401                 TArrayI manus;
402                 seg->GetAllElectronicCardIDs(manus);
403
404                 // filling TList manu
405                 for ( Int_t im = 0; im < manus.GetSize(); ++im ) {
406
407                     AliMpIntPair* manu = 0x0;
408                     if( stationType == AliMp::kStation345)
409                         manu = new AliMpIntPair((manus[im] & manuMask), planeType, kTRUE); //remove offset for NB
410                     else
411                         manu = new AliMpIntPair(manus[im], planeType, kTRUE); //keep offset for NB
412
413                     manuList.Add(manu);
414
415                     detElement->AddManu(manus[im]);
416                 }
417             }// cathode
418
419             manuList.Sort(); // sort regardless B or NB plane
420
421             // filling manu to the corresponding buspatch
422             for (Int_t iEntry = 0; iEntry < manuList.GetEntries(); ++iEntry) {
423
424                 AliMpIntPair* manuPtr = (AliMpIntPair*)manuList.At(iEntry);
425
426                 Int_t manuId = manuPtr->GetFirst();
427                 Int_t pos    = GetBusPatchIndex(detElemId, manuId);
428
429                 if (pos > detElement->GetNofBusPatches()) {
430                     AliError(Form("pos greater %d than size %d manuId %d detElemId %d \n",
431                                   pos, detElement->GetNofBusPatches(), manuId, detElemId));
432                     return false;
433                 }
434
435                 // get buspatch and fill manus
436                 Int_t busPatchId = detElement->GetBusPatchId(pos);
437                 AliMpBusPatch* busPatch = GetBusPatch(busPatchId);
438
439                 if( stationType == AliMp::kStation345) {
440
441                     if (manuPtr->GetSecond())
442                         busPatch->AddManu(manuId+manuMask+1); // add offset again after sorted
443                     else
444                         busPatch->AddManu(manuId);
445
446                 } else {
447
448                     busPatch->AddManu(manuId);
449
450                 }
451             }
452
453             manuList.Delete();
454
455             if (AliDebugLevel() == 3) {
456
457                 // print out for checking
458                 for(Int_t pos = 0; pos < detElement->GetNofBusPatches(); ++pos) {
459                     Int_t busPatchId = detElement->GetBusPatchId(pos);
460                     AliMpBusPatch* busPatch = GetBusPatch(busPatchId);
461                     printf("BusPatch: %d\n", busPatch->GetId());
462                     for (Int_t iEntry = 0; iEntry < busPatch->GetNofManus(); ++iEntry)
463                         printf("manu Id: %d\n", busPatch->GetManuId(iEntry));
464                 }
465             }
466
467         } // detection element loop
468     }// DDL loop
469
470     return true;
471 }
472
473 //______________________________________________________________________________
474 Bool_t AliMpDDLStore::ReadBusPatchSpecial()
475 {
476 /// Read file with bus patches with a special order of manus
477 /// and reset the manus arrays filled via SetManu function
478
479   istream& in 
480     = fkDataStreams.
481         CreateDataStream(AliMpFiles::BusPatchSpecialFilePath());
482
483   char line[255];
484
485   while ( in.getline(line,255) ) {
486
487     if ( line[0] == '#' ) continue;
488
489     TString tmp(AliMpHelper::Normalize(line));
490     TObjArray* stringList = tmp.Tokenize(TString(" "));
491
492     TString sKey = ((TObjString*)stringList->At(0))->GetString();
493     
494     TString sDDL = ((TObjString*)stringList->At(1))->GetString();
495     TArrayI ddlList;
496     AliMpHelper::DecodeName(sDDL,';',ddlList);
497
498     TString sBusPatch = ((TObjString*)stringList->At(2))->GetString();
499     TArrayI busPatchList;
500     AliMpHelper::DecodeName(sBusPatch,',',busPatchList);
501     
502     // Loop over DDL and Bus Patch
503     for (Int_t iDDL = 0; iDDL < ddlList.GetSize(); ++iDDL ) {
504       for (Int_t iBusPatch = 0; iBusPatch < busPatchList.GetSize(); ++iBusPatch) {
505         // Global bus patch ID
506         Int_t busPatchID 
507           = AliMpBusPatch::GetGlobalBusID(
508               busPatchList.At(iBusPatch), ddlList.At(iDDL));
509         
510         // Get this bus patch
511         AliMpBusPatch* busPatch = GetBusPatch(busPatchID);
512         if ( ! busPatch ) {
513           AliErrorStream() << "Bus patch " << busPatchID << " does not exist." << endl;
514           return kFALSE;
515         }
516      
517         if ( sKey == fgkRevertKeyword ) {
518           AliDebugStream(3)
519             << "Reverting readout of bus patch " << busPatchID  << endl;
520         
521           // Now revert the manus in this bus patch
522           busPatch->RevertReadout();     
523         }
524         else if ( sKey == fgkExplicitKeyword ) {
525         
526           busPatch->ResetReadout();
527
528           TString sManus = ((TObjString*)stringList->At(3))->GetString();
529           TArrayI manuList;
530           AliMpHelper::DecodeName(sManus,',',manuList);
531
532           AliDebugStream(3)
533             << "Reseting readout of bus patch " << busPatchID  
534             << "  manus: " << sManus.Data() << endl;
535
536           for (Int_t i = 0; i < manuList.GetSize(); i++) {
537             busPatch->AddManu(manuList.At(i));
538           }
539         }
540         else {             
541           AliErrorStream() << "Unrecognized key." << endl;
542           return kFALSE;
543         }
544       }
545     }
546   }
547   
548   delete &in;
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::GetStation12Type(idDE) == AliMq::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     istream& in 
587       = fkDataStreams.
588           CreateDataStream(AliMpFiles::BusPatchInfoFilePath());
589
590     char line[255];
591
592     for (Int_t iDDL = 0; iDDL < fgkNofDDLs; ++iDDL ) {
593         AliMpDDL* ddl = GetDDL(iDDL);
594
595         for (Int_t iBusPatch = 0; iBusPatch < ddl->GetNofBusPatches(); ++iBusPatch) {
596
597             do {
598                 if (!in.getline(line,255)) {
599                     AliWarning(Form("Wrong size in bus patch length file; index %d DDL %d",
600                                     iBusPatch, iDDL));
601                     return false;
602                 }
603             } while(line[0] == '#');
604
605             TString tmp(AliMpHelper::Normalize(line));
606
607             TObjArray* stringList = tmp.Tokenize(TString(" "));
608
609             // Crocus label
610             TString crLabel    = ((TObjString*)stringList->At(0))->GetString();
611             Int_t pos          = crLabel.First('-');
612             tmp                = crLabel(pos-2, crLabel.Length()-pos+2);
613             TArrayI list;
614             AliMpHelper::DecodeName(tmp.Data(), '-', list);
615             
616             Int_t localDDLId  = list[0];
617             Int_t frtId       = list[1] - 1; // begin at zero ! 
618             Int_t localBusId  = list[2];
619
620             // Add FRT number for given ddl if not present
621             if ( !ddl->HasFrtId(frtId) )
622               ddl->AddFrt(frtId);
623
624             // BP & translator label
625             TString label      = ((TObjString*)stringList->At(1))->GetString();
626             TString transLabel = ((TObjString*)stringList->At(2))->GetString();
627
628             // BP length
629             TString sLength    = ((TObjString*)stringList->At(3))->GetString();
630             Float_t length     = sLength.Atof();
631
632             delete stringList;
633                        
634             if (localBusId != iBusPatch + 1)
635                AliWarning(Form("Wrong local buspatch id %d instead of %d", iBusPatch+1, localBusId));
636                
637             if(localDDLId != ddl->GetId()+1)
638                 AliWarning(Form("Wrong local DDL id %d instead of %d", ddl->GetId()+1, localDDLId));
639
640             Int_t busPatchId = ddl->GetBusPatchId(iBusPatch);
641             AliMpBusPatch* busPatch = GetBusPatch(busPatchId);
642             busPatch->SetCableLength(length);
643             busPatch->SetCableLabel(label);
644             busPatch->SetTranslatorLabel(transLabel);
645             busPatch->SetFrtId(frtId);
646
647         }
648     }
649     
650     delete &in;
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 void AliMpDDLStore::PrintAllManu() const {
840     /// Print all manu Ids and their serial numbers sorted by detection element
841     /// and bus patch.                                                            \n
842     /// As serial manu numbers are filled in a different way than manu Ids this
843     /// printing allows to check that both ways are consistent
844
845     // Loop over DE
846     AliMpDEIterator it;
847     for ( it.First(); ! it.IsDone(); it.Next() ) {
848         AliMpDetElement* de = it.CurrentDE();
849         cout << "DE: " << de->GetId() << endl;
850
851         // Loop over bus patches in this DE
852         for ( Int_t i=0; i< de->GetNofBusPatches(); ++i ) {
853
854             AliMpBusPatch* busPatch = GetBusPatch(de->GetBusPatchId(i));
855             cout << "  busPatch: " << busPatch->GetId() << endl;
856
857             cout << "    Manu       : ";
858             for ( Int_t j=0; j<busPatch->GetNofManus(); ++j ) {
859                 cout << std::setw(6) << busPatch->GetManuId(j) << " ";
860             }
861             cout << endl;
862
863             if ( AliMpManuStore::Instance(kFALSE) ) {
864               cout << "    Manu serial: ";
865               for ( Int_t k=0; k<busPatch->GetNofManus(); ++k ) {
866                 cout << std::setw(6) 
867                      << AliMpManuStore::Instance()
868                         ->GetManuSerial(de->GetId(), busPatch->GetManuId(k)) << " ";
869               }
870               cout << endl;
871             }  
872         }
873     }
874 }
875
876 //______________________________________________________________________________
877 Int_t  AliMpDDLStore::GetNextDEfromLocalBoard(Int_t localBoardId, Int_t chamberId ) const {
878     /// return the next detection element in line
879
880     AliMpLocalBoard* localBoard  =  GetLocalBoard(localBoardId);
881
882     TString name(localBoard->GetName());
883
884     Int_t line = localBoard->GetPosition().GetFirst();
885     ++line;
886
887     name.Replace(4,1,Form("%d", line));
888
889     Int_t nextLocalId;
890     if ((nextLocalId = GetLocalBoardId(name)))
891         return GetDEfromLocalBoard(nextLocalId, chamberId);
892     else
893         return 0;
894
895     return 0;
896 }
897
898 //______________________________________________________________________________
899 Int_t  AliMpDDLStore::GetPreviousDEfromLocalBoard(Int_t localBoardId, Int_t chamberId) const {
900     /// return the previous detection element in line
901
902     AliMpLocalBoard* localBoard  =  GetLocalBoard(localBoardId);
903
904     TString name(localBoard->GetName());
905
906     Int_t line = localBoard->GetPosition().GetFirst();
907     --line;
908
909     name.Replace(4,1,Form("%d", line));
910
911     Int_t prevLocalId;
912     if ((prevLocalId = GetLocalBoardId(name)))
913         return GetDEfromLocalBoard(prevLocalId, chamberId);
914     else
915         return 0;
916
917 }
918
919 //______________________________________________________________________________
920 void AliMpDDLStore::SetRegionalTrigger(const AliMpRegionalTrigger& regionalTrigger)
921 {
922 /// Replace the existing regional trigger with the given one
923
924   fRegionalTrigger = regionalTrigger;
925   
926   // Remove the existing trigger DDLsf
927   fDDLs.RemoveAt(fgkNofDDLs+1);
928   fDDLs.RemoveAt(fgkNofDDLs);
929   
930   // Set new trigger DDLs from new regional trigger
931   SetTriggerDDLs();
932 }  
933
934
935 //______________________________________________________________________________
936 TIterator* 
937 AliMpDDLStore::CreateBusPatchIterator() const
938 {
939 /// Create the iterator over bus patches
940
941   return fBusPatches.CreateIterator();
942 }