]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/ESD/AliTriggerConfiguration.cxx
Fix for PF, all information saved in ESD
[u/mrichter/AliRoot.git] / STEER / ESD / AliTriggerConfiguration.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 ///////////////////////////////////////////////////////////////////////////////
17 //
18 // This class which defines defines the Trigger Configuration 
19 //
20 // Trigger Configuration defines the trigger setup for a particular run
21 // We have default configurations for each running mode (Pb-Pb, p-p, p-A, Calibration, etc).
22 // It keep:
23 //   All the information conained in the CTP configuration file used
24 //   online during the data taking
25 //
26 // Configurations could be created and stored in local file.
27 // By default the configuration is loaded from the corresponding GRP entry
28 // inside the OCDB. There one can have one and only one configuration per run.
29 //
30 //   Example how to create a Trigger Configuration:
31 //
32 //   AliTriggerConfiguration config( "TEST", "Test Configuration" );
33 //
34 //   // Define a Cluster Detector
35 //   config.AddDetectorCluster( "VZERO ZDC MUON" );
36 //
37 //   // Define the trigger conditions (see AliTriggerCondition.cxx)
38 //   config.AddCondition( "VZERO_TEST1_L0 & MUON_SPlus_LPt_L0 & ZDC_TEST2_L0", // condition
39 //                         "VO1_M1_ZDC2",      // short name
40 //                         "Dummy",            // short description
41 //                          0x0100 );          // class mask (set one bit)
42 //
43 //   config.AddCondition( "VZERO_TEST2_L0 & MUON_SMinus_HPt_L0 & ZDC_TEST1_L0",
44 //                         "VO2_M3_ZDC1",
45 //                         "Dummy",
46 //                          0x0200 );
47 //
48 //   config.AddCondition( "VZERO_TEST3_L0 | MUON_Unlike_LPt_L0 | ZDC_TEST3_L0",
49 //                         "VO3_M1_ZDC3",
50 //                         "Dummy",
51 //                          0x0400 );
52 //   config.CheckInputsConditions("Config.C");
53 //   config.Print();
54 //
55 //   // save the configuration to file 
56 //   // (default file name $ALICE_ROOT/data/triggerConfigurations.root)
57 //   config.WriteConfiguration(); or config.WriteConfiguration( filename );
58 //
59 ///////////////////////////////////////////////////////////////////////////////
60 #include <Riostream.h>
61
62 #include <TCint.h>
63 #include <TFile.h>
64 #include <TKey.h>
65 #include <TObjArray.h>
66 #include <TObjString.h>
67 #include <TObject.h>
68 #include <TROOT.h>
69 #include <TString.h>
70 #include <TSystem.h>
71 #include <TMath.h>
72
73 #include "AliLog.h"
74 #include "AliTriggerBCMask.h"
75 #include "AliTriggerClass.h"
76 #include "AliTriggerCluster.h"
77 #include "AliTriggerConfiguration.h"
78 #include "AliTriggerDescriptor.h"
79 #include "AliTriggerInput.h"
80 #include "AliTriggerInteraction.h"
81 #include "AliTriggerPFProtection.h"
82
83 ClassImp(AliTriggerConfiguration)
84
85 const TString AliTriggerConfiguration::fgkConfigurationFileName("/data/triggerConfigurations.root");
86
87 //_____________________________________________________________________________
88 AliTriggerConfiguration::AliTriggerConfiguration():
89   TNamed(),
90   fInputs(),
91   fInteractions(),
92   fFunctions(),
93   fPFProtections(),
94   fMasks(),
95   fDescriptors(),
96   fClusters(),
97   fClasses(),
98   fVersion(0)
99 {
100   // Default constructor
101 }
102
103 //_____________________________________________________________________________
104 AliTriggerConfiguration::AliTriggerConfiguration( TString & name, TString & description ):
105   TNamed( name, description ),
106   fInputs(),
107   fInteractions(),
108   fFunctions(),
109   fPFProtections(),
110   fMasks(),
111   fDescriptors(),
112   fClusters(),
113   fClasses(),
114   fVersion(0)
115 {
116   // Constructor
117 }
118
119 //_____________________________________________________________________________
120 AliTriggerConfiguration::~AliTriggerConfiguration() 
121
122   // Destructor
123   fInputs.SetOwner();
124   fInputs.Delete();
125   fInteractions.SetOwner();
126   fInteractions.Delete();
127   fFunctions.SetOwner();
128   fFunctions.Delete();
129   fPFProtections.SetOwner();
130   fPFProtections.Delete();
131   fMasks.SetOwner();
132   fMasks.Delete();
133   fDescriptors.SetOwner();
134   fDescriptors.Delete();
135   fClusters.SetOwner(); 
136   fClusters.Delete(); 
137   fClasses.SetOwner(); 
138   fClasses.Delete(); 
139 }
140
141 //_____________________________________________________________________________
142 Bool_t AliTriggerConfiguration::AddInput( AliTriggerInput* input )
143 {
144   // Add a trigger input to
145   // the list of the trigger inputs
146   if (fInputs.GetEntries() < kNMaxInputs) {
147     fInputs.AddLast( input );
148     return kTRUE;
149   }
150   else {
151     AliError("CTP can handle up to 60 inputs ! Impossible to add the required input !");
152     return kFALSE;
153   }
154 }
155
156 //_____________________________________________________________________________
157 AliTriggerInput* AliTriggerConfiguration::AddInput( TString &name, TString &det,
158                                                     UChar_t level, UInt_t signature,
159                                                     UChar_t number )
160 {
161   // Add a trigger input to
162   // the list of the trigger inputs
163   AliTriggerInput *input = new AliTriggerInput(name,det,level,signature,number);
164   if (!AddInput(input)) {
165     delete input;
166     return NULL;
167   }
168   else
169     return input;
170 }
171
172 //_____________________________________________________________________________
173 AliTriggerInteraction* AliTriggerConfiguration::AddInteraction(TString &name, TString &logic)
174 {
175   // Add a trigger interaction object to
176   // the list of the trigger interactions
177   AliTriggerInteraction *interact = new AliTriggerInteraction(name,logic);
178   if (!AddInteraction(interact)) {
179     delete interact;
180     return NULL;
181   }
182   else
183     return interact;
184 }
185
186 //_____________________________________________________________________________
187 Bool_t  AliTriggerConfiguration::AddInteraction(AliTriggerInteraction *interact)
188 {
189   // Add a trigger interaction object to
190   // the list of the trigger interactions
191   if (fInteractions.GetEntries() < kNMaxInteractions) {
192     if (interact->CheckInputs(fInputs)) {
193       fInteractions.AddLast( interact );
194       return kTRUE;
195     }
196     else
197       AliError("Invalid interaction ! Impossible to add it !");
198   }
199   else
200     AliError("CTP can handle up to 2 interactions ! Impossible to add the required interaction !");
201
202   return kFALSE;
203 }
204
205 //_____________________________________________________________________________
206 AliTriggerInteraction* AliTriggerConfiguration::AddFunction(TString &name, TString &logic)
207 {
208   // Add a trigger function object to
209   // the list of the trigger functions
210   AliTriggerInteraction *func = new AliTriggerInteraction(name,logic);
211   if (!AddFunction(func)) {
212     delete func;
213     return NULL;
214   }
215   else
216     return func;
217 }
218
219 //_____________________________________________________________________________
220 Bool_t  AliTriggerConfiguration::AddFunction(AliTriggerInteraction *func)
221 {
222   // Add a trigger function object to
223   // the list of the trigger functions
224   if (fFunctions.GetEntries() < kNMaxFunctions) {
225     if (func->CheckInputs(fInputs)) {
226       fFunctions.AddLast( func );
227       return kTRUE;
228     }
229     else
230       AliError("Invalid logical function ! Impossible to add it !");
231   }
232   else
233     AliError("CTP can handle up to 2 logical functions ! Impossible to add the required interaction !");
234
235   return kFALSE;
236 }
237
238 //_____________________________________________________________________________
239 Bool_t AliTriggerConfiguration::AddPFProtection( AliTriggerPFProtection* pfp )
240 {
241   // Add a trigger past-future protection object to
242   // the list of the trigger past-future protections
243   if (fPFProtections.GetEntries() < kNMaxPFProtections) {
244     //if (pfp->CheckInteractions(fInteractions)) {
245     if (1) {
246       fPFProtections.AddLast( pfp );
247       return kTRUE;
248     }
249     else
250       AliError("Invalid past-future protection ! Impossible to add it !");
251   }
252   else
253     AliError("CTP can handle up to 4 past-future protections ! Impossible to add the required protection !");
254
255   return kFALSE;
256 }
257
258 //_____________________________________________________________________________
259 AliTriggerBCMask* AliTriggerConfiguration::AddMask( TString &name, TString &mask )
260 {
261   // Add a trigger bunch-crossing mask object to
262   // the list of the trigger bunch-crossing masks
263   AliTriggerBCMask *bcmask = new AliTriggerBCMask(name,mask);
264   if (!AddMask(bcmask)) {
265     delete bcmask;
266     return NULL;
267   }
268   else
269     return bcmask;
270 }
271
272 //_____________________________________________________________________________
273 Bool_t AliTriggerConfiguration::AddMask( AliTriggerBCMask* mask )
274 {
275   // Add a trigger bunch-crossing mask object to
276   // the list of the trigger bunch-crossing masks
277   if (fMasks.GetEntries() < (kNMaxMasks+1)) {  //+1 to account for NONE
278       fMasks.AddLast( mask );
279       return kTRUE;
280   }
281   else
282     AliError("CTP can handle up to 4 bunch-crossing masks ! Impossible to add the required mask !");
283
284   return kFALSE;
285 }
286
287 //_____________________________________________________________________________
288 AliTriggerCluster* AliTriggerConfiguration::AddCluster( TString &name, UChar_t index, TString &detectors)
289 {
290   // Add a trigger detector readout cluster to
291   // the list of the trigger clusters
292   AliTriggerCluster *clust = new AliTriggerCluster(name,index,detectors);
293   if (!AddCluster(clust)) {
294     delete clust;
295     return NULL;
296   }
297   else
298     return clust;
299
300 }
301
302 //_____________________________________________________________________________
303 Bool_t AliTriggerConfiguration::AddCluster( AliTriggerCluster* cluster )
304 {
305   // Add a trigger detector readout cluster to
306   // the list of the trigger clusters
307   if (fClusters.GetEntries() < kNMaxClusters) {
308     TString dets(cluster->GetDetectorsInCluster());
309     if (!(dets.IsNull())) {
310       fClusters.AddLast( cluster );
311       return kTRUE;
312     }
313     else
314       AliError("Empty trigger cluster ! Impossible to add it !");
315   }
316   else
317     AliError("CTP can handle up to 6 different detector clusters ! Impossible to add the required cluster !");
318
319   return kFALSE;
320 }
321
322 //_____________________________________________________________________________
323 TString AliTriggerConfiguration::GetActiveDetectors() const
324 {
325   // Return an string with all active detector
326   // from each cluster
327
328    TString activeDet = "";
329
330    Int_t nclus = fClusters.GetEntriesFast();
331    if( !nclus ) return activeDet;
332    
333    for( Int_t j=0; j<nclus; ++j ) {
334       TString detStr = ((AliTriggerCluster*)fClusters.At(j))->GetDetectorsInCluster();
335       TObjArray* det = detStr.Tokenize(" ");
336       Int_t ndet = det->GetEntriesFast();
337       for( Int_t k=0; k<ndet; ++k ) {
338          if( activeDet.Contains( ((TObjString*)det->At(k))->String() ) )continue;
339          activeDet.Append( " " );
340          activeDet.Append( ((TObjString*)det->At(k))->String() );
341       }
342    }
343    return activeDet;
344 }
345
346 //_____________________________________________________________________________
347 TString AliTriggerConfiguration::GetTriggeringDetectors() const
348 {
349   // Return an string with all detectors
350   // used for triggering
351
352    TString trDet = "";
353
354    Int_t ninputs = fInputs.GetEntriesFast();
355    if( !ninputs ) return trDet;
356    
357    for( Int_t j=0; j<ninputs; j++ ) {
358       TString detStr = ((AliTriggerInput*)fInputs.At(j))->GetDetector();
359       if( trDet.Contains( detStr ) ) continue;
360       trDet.Append( " " );
361       trDet.Append( detStr );
362    }
363    return trDet;
364 }
365
366 //_____________________________________________________________________________
367 TString AliTriggerConfiguration::GetTriggeringModules() const
368 {
369    // Return an string with all detectors (modules in the AliRoot
370   // simulation sense) used for triggering
371
372    TString trDet = "";
373
374    Int_t ninputs = fInputs.GetEntriesFast();
375    if( !ninputs ) return trDet;
376    
377    for( Int_t j=0; j<ninputs; j++ ) {
378       TString detStr = ((AliTriggerInput*)fInputs.At(j))->GetModule();
379       if( trDet.Contains( detStr ) ) continue;
380       trDet.Append( " " );
381       trDet.Append( detStr );
382    }
383    return trDet;
384 }
385
386 //_____________________________________________________________________________
387 AliTriggerDescriptor* AliTriggerConfiguration::AddDescriptor( TString &name, TString &cond )
388 {
389   // Add a trigger descriptor to
390   // the list of the trigger descriptors
391   AliTriggerDescriptor *desc = new AliTriggerDescriptor(name,cond);
392   if (!AddDescriptor(desc)) {
393     delete desc;
394     return NULL;
395   }
396   else
397     return desc;
398 }
399
400 //_____________________________________________________________________________
401 Bool_t AliTriggerConfiguration::AddDescriptor( AliTriggerDescriptor *desc )
402 {
403   // Add a trigger descriptor to
404   // the list of the trigger descriptors
405   if (fDescriptors.GetEntries() < kNMaxClasses) {
406     if (desc->CheckInputsAndFunctions(fInputs,fFunctions)) {
407       fDescriptors.AddLast( desc );
408       return kTRUE;
409     }
410     else
411       AliError("Invalid trigger desciptor ! Impossible to add it !");
412   }
413   else
414     AliError("CTP can handle up to 50 different descriptors ! Impossible to add the required descriptor !");
415
416   return kFALSE;
417 }
418
419 //_____________________________________________________________________________
420 Bool_t AliTriggerConfiguration::AddClass( AliTriggerClass *trclass )
421 {
422   // Add a trigger class to
423   // the list of the trigger classes
424   if (fClasses.GetEntries() < kNMaxClasses) {
425     if (trclass->CheckClass(this)) {
426       fClasses.AddLast( trclass );
427       return kTRUE;
428     }
429     else
430       AliError("Invalid trigger class ! Impossible to add it !");
431   }
432   else
433     AliError("CTP can handle up to 50 different classes ! Impossible to add the required class !");
434
435   return kFALSE;
436 }
437
438 //_____________________________________________________________________________
439 AliTriggerClass *AliTriggerConfiguration::AddClass( TString &name, UChar_t index,
440                                                     AliTriggerDescriptor *desc, AliTriggerCluster *clus,
441                                                     AliTriggerPFProtection *pfp, AliTriggerBCMask *mask,
442                                                     UInt_t prescaler, Bool_t allrare)
443 {
444   // Add a trigger class to
445   // the list of the trigger classes
446   if (!fDescriptors.FindObject(desc)) {
447     AliError("Invalid descriptor ! Impossible to add the class !");
448     return NULL;
449   }
450   if (!fClusters.FindObject(clus)) {
451     AliError("Invalid cluster ! Impossible to add the class !");
452     return NULL;
453   }
454   if (!fPFProtections.FindObject(pfp)) {
455     AliError("Invalid past-future protection ! Impossible to add the class !");
456     return NULL;
457   }
458   if (!fMasks.FindObject(mask)) {
459     AliError("Invalid bunch-crossing mask ! Impossible to add the class !");
460     return NULL;
461   }
462   AliTriggerClass* trclass = new AliTriggerClass( name,index,desc,clus,pfp,mask,prescaler,allrare );
463   if (!AddClass(trclass)) {
464     delete trclass;
465     return NULL;
466   }
467   else
468     return trclass;
469 }
470
471 //_____________________________________________________________________________
472 AliTriggerClass *AliTriggerConfiguration::AddClass( TString &name, UChar_t index,
473                                                     TString &desc, TString &clus,
474                                                     TString &pfp, TString &mask,
475                                                     UInt_t prescaler, Bool_t allrare)
476 {
477    // Add a new trigger class
478   if (!fDescriptors.FindObject(desc)) {
479     AliError("Invalid descriptor ! Impossible to add the class !");
480     return NULL;
481   }
482   if (!fClusters.FindObject(clus)) {
483     AliError("Invalid cluster ! Impossible to add the class !");
484     return NULL;
485   }
486   if (!fPFProtections.FindObject(pfp)) {
487     AliError("Invalid past-future protection ! Impossible to add the class !");
488     return NULL;
489   }
490   if (!fMasks.FindObject(mask)) {
491     AliError("Invalid bunch-crossing mask ! Impossible to add the class !");
492     return NULL;
493   }
494   AliTriggerClass* trclass = new AliTriggerClass( this, name,index,desc,clus,pfp,mask,prescaler,allrare );
495   if (!AddClass(trclass)) {
496     delete trclass;
497     return NULL;
498   }
499   else
500     return trclass;
501 }
502
503 //_____________________________________________________________________________
504 Bool_t AliTriggerConfiguration::ProcessConfigurationLine(const char* line, Int_t& level)
505 {
506     // processes one line of configuration
507
508      TString strLine(line);
509
510      if (strLine.BeginsWith("#")) return kTRUE;
511      if (strLine.BeginsWith("PARTITION:")) {
512        strLine.ReplaceAll("PARTITION:","");
513        SetName(strLine.Data());
514        return kTRUE;
515      }
516      if (strLine.BeginsWith("VERSION:")) {
517        strLine.ReplaceAll("VERSION:","");
518        fVersion = strLine.Atoi();
519        return kTRUE;
520      }
521      if (strLine.BeginsWith("INPUTS:")) {
522        level = 1;
523        return kTRUE;
524      }
525      if (strLine.BeginsWith("INTERACTIONS:")) {
526        level = 2;
527        return kTRUE;
528      }
529      if (strLine.BeginsWith("DESCRIPTORS:")) {
530        level = 3;
531        return kTRUE;
532      }
533      if (strLine.BeginsWith("CLUSTERS:")) {
534        level = 4;
535        return kTRUE;
536      }
537      if (strLine.BeginsWith("PFS:")) {
538        level = 5;
539        return kTRUE;
540      }
541      if (strLine.BeginsWith("BCMASKS:")) {
542        level = 6;
543        return kTRUE;
544      }
545      if (strLine.BeginsWith("CLASSES:")) {
546        level = 7;
547        return kTRUE;
548      }
549
550      strLine.ReplaceAll("*",'!');
551      strLine.ReplaceAll("~",'!');
552      TObjArray *tokens = strLine.Tokenize(" \t");
553      Int_t ntokens = tokens->GetEntriesFast();
554      if (ntokens == 0)
555      {
556        delete tokens;
557        return kTRUE;
558      }
559      switch (level) {
560      case 1:
561        // Read inputs
562        if (ntokens != 5) {
563          AliError(Form("Invalid trigger input syntax (%s)!",strLine.Data()));
564          return kFALSE;
565        }
566        AddInput(((TObjString*)tokens->At(0))->String(),
567                      ((TObjString*)tokens->At(1))->String(),
568                      ((TObjString*)tokens->At(2))->String().Atoi(),
569                      ((TObjString*)tokens->At(3))->String().Atoi(),
570                      ((TObjString*)tokens->At(4))->String().Atoi());
571        break;
572      case 2:
573        // Read interaction
574        if (ntokens != 2) {
575          AliError(Form("Invalid trigger interaction syntax (%s)!",strLine.Data()));
576          return kFALSE;
577        }
578        AddInteraction(((TObjString*)tokens->At(0))->String(),
579                            ((TObjString*)tokens->At(1))->String());
580        break;
581      case 3:
582        // Read logical functions and descriptors
583        if (ntokens < 2) {
584          if ((((TObjString*)tokens->At(0))->String().CompareTo("EMPTY") == 0) ||
585              (((TObjString*)tokens->At(0))->String().CompareTo("DTRUE") == 0) ||
586              (((TObjString*)tokens->At(0))->String().CompareTo("DEMPTY") == 0)) {
587            AddDescriptor(((TObjString*)tokens->At(0))->String(),
588                          strLine.ReplaceAll(((TObjString*)tokens->At(0))->String(),""));
589            break;
590          }
591          else {
592            AliError(Form("Invalid trigger descriptor syntax (%s)!",strLine.Data()));
593            return kFALSE;
594          }
595        }
596        if (((TObjString*)tokens->At(0))->String().BeginsWith("l0f")) {
597          // function
598          AddFunction(((TObjString*)tokens->At(0))->String(),
599                           strLine.ReplaceAll(((TObjString*)tokens->At(0))->String(),""));
600        }
601        else {
602          AddDescriptor(((TObjString*)tokens->At(0))->String(),
603                             strLine.ReplaceAll(((TObjString*)tokens->At(0))->String(),""));
604        }
605        break;
606      case 4:
607        {
608          if (ntokens < 2) {
609            AliError(Form("Invalid trigger cluster syntax (%s)!",strLine.Data()));
610            return kFALSE;
611          }
612          if (((TObjString*)tokens->At(1))->String().Atoi() <= 0) {
613            AliError(Form("Invalid trigger cluster syntax (%s)!",strLine.Data()));
614            return kFALSE;
615          }
616          TString strTemp;
617          for(Int_t i = 2; i < ntokens; i++) {
618            strTemp += ((TObjString*)tokens->At(i))->String();
619            strTemp += " ";
620          }
621          AddCluster(((TObjString*)tokens->At(0))->String(),
622                          ((TObjString*)tokens->At(1))->String().Atoi(),
623                          strTemp);
624        }
625        break;
626      case 5:
627        {
628          AliTriggerPFProtection *pfp = NULL;
629          if ((((TObjString*)tokens->At(0))->String().CompareTo("NONE") == 0) ||
630              (((TObjString*)tokens->At(0))->String().CompareTo("NOPF") == 0)) {
631            pfp = new AliTriggerPFProtection(((TObjString*)tokens->At(0))->String());
632          }
633          else {
634            if (ntokens == 10){ 
635             pfp = new AliTriggerPFProtection(((TObjString*)tokens->At(0))->String(),
636                                             ((TObjString*)tokens->At(1))->String(),
637                                             ((TObjString*)tokens->At(2))->String(),
638                                             ((TObjString*)tokens->At(3))->String());
639             pfp->SetNa1(((TObjString*)tokens->At(4))->String().Atoi());
640             pfp->SetNa2(((TObjString*)tokens->At(5))->String().Atoi());
641             pfp->SetNb1(((TObjString*)tokens->At(6))->String().Atoi());
642             pfp->SetNb2(((TObjString*)tokens->At(7))->String().Atoi());
643             pfp->SetTa(((TObjString*)tokens->At(8))->String().Atoi());
644             pfp->SetTb(((TObjString*)tokens->At(9))->String().Atoi());
645           }else if(ntokens == 13){
646             UInt_t pfdef[12];
647             for(Int_t i=0;i<12;i++){
648                TString ss(((TObjString*)tokens->At(i+1))->String());
649                ss.Remove(0,2);
650                UInt_t num=0;
651                for(Int_t j=ss.Length()-1;j>=0;j--){
652                 UInt_t nn=ss[j];
653                 if(nn<58)nn=nn-48; else nn=10+nn-97;
654                 num=num+(1<<(ss.Length()-1-j)*4)*nn;
655                 //cout << ss[j] << " " << nn << " "  << num << endl;
656                }
657                pfdef[i]=num;
658             }   
659             pfp = new AliTriggerPFProtection(((TObjString*)tokens->At(0))->String(),pfdef);
660           }else{
661              AliError(Form("Invalid trigger pfs syntax (%s)!",strLine.Data()));
662              return kFALSE;
663           }
664          }
665          AddPFProtection(pfp);
666        }
667        break;
668      case 6:
669          if (ntokens > 2) {
670            AliError(Form("Invalid trigger bcmasks syntax (%s)!",strLine.Data()));
671            return kFALSE;
672          }
673        if (((TObjString*)tokens->At(0))->String().CompareTo("NONE") == 0)
674        {         
675          AddMask(new AliTriggerBCMask(((TObjString*)tokens->At(0))->String()));
676        }
677        else {
678          AddMask(((TObjString*)tokens->At(0))->String(),
679                       ((TObjString*)tokens->At(1))->String());
680        }
681        break;
682      case 7:
683        {
684          if ((ntokens < 8) || (ntokens >10)) {
685            AliError(Form("Invalid trigger class syntax (%s)!",strLine.Data()));
686            return kFALSE;
687          }
688          AliTriggerClass *trclass = new AliTriggerClass(this,
689                                                         ((TObjString*)tokens->At(0))->String(),((TObjString*)tokens->At(1))->String().Atoi(),
690                                                         ((TObjString*)tokens->At(2))->String(),((TObjString*)tokens->At(3))->String(),
691                                                         ((TObjString*)tokens->At(4))->String(),((TObjString*)tokens->At(5))->String(),
692                                                         ((TObjString*)tokens->At(6))->String().Atoi(),(Bool_t)(((TObjString*)tokens->At(7))->String().Atoi()));
693          AddClass(trclass);
694        }
695      default:
696        break;
697      }
698      delete tokens;
699
700      return kTRUE;
701 }
702
703 //_____________________________________________________________________________
704 AliTriggerConfiguration* AliTriggerConfiguration::LoadConfiguration(TString & configuration)
705 {
706    // Load one pre-created Configurations from database/file that match
707    // with the input string 'configuration'
708    // Ej: "Pb-Pb" or "p-p-DIMUON CALIBRATION-CENTRAL-BARREL"
709   // By default the files are stored in GRP/CTP folder.
710   // The filename is constructed as: GRP/CTP/<configuration>.cfg
711
712    // Load the selected configuration
713   TString filename;
714   if (configuration.EndsWith(".cfg") ||
715       configuration.EndsWith(".shuttle")) {
716     filename = configuration;
717   }
718   else {
719     filename = gSystem->Getenv("ALICE_ROOT");
720     filename += "/GRP/CTP/";
721     filename += configuration;
722     filename += ".cfg";
723   }
724
725    if( gSystem->AccessPathName( filename.Data() ) ) {
726       AliErrorClass( Form( "file (%s) not found", filename.Data() ) );
727       return NULL;
728    }
729
730
731    ifstream *file = new ifstream ( filename.Data() );
732    if (!*file) {
733      AliErrorClass(Form("Error opening file (%s) !",filename.Data()));
734      file->close();
735      delete file;
736      return NULL;
737    }
738
739    AliTriggerConfiguration *cfg = new AliTriggerConfiguration();
740
741    Int_t level = 0;
742
743    TString strLine;
744    while (strLine.ReadLine(*file)) {
745      if (cfg->ProcessConfigurationLine(strLine, level) == kFALSE)
746      {
747         delete cfg;
748         cfg = 0;
749         break;
750      }
751    }
752
753    file->close();
754    delete file;
755
756    return cfg;
757 }
758
759 //_____________________________________________________________________________
760 AliTriggerConfiguration* AliTriggerConfiguration::LoadConfigurationFromString(const char* configuration)
761 {
762    // Loads configuration given as parameter <configuration>
763
764    if (!configuration)
765      return 0;
766
767    AliTriggerConfiguration *cfg = new AliTriggerConfiguration();
768
769    Int_t level = 0;
770
771    TObjArray* tokens = TString(configuration).Tokenize("\n");
772    for (Int_t i=0; i<tokens->GetEntries(); i++)
773    {
774      TObjString* str = dynamic_cast<TObjString*>(tokens->At(i));
775      if (!str)
776        continue;
777
778      if (cfg->ProcessConfigurationLine(str->String(), level) == kFALSE)
779      {
780         delete cfg;
781         cfg = 0;
782         break;
783      }
784    }
785
786    delete tokens;
787
788    return cfg;
789 }
790
791 //_____________________________________________________________________________
792 TObjArray* AliTriggerConfiguration::GetAvailableConfigurations( const char* filename )
793 {
794    // Return an array of configuration in the file
795
796    TString path;
797    if( !filename[0] ) {
798       path += gSystem->Getenv( "ALICE_ROOT" );
799       path += fgkConfigurationFileName;
800    }
801    else
802       path += filename;
803
804    if( gSystem->AccessPathName( path.Data() ) ) {
805       AliErrorGeneral( "AliTriggerConfiguration", Form( "file (%s) not found", path.Data() ) );
806       return NULL;
807    }
808
809    TObjArray* desArray = new TObjArray();
810
811    TFile file( path.Data(), "READ" );
812    if( file.IsZombie() ) {
813       AliErrorGeneral( "AliTriggerConfiguration", Form( "Error opening file (%s)", path.Data() ) );
814       return NULL;
815    }
816
817    file.ReadAll();
818
819    TKey* key;
820    TIter next( file.GetListOfKeys() );
821    while( (key = (TKey*)next()) ) {
822       TObject* obj = key->ReadObj();
823       if( obj->InheritsFrom( "AliTriggerConfiguration" ) ) {
824          desArray->AddLast( obj );
825       }
826    }
827    file.Close();
828
829    return desArray;
830 }
831
832 //_____________________________________________________________________________
833 void AliTriggerConfiguration::WriteConfiguration( const char* filename )
834 {
835    // Write the configuration
836    TString path;
837    if( !filename[0] ) {
838       path += gSystem->Getenv("ALICE_ROOT");
839       path += fgkConfigurationFileName;
840    }
841    else
842       path += filename;
843
844    TFile file( path.Data(), "UPDATE" );
845    if( file.IsZombie() ) {
846       AliErrorGeneral( "AliTriggerConfiguration", 
847                         Form( "Can't open file (%s)", path.Data() ) );
848       return;
849    }
850
851    Bool_t result = (Write( GetName(), TObject::kOverwrite ) != 0);
852    if( !result )
853       AliErrorGeneral( "AliTriggerConfiguration",
854                         Form( "Can't write entry to file <%s>!", path.Data() ) );
855    file.Close();
856 }
857
858 //_____________________________________________________________________________
859 Int_t AliTriggerConfiguration::GetClassIndexFromName(const char* className) const
860 {
861    //const TObjArray& classes = cfg->GetClasses();
862    Int_t nclasses = (Int_t)fClasses.GetEntriesFast();
863    for (Int_t i=0;i<nclasses;i++) {
864        AliTriggerClass* trgclass = (AliTriggerClass*)fClasses.At(i);
865        if (TString(trgclass->GetName()).CompareTo(className) == 0) { 
866           ULong64_t classmask = (ULong64_t)trgclass->GetMask();
867           return TMath::Nint(TMath::Log2(classmask))+1;
868        }
869    }
870    return -1;
871 }
872 //_____________________________________________________________________________
873 const char* AliTriggerConfiguration::GetClassNameFromIndex(Int_t classIndex) const
874 {
875    Int_t nclasses = (Int_t)fClasses.GetEntriesFast();
876    for (Int_t i=0;i<nclasses;i++) {
877        AliTriggerClass* trgclass = (AliTriggerClass*)fClasses.At(i);
878        ULong64_t classmask = (ULong64_t)trgclass->GetMask();
879        if (TMath::Nint(TMath::Log2(classmask))+1 == classIndex) return trgclass->GetName();
880    }
881    return 0;
882 }
883 //_____________________________________________________________________________
884 AliTriggerClass* AliTriggerConfiguration::GetTriggerClass(Int_t classIndex) const
885 {
886    Int_t nclasses = (Int_t)fClasses.GetEntriesFast();
887    for (Int_t i=0;i<nclasses;i++) {
888        AliTriggerClass* trgclass = (AliTriggerClass*)fClasses.At(i);
889        ULong64_t classmask = (ULong64_t)trgclass->GetMask();
890        if (TMath::Nint(TMath::Log2(classmask))+1 == classIndex) return trgclass;
891    }
892    return 0;
893 }
894 //_____________________________________________________________________________
895 void AliTriggerConfiguration::Reset()
896 {
897    for( Int_t j=0; j<fInputs.GetEntriesFast(); j++ )
898      ((AliTriggerInput*)fInputs.At(j))->Reset();
899
900    for( Int_t j=0; j<fClasses.GetEntriesFast(); j++ )
901      ((AliTriggerClass*)fClasses.At(j))->Reset();
902 }
903
904 //_____________________________________________________________________________
905 void AliTriggerConfiguration::Print( const Option_t*  ) const
906 {
907    // Print
908   cout << "#################################################" << endl;
909    cout << "Trigger Configuration:"  << endl;
910    cout << "  Name:              " << GetName() << endl; 
911    cout << "  Description:       " << GetTitle() << endl;
912    cout << "  Version:           " << GetVersion() << endl;
913    cout << "  Active Detectors:  " << GetActiveDetectors() << endl;
914    cout << "  Trigger Detectors: " << GetTriggeringDetectors() << endl;
915
916    cout << "#################################################" << endl;
917    fInputs.Print();
918    cout << "#################################################" << endl;
919    fInteractions.Print();
920    cout << "#################################################" << endl;
921    fFunctions.Print();
922    cout << "#################################################" << endl;
923    fDescriptors.Print();
924    cout << "#################################################" << endl;
925    fClusters.Print();
926    cout << "#################################################" << endl;
927    fPFProtections.Print();
928    cout << "#################################################" << endl;
929    fMasks.Print();
930    cout << "#################################################" << endl;
931    fClasses.Print();
932    cout << "#################################################" << endl;
933
934    cout << endl;
935 }
936
937
938 //////////////////////////////////////////////////////////////////////////////
939 // Helper method
940
941 //_____________________________________________________________________________
942 Bool_t AliTriggerConfiguration::IsSelected( TString detName, TString& detectors ) const
943 {
944    // check whether detName is contained in detectors
945    // if yes, it is removed from detectors
946
947    // check if all detectors are selected
948    if( (detectors.CompareTo("ALL") == 0 ) ||
949         detectors.BeginsWith("ALL ") ||
950         detectors.EndsWith(" ALL") ||
951         detectors.Contains(" ALL ") ) {
952       detectors = "ALL";
953       return kTRUE;
954    }
955
956    // search for the given detector
957    Bool_t result = kFALSE;
958    if( (detectors.CompareTo( detName ) == 0) ||
959         detectors.BeginsWith( detName+" " ) ||
960         detectors.EndsWith( " "+detName ) ||
961         detectors.Contains( " "+detName+" " ) ) {
962       detectors.ReplaceAll( detName, "" );
963       result = kTRUE;
964    }
965
966    // clean up the detectors string
967    while( detectors.Contains("  ") )  detectors.ReplaceAll( "  ", " " );
968    while( detectors.BeginsWith(" ") ) detectors.Remove( 0, 1 );
969    while( detectors.EndsWith(" ") )   detectors.Remove( detectors.Length()-1, 1 );
970
971    return result;
972 }