]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONTriggerCrateStore.cxx
Do not unload gAlice, it is needed until the end of the simulation run
[u/mrichter/AliRoot.git] / MUON / AliMUONTriggerCrateStore.cxx
1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 *                                                                        *
4 * Author: The ALICE Off-line Project.                                    *
5 * Contributors are mentioned in the code where appropriate.              *
6 *                                                                        *
7 * Permission to use, copy, modify and distribute this software and its   *
8 * documentation strictly for non-commercial purposes is hereby granted   *
9 * without fee, provided that the above copyright notice appears in all   *
10 * copies and that both the copyright notice and this permission notice   *
11 * appear in the supporting documentation. The authors make no claims     *
12 * about the suitability of this software for any purpose. It is          *
13 * provided "as is" without express or implied warranty.                  *
14 **************************************************************************/
15
16 // $Id$
17
18 #include "AliMUONTriggerCrateStore.h"
19
20 #include "AliMUONTriggerCrate.h"
21 #include "AliMUONLocalTriggerBoard.h"
22 #include "AliMUONRegionalTriggerBoard.h"
23 #include "AliMpExMap.h"
24 #include "TString.h"
25 #include "TSystem.h"
26 #include "AliLog.h"
27 #include "Riostream.h"
28
29 /// 
30 /// \class AliMUONTriggerCrateStore
31 /// 
32 /// A container of trigger crate objects that offers iteration
33 /// over both the crates themselves and the local boards they contain
34 ///
35 /// \author Laurent Aphecetche
36
37 ClassImp(AliMUONTriggerCrateStore)
38
39 //_____________________________________________________________________________
40 AliMUONTriggerCrateStore::AliMUONTriggerCrateStore()
41 : TObject(),
42 fCrates(0x0),
43 fLocalBoards(0x0),
44 fCrateIterator(0x0),
45 fLBIterator(0x0),
46 fCurrentCrate(0x0),
47 fCurrentLocalBoard(-1)
48 {
49   // ctor
50 }
51
52 //_____________________________________________________________________________
53 AliMUONTriggerCrateStore::~AliMUONTriggerCrateStore()
54 {
55     // dtor
56   delete fCrateIterator;
57   delete fLBIterator;
58   delete fCrates;
59   delete fLocalBoards;
60 }
61
62 //_____________________________________________________________________________
63 void 
64 AliMUONTriggerCrateStore::AddCrate(const char *name)
65 {
66   /// create and add a crate to our map
67   if (!fCrates)
68   {
69     AliError("Object not properly initialized");
70     return;
71   }
72
73   AliDebug(1,Form("Adding crate %s",name));
74   TObject* there = fCrates->GetValue(name);
75   if (there)
76   {
77     AliError(Form("Cannot add crate %s because it's already there !",name));
78   }
79   else
80   {
81     fCrates->Add(name,new AliMUONTriggerCrate(name,17));
82   }
83 }
84
85 //_____________________________________________________________________________
86 AliMUONLocalTriggerBoard* 
87 AliMUONTriggerCrateStore::LocalBoard(Int_t boardNumber) const
88 {
89   /// return a board by number
90   
91   if ( !fLocalBoards )
92   {
93     AliError("Object not properly initialized");
94     return 0x0;
95   }
96
97   return static_cast<AliMUONLocalTriggerBoard*>(fLocalBoards->GetValue(boardNumber));
98 }
99
100 //_____________________________________________________________________________
101 AliMUONTriggerCrate* 
102 AliMUONTriggerCrateStore::Crate(const char *name) const
103 {
104   /// return a crate by name
105   if ( !fCrates )
106   {
107     AliError("Object not properly initialized");
108     return 0x0;
109   }
110   return static_cast<AliMUONTriggerCrate*>(fCrates->GetValue(name));
111 }
112
113 //_____________________________________________________________________________
114 void
115 AliMUONTriggerCrateStore::FirstCrate()
116 {
117   /// initialize iteration
118   if ( !fCrates )
119   {
120     AliError("Object not properly initialized");
121     return;
122   }
123   if (!fCrateIterator)
124   {
125     fCrateIterator = new TExMapIter(fCrates->GetIterator());
126   }
127   fCrateIterator->Reset();
128 }
129
130 //_____________________________________________________________________________
131 void
132 AliMUONTriggerCrateStore::FirstLocalBoard()
133 {
134   /// Initialize iterator on local boards.
135   /// Please note that we're not using directly the FirstCrate() and
136   /// NextCrate() methods here to avoid mix and match between crate iterator
137   /// and local board iterator
138   fCurrentCrate = 0x0;
139   fCurrentLocalBoard = 0;
140
141   if ( !fLBIterator ) 
142   {
143     fLBIterator = new TExMapIter(fCrates->GetIterator());
144   }
145   fLBIterator->Reset();
146   Long_t key, value;
147   Bool_t ok = fLBIterator->Next(key,value);
148   if ( ok )
149   {
150     fCurrentCrate = reinterpret_cast<AliMUONTriggerCrate*>(value);
151     fCurrentLocalBoard = 1;
152   }
153 }
154
155 //_____________________________________________________________________________
156 AliMUONTriggerCrate*
157 AliMUONTriggerCrateStore::NextCrate()
158 {
159   /// Return the next crate in iteration, or 0 if iteration is ended.
160   if (!fCrateIterator) return 0x0;
161   
162   Long_t key, value;
163   Bool_t ok = fCrateIterator->Next(key,value);
164   if (ok)
165   {
166     return reinterpret_cast<AliMUONTriggerCrate*>(value);
167   }
168   else
169   {
170     return 0x0;
171   }
172 }
173
174 //_____________________________________________________________________________
175 AliMUONLocalTriggerBoard*
176 AliMUONTriggerCrateStore::NextLocalBoard()
177 {  
178   /// Return the next local board in iteration, or 0 if iteration is ended.
179   if ( !fLBIterator ) return 0x0;
180
181   if ( fCurrentLocalBoard >= fCurrentCrate->Boards()->GetLast() +1)
182 //  if ( fCurrentLocalBoard >= fCurrentCrate->Boards()->GetLast() )
183   {
184     // try to go to next crate, if some are left
185     Long_t key, value;
186     Bool_t ok = fLBIterator->Next(key,value);
187     if ( ok )
188     {
189       fCurrentCrate = reinterpret_cast<AliMUONTriggerCrate*>(value);
190       fCurrentLocalBoard = 1;
191     }
192     else
193     {
194       fCurrentLocalBoard = 0;
195       return 0x0;
196     }
197   }
198
199   AliMUONLocalTriggerBoard* lb = static_cast<AliMUONLocalTriggerBoard*>
200     (fCurrentCrate->Boards()->At(fCurrentLocalBoard));
201   
202   ++fCurrentLocalBoard;
203   
204   return lb;
205 }
206
207 //_____________________________________________________________________________
208 Int_t
209 AliMUONTriggerCrateStore::NumberOfCrates() const
210 {
211   /// Number of crates we're holding
212   if ( fCrates ) return fCrates->GetSize();
213   return 0;
214 }
215
216 //_____________________________________________________________________________
217 Int_t
218 AliMUONTriggerCrateStore::NumberOfLocalBoards() const
219 {
220   /// Number of local boards we're holding
221   if ( fLocalBoards ) return fLocalBoards->GetSize();
222   return 0;
223 }
224
225 //_____________________________________________________________________________
226 void
227 AliMUONTriggerCrateStore::ReadFromFile(const char* file)
228 {
229   /// Read crate and local board information from file.
230   fCrates = new AliMpExMap(kTRUE);
231   fLocalBoards = new AliMpExMap(kFALSE);
232   
233   ifstream myInputFile(gSystem->ExpandPathName(file), ios::in);
234   
235   string sLine, sValue;
236   
237   if ( !myInputFile ) 
238   {
239     AliError("TRIGGER ELECTRONICS CONFIGURATION FILE COULD NOT BE OPENED");
240   }
241   else
242   {
243     while (getline(myInputFile,sLine))
244     {
245       if (sLine.empty()) continue; // Ignore empty lines
246       else
247       {
248         const Int_t kMaxfields = 15; char **fields = new char*[kMaxfields];
249         
250         char s[100]; 
251         
252         if (sLine.find("Board",0) != string::npos) 
253         {   
254           strcpy(s,sLine.c_str());
255           
256           Int_t numlines = 0;
257           
258           for (char *token = strtok(s, " ");
259                token != NULL;
260                token = strtok(NULL, " "))
261           {
262             fields[numlines] = new char[strlen(token)+1];
263             strcpy(fields[numlines++],token);
264           }
265           
266           char str[10]; strcpy(str, fields[6]); strcat(str, fields[7]);
267           
268           AliMUONTriggerCrate *crate = Crate(str); 
269           
270           if (!crate) 
271           {
272             AddCrate(str); crate = Crate(str);
273             
274             AliMUONRegionalTriggerBoard *rboard = new AliMUONRegionalTriggerBoard();
275             crate->AddBoard(rboard, 0);
276           }               
277           
278           //             CONVENTION: SLOT 0 HOLDS THE REGIONAL BOARD
279           Int_t sl = atoi(fields[10]);
280           
281 //          AliMUONTriggerLut* lut = calibData->TriggerLut();
282           
283           AliMUONLocalTriggerBoard *board = 
284             new AliMUONLocalTriggerBoard(fields[4], sl, 0x0);
285           
286                                         if (strcmp(fields[1],"nn")) 
287                                         {
288                                                 Int_t sboard = atoi(fields[1]);
289             
290                                                 board->SetNumber(sboard);
291             fLocalBoards->Add(sboard,board);
292             
293 //                                              fCrateMap[sboard-1] = new char[strlen(str)+1]; strcpy(fCrateMap[sboard-1], str);
294 //                                              fBoardMap[sboard-1] = sl;
295                                         }
296           
297                                         board->SetCrate(str);
298           
299           crate->AddBoard(board, sl);
300           
301           while (getline(myInputFile,sLine)) if (sLine.find("transv",0) != string::npos) break;
302           
303           strcpy(s,sLine.c_str());
304           
305           for (char *token = strtok(s, " ");
306                token != NULL;
307                token = strtok(NULL, " ")) if (!strcmp(token,"NONE")) board->SetTC(kFALSE);
308           
309           while (getline(myInputFile,sLine)) if (sLine.find("Switch",0) != string::npos) break;
310           
311           while (getline(myInputFile,sLine)) if (!sLine.empty()) break;   
312           
313           strcpy(s,sLine.c_str());
314           
315           Int_t lines = 0;
316           
317           for (char *token = strtok(s, " ");
318                token != NULL;
319                token = strtok(NULL, " ")) board->SetSwitch(lines++, atoi(token));
320           
321           for (Int_t i = 0; i<numlines; i++) 
322             if (fields[i]) {delete [] fields[i]; fields[i] = 0;}
323               
324               delete [] fields; fields = 0;
325         }
326       }
327     }
328   }
329 }