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