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