]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliTriggerConfiguration.cxx
Removal of AliMCQA and of TreeH method on AliDetector
[u/mrichter/AliRoot.git] / STEER / 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 //#include <cstdlib>
62
63 #include <TObject.h>
64 #include <TString.h>
65 #include <TObjString.h>
66 #include <TObjArray.h>
67 #include <TSystem.h>
68 #include <TKey.h>
69 #include <TFile.h>
70
71 #include "AliLog.h"
72 #include "AliRun.h"
73 #include "AliRunLoader.h"
74 #include "AliModule.h"
75
76 #include "AliTriggerInput.h"
77 //#include "AliTriggerDetector.h"
78 #include "AliTriggerInteraction.h"
79 #include "AliTriggerBCMask.h"
80 #include "AliTriggerCluster.h"
81 #include "AliTriggerPFProtection.h"
82 #include "AliTriggerDescriptor.h"
83 #include "AliTriggerClass.h"
84 #include "AliTriggerConfiguration.h"
85
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 50 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 2 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       fPFProtections.AddLast( pfp );
249       return kTRUE;
250     }
251     else
252       AliError("Invalid past-future protection ! Impossible to add it !");
253   }
254   else
255     AliError("CTP can handle up to 4 past-future protections ! Impossible to add the required protection !");
256
257   return kFALSE;
258 }
259
260 //_____________________________________________________________________________
261 AliTriggerBCMask* AliTriggerConfiguration::AddMask( TString &name, TString &mask )
262 {
263   // Add a trigger bunch-crossing mask object to
264   // the list of the trigger bunch-crossing masks
265   AliTriggerBCMask *bcmask = new AliTriggerBCMask(name,mask);
266   if (!AddMask(bcmask)) {
267     delete bcmask;
268     return NULL;
269   }
270   else
271     return bcmask;
272 }
273
274 //_____________________________________________________________________________
275 Bool_t AliTriggerConfiguration::AddMask( AliTriggerBCMask* mask )
276 {
277   // Add a trigger bunch-crossing mask object to
278   // the list of the trigger bunch-crossing masks
279   if (fMasks.GetEntries() < kNMaxMasks) {
280       fMasks.AddLast( mask );
281       return kTRUE;
282   }
283   else
284     AliError("CTP can handle up to 4 bunch-crossing masks ! Impossible to add the required mask !");
285
286   return kFALSE;
287 }
288
289 //_____________________________________________________________________________
290 AliTriggerCluster* AliTriggerConfiguration::AddCluster( TString &name, UChar_t index, TString &detectors)
291 {
292   // Add a trigger detector readout cluster to
293   // the list of the trigger clusters
294   AliTriggerCluster *clust = new AliTriggerCluster(name,index,detectors);
295   if (!AddCluster(clust)) {
296     delete clust;
297     return NULL;
298   }
299   else
300     return clust;
301
302 }
303
304 //_____________________________________________________________________________
305 Bool_t AliTriggerConfiguration::AddCluster( AliTriggerCluster* cluster )
306 {
307   // Add a trigger detector readout cluster to
308   // the list of the trigger clusters
309   if (fClusters.GetEntries() < kNMaxClusters) {
310     TString dets(cluster->GetDetectorsInCluster());
311     if (!(dets.IsNull())) {
312       fClusters.AddLast( cluster );
313       return kTRUE;
314     }
315     else
316       AliError("Empty trigger cluster ! Impossible to add it !");
317   }
318   else
319     AliError("CTP can handle up to 6 different detector clusters ! Impossible to add the required cluster !");
320
321   return kFALSE;
322 }
323
324 //_____________________________________________________________________________
325 TString AliTriggerConfiguration::GetActiveDetectors() const
326 {
327   // Return an string with all active detector
328   // from each cluster
329
330    TString activeDet = "";
331
332    Int_t nclus = fClusters.GetEntriesFast();
333    if( !nclus ) return activeDet;
334    
335    for( Int_t j=0; j<nclus; ++j ) {
336       TString detStr = ((AliTriggerCluster*)fClusters.At(j))->GetDetectorsInCluster();
337       TObjArray* det = detStr.Tokenize(" ");
338       Int_t ndet = det->GetEntriesFast();
339       for( Int_t k=0; k<ndet; ++k ) {
340          if( activeDet.Contains( ((TObjString*)det->At(k))->String() ) )continue;
341          activeDet.Append( " " );
342          activeDet.Append( ((TObjString*)det->At(k))->String() );
343       }
344    }
345    return activeDet;
346 }
347
348 //_____________________________________________________________________________
349 TString AliTriggerConfiguration::GetTriggeringDetectors() const
350 {
351   // Return an string with all detectors
352   // used for triggering
353
354    TString trDet = "";
355
356    Int_t ninputs = fInputs.GetEntriesFast();
357    if( !ninputs ) return trDet;
358    
359    for( Int_t j=0; j<ninputs; j++ ) {
360       TString detStr = ((AliTriggerInput*)fInputs.At(j))->GetDetector();
361       if( trDet.Contains( detStr ) ) continue;
362       trDet.Append( " " );
363       trDet.Append( detStr );
364    }
365    return trDet;
366 }
367
368 //_____________________________________________________________________________
369 TString AliTriggerConfiguration::GetTriggeringModules() const
370 {
371    // Return an string with all detectors (modules in the AliRoot
372   // simulation sense) used for triggering
373
374    TString trDet = "";
375
376    Int_t ninputs = fInputs.GetEntriesFast();
377    if( !ninputs ) return trDet;
378    
379    for( Int_t j=0; j<ninputs; j++ ) {
380       TString detStr = ((AliTriggerInput*)fInputs.At(j))->GetModule();
381       if( trDet.Contains( detStr ) ) continue;
382       trDet.Append( " " );
383       trDet.Append( detStr );
384    }
385    return trDet;
386 }
387
388 //_____________________________________________________________________________
389 AliTriggerDescriptor* AliTriggerConfiguration::AddDescriptor( TString &name, TString &cond )
390 {
391   // Add a trigger descriptor to
392   // the list of the trigger descriptors
393   AliTriggerDescriptor *desc = new AliTriggerDescriptor(name,cond);
394   if (!AddDescriptor(desc)) {
395     delete desc;
396     return NULL;
397   }
398   else
399     return desc;
400 }
401
402 //_____________________________________________________________________________
403 Bool_t AliTriggerConfiguration::AddDescriptor( AliTriggerDescriptor *desc )
404 {
405   // Add a trigger descriptor to
406   // the list of the trigger descriptors
407   if (fDescriptors.GetEntries() < kNMaxClasses) {
408     if (desc->CheckInputsAndFunctions(fInputs,fFunctions)) {
409       fDescriptors.AddLast( desc );
410       return kTRUE;
411     }
412     else
413       AliError("Invalid trigger desciptor ! Impossible to add it !");
414   }
415   else
416     AliError("CTP can handle up to 50 different descriptors ! Impossible to add the required descriptor !");
417
418   return kFALSE;
419 }
420
421 //_____________________________________________________________________________
422 Bool_t AliTriggerConfiguration::AddClass( AliTriggerClass *trclass )
423 {
424   // Add a trigger class to
425   // the list of the trigger classes
426   if (fClasses.GetEntries() < kNMaxClasses) {
427     if (trclass->CheckClass(this)) {
428       fClasses.AddLast( trclass );
429       return kTRUE;
430     }
431     else
432       AliError("Invalid trigger class ! Impossible to add it !");
433   }
434   else
435     AliError("CTP can handle up to 50 different classes ! Impossible to add the required class !");
436
437   return kFALSE;
438 }
439
440 //_____________________________________________________________________________
441 AliTriggerClass *AliTriggerConfiguration::AddClass( TString &name, UChar_t index,
442                                                     AliTriggerDescriptor *desc, AliTriggerCluster *clus,
443                                                     AliTriggerPFProtection *pfp, AliTriggerBCMask *mask,
444                                                     UInt_t prescaler, Bool_t allrare)
445 {
446   // Add a trigger class to
447   // the list of the trigger classes
448   if (!fDescriptors.FindObject(desc)) {
449     AliError("Invalid descriptor ! Impossible to add the class !");
450     return NULL;
451   }
452   if (!fClusters.FindObject(clus)) {
453     AliError("Invalid cluster ! Impossible to add the class !");
454     return NULL;
455   }
456   if (!fPFProtections.FindObject(pfp)) {
457     AliError("Invalid past-future protection ! Impossible to add the class !");
458     return NULL;
459   }
460   if (!fMasks.FindObject(mask)) {
461     AliError("Invalid bunch-crossing mask ! Impossible to add the class !");
462     return NULL;
463   }
464   AliTriggerClass* trclass = new AliTriggerClass( name,index,desc,clus,pfp,mask,prescaler,allrare );
465   if (!AddClass(trclass)) {
466     delete trclass;
467     return NULL;
468   }
469   else
470     return trclass;
471 }
472
473 //_____________________________________________________________________________
474 AliTriggerClass *AliTriggerConfiguration::AddClass( TString &name, UChar_t index,
475                                                     TString &desc, TString &clus,
476                                                     TString &pfp, TString &mask,
477                                                     UInt_t prescaler, Bool_t allrare)
478 {
479    // Add a new trigger class
480   if (!fDescriptors.FindObject(desc)) {
481     AliError("Invalid descriptor ! Impossible to add the class !");
482     return NULL;
483   }
484   if (!fClusters.FindObject(clus)) {
485     AliError("Invalid cluster ! Impossible to add the class !");
486     return NULL;
487   }
488   if (!fPFProtections.FindObject(pfp)) {
489     AliError("Invalid past-future protection ! Impossible to add the class !");
490     return NULL;
491   }
492   if (!fMasks.FindObject(mask)) {
493     AliError("Invalid bunch-crossing mask ! Impossible to add the class !");
494     return NULL;
495   }
496   AliTriggerClass* trclass = new AliTriggerClass( this, name,index,desc,clus,pfp,mask,prescaler,allrare );
497   if (!AddClass(trclass)) {
498     delete trclass;
499     return NULL;
500   }
501   else
502     return trclass;
503 }
504
505 //_____________________________________________________________________________
506 Bool_t AliTriggerConfiguration::ProcessConfigurationLine(const char* line, Int_t& level)
507 {
508     // processes one line of configuration
509
510      TString strLine(line);
511
512      if (strLine.BeginsWith("#")) return kTRUE;
513      if (strLine.BeginsWith("PARTITION:")) {
514        strLine.ReplaceAll("PARTITION:","");
515        SetName(strLine.Data());
516        return kTRUE;
517      }
518      if (strLine.BeginsWith("VERSION:")) {
519        strLine.ReplaceAll("VERSION:","");
520        fVersion = strLine.Atoi();
521        return kTRUE;
522      }
523      if (strLine.BeginsWith("INPUTS:")) {
524        level = 1;
525        return kTRUE;
526      }
527      if (strLine.BeginsWith("INTERACTIONS:")) {
528        level = 2;
529        return kTRUE;
530      }
531      if (strLine.BeginsWith("DESCRIPTORS:")) {
532        level = 3;
533        return kTRUE;
534      }
535      if (strLine.BeginsWith("CLUSTERS:")) {
536        level = 4;
537        return kTRUE;
538      }
539      if (strLine.BeginsWith("PFS:")) {
540        level = 5;
541        return kTRUE;
542      }
543      if (strLine.BeginsWith("BCMASKS:")) {
544        level = 6;
545        return kTRUE;
546      }
547      if (strLine.BeginsWith("CLASSES:")) {
548        level = 7;
549        return kTRUE;
550      }
551
552      strLine.ReplaceAll("*",'!');
553      strLine.ReplaceAll("~",'!');
554      TObjArray *tokens = strLine.Tokenize(" \t");
555      Int_t ntokens = tokens->GetEntriesFast();
556      if (ntokens == 0)
557      {
558        delete tokens;
559        return kTRUE;
560      }
561      switch (level) {
562      case 1:
563        // Read inputs
564        if (ntokens != 5) {
565          AliError(Form("Invalid trigger input syntax (%s)!",strLine.Data()));
566          return kFALSE;
567        }
568        AddInput(((TObjString*)tokens->At(0))->String(),
569                      ((TObjString*)tokens->At(1))->String(),
570                      ((TObjString*)tokens->At(2))->String().Atoi(),
571                      ((TObjString*)tokens->At(3))->String().Atoi(),
572                      ((TObjString*)tokens->At(4))->String().Atoi());
573        break;
574      case 2:
575        // Read interaction
576        if (ntokens != 2) {
577          AliError(Form("Invalid trigger interaction syntax (%s)!",strLine.Data()));
578          return kFALSE;
579        }
580        AddInteraction(((TObjString*)tokens->At(0))->String(),
581                            ((TObjString*)tokens->At(1))->String());
582        break;
583      case 3:
584        // Read logical functions and descriptors
585        if (ntokens < 2) {
586          if ((((TObjString*)tokens->At(0))->String().CompareTo("EMPTY") == 0) ||
587              (((TObjString*)tokens->At(0))->String().CompareTo("DEMPTY") == 0)) {
588            AddDescriptor(((TObjString*)tokens->At(0))->String(),
589                          strLine.ReplaceAll(((TObjString*)tokens->At(0))->String(),""));
590            break;
591          }
592          else {
593            AliError(Form("Invalid trigger descriptor syntax (%s)!",strLine.Data()));
594            return kFALSE;
595          }
596        }
597        if (((TObjString*)tokens->At(0))->String().BeginsWith("l0f")) {
598          // function
599          AddFunction(((TObjString*)tokens->At(0))->String(),
600                           strLine.ReplaceAll(((TObjString*)tokens->At(0))->String(),""));
601        }
602        else {
603          AddDescriptor(((TObjString*)tokens->At(0))->String(),
604                             strLine.ReplaceAll(((TObjString*)tokens->At(0))->String(),""));
605        }
606        break;
607      case 4:
608        {
609          if (ntokens < 2) {
610            AliError(Form("Invalid trigger cluster syntax (%s)!",strLine.Data()));
611            return kFALSE;
612          }
613          TString strTemp;
614          for(Int_t i = 2; i < ntokens; i++) {
615            strTemp += ((TObjString*)tokens->At(i))->String();
616            strTemp += " ";
617          }
618          AddCluster(((TObjString*)tokens->At(0))->String(),
619                          ((TObjString*)tokens->At(1))->String().Atoi(),
620                          strTemp);
621        }
622        break;
623      case 5:
624        {
625          AliTriggerPFProtection *pfp = NULL;
626          if ((((TObjString*)tokens->At(0))->String().CompareTo("NONE") == 0) ||
627              (((TObjString*)tokens->At(0))->String().CompareTo("NOPF") == 0)) {
628            pfp = new AliTriggerPFProtection(((TObjString*)tokens->At(0))->String());
629          }
630          else {
631            if (ntokens != 10) {
632              AliError(Form("Invalid trigger pfs syntax (%s)!",strLine.Data()));
633              return kFALSE;
634            }
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          }
646          AddPFProtection(pfp);
647        }
648        break;
649      case 6:
650          if (ntokens > 2) {
651            AliError(Form("Invalid trigger bcmasks syntax (%s)!",strLine.Data()));
652            return kFALSE;
653          }
654        if (((TObjString*)tokens->At(0))->String().CompareTo("NONE") == 0)
655          AddMask(new AliTriggerBCMask(((TObjString*)tokens->At(0))->String()));
656        else {
657          AddMask(((TObjString*)tokens->At(0))->String(),
658                       ((TObjString*)tokens->At(1))->String());
659        }
660        break;
661      case 7:
662        {
663          if (ntokens != 8) {
664            AliError(Form("Invalid trigger class syntax (%s)!",strLine.Data()));
665            return kFALSE;
666          }
667          AliTriggerClass *trclass = new AliTriggerClass(this,
668                                                         ((TObjString*)tokens->At(0))->String(),((TObjString*)tokens->At(1))->String().Atoi(),
669                                                         ((TObjString*)tokens->At(2))->String(),((TObjString*)tokens->At(3))->String(),
670                                                         ((TObjString*)tokens->At(4))->String(),((TObjString*)tokens->At(5))->String(),
671                                                         ((TObjString*)tokens->At(6))->String().Atoi(),(Bool_t)(((TObjString*)tokens->At(7))->String().Atoi()));
672          AddClass(trclass);
673        }
674      default:
675        break;
676      }
677      delete tokens;
678
679      return kTRUE;
680 }
681
682 //_____________________________________________________________________________
683 AliTriggerConfiguration* AliTriggerConfiguration::LoadConfiguration(TString & configuration)
684 {
685    // Load one pre-created Configurations from database/file that match
686    // with the input string 'configuration'
687    // Ej: "Pb-Pb" or "p-p-DIMUON CALIBRATION-CENTRAL-BARREL"
688   // By default the files are stored in GRP/CTP folder.
689   // The filename is constructed as: GRP/CTP/<configuration>.cfg
690
691    // Load the selected configuration
692   TString filename;
693   if (configuration.EndsWith(".cfg") ||
694       configuration.EndsWith(".shuttle")) {
695     filename = configuration;
696   }
697   else {
698     filename = gSystem->Getenv("ALICE_ROOT");
699     filename += "/GRP/CTP/";
700     filename += configuration;
701     filename += ".cfg";
702   }
703
704    if( gSystem->AccessPathName( filename.Data() ) ) {
705       AliErrorClass( Form( "file (%s) not found", filename.Data() ) );
706       return NULL;
707    }
708
709
710    ifstream *file = new ifstream ( filename.Data() );
711    if (!*file) {
712      AliErrorClass(Form("Error opening file (%s) !",filename.Data()));
713      file->close();
714      delete file;
715      return NULL;
716    }
717
718    AliTriggerConfiguration *cfg = new AliTriggerConfiguration();
719
720    Int_t level = 0;
721
722    TString strLine;
723    while (strLine.ReadLine(*file)) {
724      if (cfg->ProcessConfigurationLine(strLine, level) == kFALSE)
725      {
726         delete cfg;
727         cfg = 0;
728         break;
729      }
730    }
731
732    file->close();
733    delete file;
734
735    return cfg;
736 }
737
738 //_____________________________________________________________________________
739 AliTriggerConfiguration* AliTriggerConfiguration::LoadConfigurationFromString(const char* configuration)
740 {
741    // Loads configuration given as parameter <configuration>
742
743    if (!configuration)
744      return 0;
745
746    AliTriggerConfiguration *cfg = new AliTriggerConfiguration();
747
748    Int_t level = 0;
749
750    TObjArray* tokens = TString(configuration).Tokenize("\n");
751    for (Int_t i=0; i<tokens->GetEntries(); i++)
752    {
753      TObjString* str = dynamic_cast<TObjString*>(tokens->At(i));
754      if (!str)
755        continue;
756
757      if (cfg->ProcessConfigurationLine(str->String(), level) == kFALSE)
758      {
759         delete cfg;
760         cfg = 0;
761         break;
762      }
763    }
764
765    delete tokens;
766
767    return cfg;
768 }
769
770 //_____________________________________________________________________________
771 TObjArray* AliTriggerConfiguration::GetAvailableConfigurations( const char* filename )
772 {
773    // Return an array of configuration in the file
774
775    TString path;
776    if( !filename[0] ) {
777       path += gSystem->Getenv( "ALICE_ROOT" );
778       path += fgkConfigurationFileName;
779    }
780    else
781       path += filename;
782
783    if( gSystem->AccessPathName( path.Data() ) ) {
784       AliErrorGeneral( "AliTriggerConfiguration", Form( "file (%s) not found", path.Data() ) );
785       return NULL;
786    }
787
788    TObjArray* desArray = new TObjArray();
789
790    TFile file( path.Data(), "READ" );
791    if( file.IsZombie() ) {
792       AliErrorGeneral( "AliTriggerConfiguration", Form( "Error opening file (%s)", path.Data() ) );
793       return NULL;
794    }
795
796    file.ReadAll();
797
798    TKey* key;
799    TIter next( file.GetListOfKeys() );
800    while( (key = (TKey*)next()) ) {
801       TObject* obj = key->ReadObj();
802       if( obj->InheritsFrom( "AliTriggerConfiguration" ) ) {
803          desArray->AddLast( obj );
804       }
805    }
806    file.Close();
807
808    return desArray;
809 }
810
811 //_____________________________________________________________________________
812 void AliTriggerConfiguration::WriteConfiguration( const char* filename )
813 {
814    // Write the configuration
815    TString path;
816    if( !filename[0] ) {
817       path += gSystem->Getenv("ALICE_ROOT");
818       path += fgkConfigurationFileName;
819    }
820    else
821       path += filename;
822
823    TFile file( path.Data(), "UPDATE" );
824    if( file.IsZombie() ) {
825       AliErrorGeneral( "AliTriggerConfiguration", 
826                         Form( "Can't open file (%s)", path.Data() ) );
827       return;
828    }
829
830    Bool_t result = (Write( GetName(), TObject::kOverwrite ) != 0);
831    if( !result )
832       AliErrorGeneral( "AliTriggerConfiguration",
833                         Form( "Can't write entry to file <%s>!", path.Data() ) );
834    file.Close();
835 }
836
837 //_____________________________________________________________________________
838 Bool_t AliTriggerConfiguration::CheckConfiguration( TString& configfile )
839 {
840    // To be used on the pre-creation of Configurations to check if the
841    // conditions have valid inputs names.
842    //
843    // Initiate detectors modules from a Config file
844    // Ask to each active module present in the fDetectorCluster
845    // to create a Trigger detector and retrive the inputs from it
846    // to create a list of inputs.
847    // Each condition in the configuration is then checked agains 
848    // the list of inputs
849
850
851    if (!gAlice) {
852       AliError( "no gAlice object. Restart aliroot and try again." );
853       return kFALSE;
854    }
855    if (gAlice->Modules()->GetEntries() > 0) {
856       AliError( "gAlice was already run. Restart aliroot and try again." );
857       return kFALSE;
858    }
859
860    AliInfo( Form( "initializing gAlice with config file %s",
861             configfile.Data() ) );
862    StdoutToAliInfo( StderrToAliError(
863       gAlice->InitMC( configfile.Data() );
864    ););
865
866    AliRunLoader* runLoader = AliRunLoader::GetRunLoader();
867    if( !runLoader ) {
868       AliError( Form( "gAlice has no run loader object. "
869                       "Check your config file: %s", configfile.Data() ) );
870       return kFALSE;
871    }
872
873    // get the possible inputs to check the condition
874    TObjArray inputs;
875    TObjArray* detArray = runLoader->GetAliRun()->Detectors();
876
877    TString detStr = GetTriggeringModules();
878
879    for( Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++ ) {
880       AliModule* det = (AliModule*) detArray->At(iDet);
881       if( !det || !det->IsActive() ) continue;
882       if( IsSelected( det->GetName(), detStr ) ) {
883          AliInfo( Form( "Creating inputs for %s", det->GetName() ) );
884          AliTriggerDetector* dtrg = det->CreateTriggerDetector();
885          dtrg->CreateInputs(GetInputs());
886          TObjArray* detInp = dtrg->GetInputs();
887          for( Int_t i=0; i<detInp->GetEntriesFast(); i++ ) {
888             AliInfo( Form( "Adding input %s", ((AliTriggerInput*)detInp->At(i))->GetName() ) );
889             inputs.AddLast( detInp->At(i) );
890          }
891       }
892    }
893
894    // check if the condition is compatible with the triggers inputs
895    Int_t ndesc = fClasses.GetEntriesFast();
896    Bool_t check = kTRUE;
897    ULong64_t mask = 0L;
898    for( Int_t j=0; j<ndesc; j++ ) {
899      AliTriggerClass *trclass = (AliTriggerClass*)fClasses.At( j );
900      if( !(trclass->CheckClass( this )) ) check = kFALSE;
901      else {
902        if (trclass->IsActive(this->GetInputs(),this->GetFunctions())) {
903          AliInfo( Form( "Trigger Class (%s) OK, class mask (0x%Lx)",
904                         trclass->GetName(), trclass->GetMask( ) ) );
905        }
906        else {
907          AliWarning( Form( "Trigger Class (%s) is NOT active, class mask (0x%Lx)",
908                            trclass->GetName(), trclass->GetMask( ) ) );
909        }
910      }
911      // check if condition mask is duplicated
912      if( mask & trclass->GetMask() ) {
913        AliError( Form("Class (%s). The class mask (0x%Lx) is ambiguous. It was already defined",
914                       trclass->GetName(), trclass->GetMask()  ) );
915        check = kFALSE;
916      }
917      mask |= trclass->GetMask();
918    }
919
920    return check;
921 }
922
923 //_____________________________________________________________________________
924 void AliTriggerConfiguration::Reset()
925 {
926    for( Int_t j=0; j<fInputs.GetEntriesFast(); j++ )
927      ((AliTriggerInput*)fInputs.At(j))->Reset();
928
929    for( Int_t j=0; j<fClasses.GetEntriesFast(); j++ )
930      ((AliTriggerClass*)fClasses.At(j))->Reset();
931 }
932
933 //_____________________________________________________________________________
934 void AliTriggerConfiguration::Print( const Option_t*  ) const
935 {
936    // Print
937   cout << "#################################################" << endl;
938    cout << "Trigger Configuration:"  << endl;
939    cout << "  Name:              " << GetName() << endl; 
940    cout << "  Description:       " << GetTitle() << endl;
941    cout << "  Version:           " << GetVersion() << endl;
942    cout << "  Active Detectors:  " << GetActiveDetectors() << endl;
943    cout << "  Trigger Detectors: " << GetTriggeringDetectors() << endl;
944
945    cout << "#################################################" << endl;
946    fInputs.Print();
947    cout << "#################################################" << endl;
948    fInteractions.Print();
949    cout << "#################################################" << endl;
950    fFunctions.Print();
951    cout << "#################################################" << endl;
952    fDescriptors.Print();
953    cout << "#################################################" << endl;
954    fClusters.Print();
955    cout << "#################################################" << endl;
956    fPFProtections.Print();
957    cout << "#################################################" << endl;
958    fMasks.Print();
959    cout << "#################################################" << endl;
960    fClasses.Print();
961    cout << "#################################################" << endl;
962
963    cout << endl;
964 }
965
966
967 //////////////////////////////////////////////////////////////////////////////
968 // Helper method
969
970 //_____________________________________________________________________________
971 Bool_t AliTriggerConfiguration::IsSelected( TString detName, TString& detectors ) const
972 {
973    // check whether detName is contained in detectors
974    // if yes, it is removed from detectors
975
976    // check if all detectors are selected
977    if( (detectors.CompareTo("ALL") == 0 ) ||
978         detectors.BeginsWith("ALL ") ||
979         detectors.EndsWith(" ALL") ||
980         detectors.Contains(" ALL ") ) {
981       detectors = "ALL";
982       return kTRUE;
983    }
984
985    // search for the given detector
986    Bool_t result = kFALSE;
987    if( (detectors.CompareTo( detName ) == 0) ||
988         detectors.BeginsWith( detName+" " ) ||
989         detectors.EndsWith( " "+detName ) ||
990         detectors.Contains( " "+detName+" " ) ) {
991       detectors.ReplaceAll( detName, "" );
992       result = kTRUE;
993    }
994
995    // clean up the detectors string
996    while( detectors.Contains("  ") )  detectors.ReplaceAll( "  ", " " );
997    while( detectors.BeginsWith(" ") ) detectors.Remove( 0, 1 );
998    while( detectors.EndsWith(" ") )   detectors.Remove( detectors.Length()-1, 1 );
999
1000    return result;
1001 }