]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliTriggerConfiguration.cxx
Coding conventions...
[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 {
102   // Default constructor
103 }
104
105 //_____________________________________________________________________________
106 AliTriggerConfiguration::AliTriggerConfiguration( TString & name, TString & description ):
107   TNamed( name, description ),
108   fInputs(),
109   fInteractions(),
110   fFunctions(),
111   fPFProtections(),
112   fMasks(),
113   fDescriptors(),
114   fClusters(),
115   fClasses()
116 {
117   // Constructor
118 }
119
120 //_____________________________________________________________________________
121 AliTriggerConfiguration::~AliTriggerConfiguration() 
122
123   // Destructor
124   fInputs.SetOwner();
125   fInputs.Delete();
126   fInteractions.SetOwner();
127   fInteractions.Delete();
128   fFunctions.SetOwner();
129   fFunctions.Delete();
130   fPFProtections.SetOwner();
131   fPFProtections.Delete();
132   fMasks.SetOwner();
133   fMasks.Delete();
134   fDescriptors.SetOwner();
135   fDescriptors.Delete();
136   fClusters.SetOwner(); 
137   fClusters.Delete(); 
138   fClasses.SetOwner(); 
139   fClasses.Delete(); 
140 }
141
142 //_____________________________________________________________________________
143 Bool_t AliTriggerConfiguration::AddInput( AliTriggerInput* input )
144 {
145   // Add a trigger input to
146   // the list of the trigger inputs
147   if (fInputs.GetEntries() < kNMaxInputs) {
148     fInputs.AddLast( input );
149     return kTRUE;
150   }
151   else {
152     AliError("CTP can handle up to 50 inputs ! Impossible to add the required input !");
153     return kFALSE;
154   }
155 }
156
157 //_____________________________________________________________________________
158 AliTriggerInput* AliTriggerConfiguration::AddInput( TString &name, TString &det,
159                                                     UChar_t level, UInt_t signature,
160                                                     UChar_t number )
161 {
162   // Add a trigger input to
163   // the list of the trigger inputs
164   AliTriggerInput *input = new AliTriggerInput(name,det,level,signature,number);
165   if (!AddInput(input)) {
166     delete input;
167     return NULL;
168   }
169   else
170     return input;
171 }
172
173 //_____________________________________________________________________________
174 AliTriggerInteraction* AliTriggerConfiguration::AddInteraction(TString &name, TString &logic)
175 {
176   // Add a trigger interaction object to
177   // the list of the trigger interactions
178   AliTriggerInteraction *interact = new AliTriggerInteraction(name,logic);
179   if (!AddInteraction(interact)) {
180     delete interact;
181     return NULL;
182   }
183   else
184     return interact;
185 }
186
187 //_____________________________________________________________________________
188 Bool_t  AliTriggerConfiguration::AddInteraction(AliTriggerInteraction *interact)
189 {
190   // Add a trigger interaction object to
191   // the list of the trigger interactions
192   if (fInteractions.GetEntries() < kNMaxInteractions) {
193     if (interact->CheckInputs(fInputs)) {
194       fInteractions.AddLast( interact );
195       return kTRUE;
196     }
197     else
198       AliError("Invalid interaction ! Impossible to add it !");
199   }
200   else
201     AliError("CTP can handle up to 2 interactions ! Impossible to add the required interaction !");
202
203   return kFALSE;
204 }
205
206 //_____________________________________________________________________________
207 AliTriggerInteraction* AliTriggerConfiguration::AddFunction(TString &name, TString &logic)
208 {
209   // Add a trigger function object to
210   // the list of the trigger functions
211   AliTriggerInteraction *func = new AliTriggerInteraction(name,logic);
212   if (!AddFunction(func)) {
213     delete func;
214     return NULL;
215   }
216   else
217     return func;
218 }
219
220 //_____________________________________________________________________________
221 Bool_t  AliTriggerConfiguration::AddFunction(AliTriggerInteraction *func)
222 {
223   // Add a trigger function object to
224   // the list of the trigger functions
225   if (fFunctions.GetEntries() < kNMaxFunctions) {
226     if (func->CheckInputs(fInputs)) {
227       fFunctions.AddLast( func );
228       return kTRUE;
229     }
230     else
231       AliError("Invalid logical function ! Impossible to add it !");
232   }
233   else
234     AliError("CTP can handle up to 2 logical functions ! Impossible to add the required interaction !");
235
236   return kFALSE;
237 }
238
239 //_____________________________________________________________________________
240 Bool_t AliTriggerConfiguration::AddPFProtection( AliTriggerPFProtection* pfp )
241 {
242   // Add a trigger past-future protection object to
243   // the list of the trigger past-future protections
244   if (fPFProtections.GetEntries() < kNMaxPFProtections) {
245     if (pfp->CheckInteractions(fInteractions)) {
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) {
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 j=0; j<ndet; j++ ) {
338          if( activeDet.Contains( ((TObjString*)det->At(j))->String() ) )continue;
339          activeDet.Append( " " );
340          activeDet.Append( ((TObjString*)det->At(j))->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 AliTriggerConfiguration* AliTriggerConfiguration::LoadConfiguration( TString & configuration)
505 {
506    // Load one pre-created Configurations from database/file that match
507    // with the input string 'configuration'
508    // Ej: "Pb-Pb" or "p-p-DIMUON CALIBRATION-CENTRAL-BARREL"
509   // By default the files are stored in GRP/CTP folder.
510   // The filename is constructed as: GRP/CTP/<configuration>.cfg
511
512    // Load the selected configuration
513    TString filename = gSystem->Getenv("ALICE_ROOT");
514    filename += "/GRP/CTP/";
515    filename += configuration;
516    filename += ".cfg";
517
518    if( gSystem->AccessPathName( filename.Data() ) ) {
519       AliErrorClass( Form( "file (%s) not found", filename.Data() ) );
520       return NULL;
521    }
522
523
524    ifstream *file = new ifstream ( filename.Data() );
525    if (!*file) {
526      AliErrorClass(Form("Error opening file (%s) !",filename.Data()));
527      file->close();
528      delete file;
529      return NULL;
530    }
531
532    AliTriggerConfiguration *cfg = new AliTriggerConfiguration();
533
534    Int_t level = 0;
535    TString strLine;
536    while (strLine.ReadLine(*file)) {
537      if (strLine.BeginsWith("#")) continue;
538      if (strLine.BeginsWith("INPUTS:")) {
539        level = 1;
540        continue;
541      }
542      if (strLine.BeginsWith("INTERACTIONS:")) {
543        level = 2;
544        continue;
545      }
546      if (strLine.BeginsWith("DESCRIPTORS:")) {
547        level = 3;
548        continue;
549      }
550      if (strLine.BeginsWith("CLUSTERS:")) {
551        level = 4;
552        continue;
553      }
554      if (strLine.BeginsWith("PFS:")) {
555        level = 5;
556        continue;
557      }
558      if (strLine.BeginsWith("BCMASKS:")) {
559        level = 6;
560        continue;
561      }
562      if (strLine.BeginsWith("CLASSES:")) {
563        level = 7;
564        continue;
565      }
566      strLine.ReplaceAll("*",'!');
567      TObjArray *tokens = strLine.Tokenize(" \t");
568      Int_t ntokens = tokens->GetEntriesFast();
569      switch (level) {
570      case 1:
571        // Read inputs
572        if (ntokens != 5) {
573          AliErrorClass(Form("Invalid trigger input syntax (%s)!",strLine.Data()));
574          file->close();
575          delete file;
576          return NULL;
577        }
578        cfg->AddInput(((TObjString*)tokens->At(0))->String(),
579                      ((TObjString*)tokens->At(1))->String(),
580                      ((TObjString*)tokens->At(2))->String().Atoi(),
581                      ((TObjString*)tokens->At(3))->String().Atoi(),
582                      ((TObjString*)tokens->At(4))->String().Atoi());
583        break;
584      case 2:
585        // Read intreractions
586        cfg->AddInteraction(((TObjString*)tokens->At(0))->String(),
587                            ((TObjString*)tokens->At(1))->String());
588        break;
589      case 3:
590        // Read logical functions and descriptors
591        if (((TObjString*)tokens->At(0))->String().BeginsWith("l0f")) {
592          // function
593          cfg->AddFunction(((TObjString*)tokens->At(0))->String(),
594                           strLine.ReplaceAll(((TObjString*)tokens->At(0))->String(),""));
595        }
596        else {
597          cfg->AddDescriptor(((TObjString*)tokens->At(0))->String(),
598                             strLine.ReplaceAll(((TObjString*)tokens->At(0))->String(),""));
599        }
600        break;
601      case 4:
602        {
603          TString strTemp;
604          for(Int_t i = 2; i < ntokens; i++) {
605            strTemp += ((TObjString*)tokens->At(i))->String();
606            strTemp += " ";
607          }
608          cfg->AddCluster(((TObjString*)tokens->At(0))->String(),
609                          ((TObjString*)tokens->At(1))->String().Atoi(),
610                          strTemp);
611        }
612        break;
613      case 5:
614        {
615          AliTriggerPFProtection *pfp = NULL;
616          if (((TObjString*)tokens->At(0))->String().CompareTo("NONE") == 0) {
617            pfp = new AliTriggerPFProtection(((TObjString*)tokens->At(0))->String());
618          }
619          else {
620            pfp = new AliTriggerPFProtection(((TObjString*)tokens->At(0))->String(),
621                                             ((TObjString*)tokens->At(1))->String(),
622                                             ((TObjString*)tokens->At(2))->String(),
623                                             ((TObjString*)tokens->At(3))->String());
624            pfp->SetNa1(((TObjString*)tokens->At(4))->String().Atoi());
625            pfp->SetNa2(((TObjString*)tokens->At(5))->String().Atoi());
626            pfp->SetNb1(((TObjString*)tokens->At(6))->String().Atoi());
627            pfp->SetNb2(((TObjString*)tokens->At(7))->String().Atoi());
628            pfp->SetTa(((TObjString*)tokens->At(8))->String().Atoi());
629            pfp->SetTb(((TObjString*)tokens->At(9))->String().Atoi());
630          }
631          cfg->AddPFProtection(pfp);
632        }
633        break;
634      case 6:
635        if (((TObjString*)tokens->At(0))->String().CompareTo("NONE") == 0)
636          cfg->AddMask(new AliTriggerBCMask(((TObjString*)tokens->At(0))->String()));
637        else {
638          cfg->AddMask(((TObjString*)tokens->At(0))->String(),
639                       ((TObjString*)tokens->At(1))->String());
640        }
641        break;
642      case 7:
643        {
644          AliTriggerClass *trclass = new AliTriggerClass(cfg,
645                                                         ((TObjString*)tokens->At(0))->String(),((TObjString*)tokens->At(1))->String().Atoi(),
646                                                         ((TObjString*)tokens->At(2))->String(),((TObjString*)tokens->At(3))->String(),
647                                                         ((TObjString*)tokens->At(4))->String(),((TObjString*)tokens->At(5))->String(),
648                                                         ((TObjString*)tokens->At(6))->String().Atoi(),(Bool_t)(((TObjString*)tokens->At(0))->String().Atoi()));
649          cfg->AddClass(trclass);
650        }
651      default:
652        break;
653      }
654      delete tokens;
655    }
656
657    file->close();
658    delete file;
659
660    return cfg;
661
662 }
663
664 //_____________________________________________________________________________
665 TObjArray* AliTriggerConfiguration::GetAvailableConfigurations( const char* filename )
666 {
667    // Return an array of configuration in the file
668
669    TString path;
670    if( !filename[0] ) {
671       path += gSystem->Getenv( "ALICE_ROOT" );
672       path += fgkConfigurationFileName;
673    }
674    else
675       path += filename;
676
677    if( gSystem->AccessPathName( path.Data() ) ) {
678       AliErrorGeneral( "AliTriggerConfiguration", Form( "file (%s) not found", path.Data() ) );
679       return NULL;
680    }
681
682    TObjArray* desArray = new TObjArray();
683
684    TFile file( path.Data(), "READ" );
685    if( file.IsZombie() ) {
686       AliErrorGeneral( "AliTriggerConfiguration", Form( "Error opening file (%s)", path.Data() ) );
687       return NULL;
688    }
689
690    file.ReadAll();
691
692    TKey* key;
693    TIter next( file.GetListOfKeys() );
694    while( (key = (TKey*)next()) ) {
695       TObject* obj = key->ReadObj();
696       if( obj->InheritsFrom( "AliTriggerConfiguration" ) ) {
697          desArray->AddLast( obj );
698       }
699    }
700    file.Close();
701
702    return desArray;
703 }
704
705 //_____________________________________________________________________________
706 void AliTriggerConfiguration::WriteConfiguration( const char* filename )
707 {
708    // Write the configuration
709    TString path;
710    if( !filename[0] ) {
711       path += gSystem->Getenv("ALICE_ROOT");
712       path += fgkConfigurationFileName;
713    }
714    else
715       path += filename;
716
717    TFile file( path.Data(), "UPDATE" );
718    if( file.IsZombie() ) {
719       AliErrorGeneral( "AliTriggerConfiguration", 
720                         Form( "Can't open file (%s)", path.Data() ) );
721       return;
722    }
723
724    Bool_t result = (Write( GetName(), TObject::kOverwrite ) != 0);
725    if( !result )
726       AliErrorGeneral( "AliTriggerConfiguration",
727                         Form( "Can't write entry to file <%s>!", path.Data() ) );
728    file.Close();
729 }
730
731 //_____________________________________________________________________________
732 Bool_t AliTriggerConfiguration::CheckConfiguration( TString& configfile )
733 {
734    // To be used on the pre-creation of Configurations to check if the
735    // conditions have valid inputs names.
736    //
737    // Initiate detectors modules from a Config file
738    // Ask to each active module present in the fDetectorCluster
739    // to create a Trigger detector and retrive the inputs from it
740    // to create a list of inputs.
741    // Each condition in the configuration is then checked agains 
742    // the list of inputs
743
744
745    if (!gAlice) {
746       AliError( "no gAlice object. Restart aliroot and try again." );
747       return kFALSE;
748    }
749    if (gAlice->Modules()->GetEntries() > 0) {
750       AliError( "gAlice was already run. Restart aliroot and try again." );
751       return kFALSE;
752    }
753
754    AliInfo( Form( "initializing gAlice with config file %s",
755             configfile.Data() ) );
756    StdoutToAliInfo( StderrToAliError(
757       gAlice->Init( configfile.Data() );
758    ););
759
760    AliRunLoader* runLoader = gAlice->GetRunLoader();
761    if( !runLoader ) {
762       AliError( Form( "gAlice has no run loader object. "
763                       "Check your config file: %s", configfile.Data() ) );
764       return kFALSE;
765    }
766
767    // get the possible inputs to check the condition
768    TObjArray inputs;
769    TObjArray* detArray = runLoader->GetAliRun()->Detectors();
770
771    TString detStr = GetTriggeringModules();
772
773    for( Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++ ) {
774       AliModule* det = (AliModule*) detArray->At(iDet);
775       if( !det || !det->IsActive() ) continue;
776       if( IsSelected( det->GetName(), detStr ) ) {
777          AliInfo( Form( "Creating inputs for %s", det->GetName() ) );
778          AliTriggerDetector* dtrg = det->CreateTriggerDetector();
779          dtrg->CreateInputs(GetInputs());
780          TObjArray* detInp = dtrg->GetInputs();
781          for( Int_t i=0; i<detInp->GetEntriesFast(); i++ ) {
782             AliInfo( Form( "Adding input %s", ((AliTriggerInput*)detInp->At(i))->GetName() ) );
783             inputs.AddLast( detInp->At(i) );
784          }
785       }
786    }
787
788    // check if the condition is compatible with the triggers inputs
789    Int_t ndesc = fClasses.GetEntriesFast();
790    Bool_t check = kTRUE;
791    ULong64_t mask = 0L;
792    for( Int_t j=0; j<ndesc; j++ ) {
793      AliTriggerClass *trclass = (AliTriggerClass*)fClasses.At( j );
794      if( !(trclass->CheckClass( this )) ) check = kFALSE;
795      else {
796        if (trclass->IsActive(this->GetInputs(),this->GetFunctions())) {
797          AliInfo( Form( "Trigger Class (%s) OK, class mask (0x%Lx)",
798                         trclass->GetName(), trclass->GetMask( ) ) );
799        }
800        else {
801          AliWarning( Form( "Trigger Class (%s) is NOT active, class mask (0x%Lx)",
802                            trclass->GetName(), trclass->GetMask( ) ) );
803        }
804      }
805      // check if condition mask is duplicated
806      if( mask & trclass->GetMask() ) {
807        AliError( Form("Class (%s). The class mask (0x%Lx) is ambiguous. It was already defined",
808                       trclass->GetName(), trclass->GetMask()  ) );
809        check = kFALSE;
810      }
811      mask |= trclass->GetMask();
812    }
813
814    return check;
815 }
816
817
818 //_____________________________________________________________________________
819 void AliTriggerConfiguration::Print( const Option_t*  ) const
820 {
821    // Print
822   cout << "#################################################" << endl;
823    cout << "Trigger Configuration:"  << endl;
824    cout << "  Name:              " << GetName() << endl; 
825    cout << "  Description:       " << GetTitle() << endl;
826    cout << "  Active Detectors:  " << GetActiveDetectors() << endl;
827    cout << "  Trigger Detectors: " << GetTriggeringDetectors() << endl;
828
829    cout << "#################################################" << endl;
830    fInputs.Print();
831    cout << "#################################################" << endl;
832    fInteractions.Print();
833    cout << "#################################################" << endl;
834    fFunctions.Print();
835    cout << "#################################################" << endl;
836    fDescriptors.Print();
837    cout << "#################################################" << endl;
838    fClusters.Print();
839    cout << "#################################################" << endl;
840    fPFProtections.Print();
841    cout << "#################################################" << endl;
842    fMasks.Print();
843    cout << "#################################################" << endl;
844    fClasses.Print();
845    cout << "#################################################" << endl;
846
847    cout << endl;
848 }
849
850
851 //////////////////////////////////////////////////////////////////////////////
852 // Helper method
853
854 //_____________________________________________________________________________
855 Bool_t AliTriggerConfiguration::IsSelected( TString detName, TString& detectors ) const
856 {
857    // check whether detName is contained in detectors
858    // if yes, it is removed from detectors
859
860    // check if all detectors are selected
861    if( (detectors.CompareTo("ALL") == 0 ) ||
862         detectors.BeginsWith("ALL ") ||
863         detectors.EndsWith(" ALL") ||
864         detectors.Contains(" ALL ") ) {
865       detectors = "ALL";
866       return kTRUE;
867    }
868
869    // search for the given detector
870    Bool_t result = kFALSE;
871    if( (detectors.CompareTo( detName ) == 0) ||
872         detectors.BeginsWith( detName+" " ) ||
873         detectors.EndsWith( " "+detName ) ||
874         detectors.Contains( " "+detName+" " ) ) {
875       detectors.ReplaceAll( detName, "" );
876       result = kTRUE;
877    }
878
879    // clean up the detectors string
880    while( detectors.Contains("  ") )  detectors.ReplaceAll( "  ", " " );
881    while( detectors.BeginsWith(" ") ) detectors.Remove( 0, 1 );
882    while( detectors.EndsWith(" ") )   detectors.Remove( detectors.Length()-1, 1 );
883
884    return result;
885 }