]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliTriggerDescriptor.cxx
Modifications to the trigger classes to have I/O. I
[u/mrichter/AliRoot.git] / STEER / AliTriggerDescriptor.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //
20 // This class for running and define a Trigger Descriptor 
21 //
22 // A Trigger Descriptor define a trigger setup for specific runnign
23 // condition (Pb-Pb, p-p, p-A, Calibration, etc).
24 // It keep:
25 //    - cluster detector (List of detectors involved)
26 //    - List of conditions
27 //
28 // Descriptors could be create in advance and store in a file.
29 //
30 //   Example how to create a Trigger Descriptor:
31 //
32 //   AliTriggerDescriptor descrip( "TEST", "Test Descriptor" );
33 //
34 //   // Define a Cluster Detector
35 //   descrip.AddDetectorCluster( "VZERO ZDC MUON" );
36 //
37 //   // Define the trigger conditions (see AliTriggerCondition.cxx)
38 //   descrip.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 //   descrip.AddCondition( "VZERO_TEST2_L0 & MUON_SMinus_HPt_L0 & ZDC_TEST1_L0",
44 //                         "VO2_M3_ZDC1",
45 //                         "Dummy",
46 //                          0x0200 );
47 //
48 //   descrip.AddCondition( "VZERO_TEST3_L0 | MUON_Unlike_LPt_L0 | ZDC_TEST3_L0",
49 //                         "VO3_M1_ZDC3",
50 //                         "Dummy",
51 //                          0x0400 );
52 //   descrip.CheckInputsConditions("Config.C");
53 //   descrip.Print();
54 //
55 //   // save the descriptor to file 
56 //   // (default file name $ALICE_ROOT/data/triggerDescriptor.root)
57 //   descrip.WriteDescriptor(); or descrip.WriteDescriptor( filename );
58 //
59 ///////////////////////////////////////////////////////////////////////////////
60
61 #include <TString.h>
62 #include <TObjArray.h>
63 #include <TSystem.h>
64 #include <TKey.h>
65 #include <TFile.h>
66
67 #include "AliLog.h"
68 #include "AliRun.h"
69 #include "AliRunLoader.h"
70 #include "AliModule.h"
71
72 #include "AliTriggerInput.h"
73 #include "AliTriggerDetector.h"
74 #include "AliTriggerCondition.h"
75 #include "AliTriggerDescriptor.h"
76
77
78 ClassImp(AliTriggerDescriptor)
79
80 //_____________________________________________________________________________
81 const char* AliTriggerDescriptor::fgkDetectorName[AliTriggerDescriptor::fgkNDetectors] =
82              { "ITS", "TRD", "PHOS", "EMCAL", "MUON", "ZDC", "START", "VZERO", "CRT" };
83
84 const TString AliTriggerDescriptor::fgkDescriptorFileName("/data/triggerDescriptors.root");
85
86 //_____________________________________________________________________________
87 AliTriggerDescriptor::AliTriggerDescriptor():
88 TNamed(), fDetectorCluster("")
89 {
90 }
91
92 //_____________________________________________________________________________
93 AliTriggerDescriptor::AliTriggerDescriptor( TString & name, TString & description ):
94 TNamed( name, description ), fDetectorCluster("")
95 {
96 }
97
98 //_____________________________________________________________________________
99 AliTriggerDescriptor::AliTriggerDescriptor( const AliTriggerDescriptor& des ):
100 TNamed( des ), fDetectorCluster( des.fDetectorCluster )
101 {
102    // Copy constructor
103    Int_t ncond = des.fConditions.GetEntriesFast();
104    for( Int_t j=0; j<ncond; j++ ) {
105       AddCondition( new AliTriggerCondition( *(AliTriggerCondition*)(des.fConditions.At( j )) ) );
106    }
107 }
108
109 //______________________________________________________________________________
110 AliTriggerDescriptor& AliTriggerDescriptor::operator=(const AliTriggerDescriptor& des)
111 {
112    // AliTriggerDescriptor assignment operator.
113
114    if (this != &des) {
115       TNamed::operator=(des);
116       fDetectorCluster = des.fDetectorCluster;
117       fConditions.Delete();
118       Int_t ncond = des.fConditions.GetEntriesFast();
119       for( Int_t j=0; j<ncond; j++ ) {
120          AddCondition( new AliTriggerCondition( *(AliTriggerCondition*)(des.fConditions.At( j )) ) );
121       }
122    }
123    return *this;
124 }
125
126 //_____________________________________________________________________________
127 Bool_t AliTriggerDescriptor::AddDetectorCluster( TString & cluster )
128 {
129    // Add a List of Detectors to be read together (Detector Cluster)
130    // Ej "TO VO ZDC MUON" or "ALL"
131
132    TString olddet = fDetectorCluster;
133    TString newdet = cluster;
134    for( Int_t iDet = 0; iDet < fgkNDetectors; iDet++ ) {
135       if( IsSelected( fgkDetectorName[iDet], newdet ) && !IsSelected( fgkDetectorName[iDet], olddet ) ) {
136          // Add the detector
137          fDetectorCluster.Append( " " );
138          fDetectorCluster.Append( fgkDetectorName[iDet] );
139       }
140    }
141
142    // check if there are no trigger detectors (E.g. TPC)
143    if ((newdet.CompareTo("ALL") != 0) && !newdet.IsNull()) {
144       AliError( Form("the following detectors are not trigger detectors: %s",
145                 newdet.Data() ) );
146       return kFALSE;
147    }
148
149    return kTRUE;
150 }
151
152 //_____________________________________________________________________________
153 void AliTriggerDescriptor::AddCondition( TString & cond, TString & name, TString & description, Long_t mask   )
154 {
155    // Add a new condition
156    AliTriggerCondition* acond = new AliTriggerCondition( cond, name, description, mask );
157    fConditions.AddLast( acond );
158 }
159
160
161 //_____________________________________________________________________________
162 AliTriggerDescriptor* AliTriggerDescriptor::LoadDescriptor( TString & descriptor, const char* filename )
163 {
164    // Load one pre-created Descriptors from database/file that match
165    // with the input string 'descriptor'
166    // Ej: "Pb-Pb" or "p-p-DIMUON CALIBRATION-CENTRAL-BARREL"
167
168    // Load the selected descriptor
169    TString path;
170    if( !filename[0] ) {
171       path += gSystem->Getenv("ALICE_ROOT");
172       path += fgkDescriptorFileName;
173    }
174    else
175       path += filename;
176
177    if( gSystem->AccessPathName( path.Data() ) ) {
178       AliErrorGeneral( "AliTriggerDescriptor", Form( "file (%s) not found", path.Data() ) );
179       return NULL;
180    }
181
182    TFile file( path.Data(), "READ" );
183    AliTriggerDescriptor* des = (AliTriggerDescriptor*)(file.Get( descriptor.Data() ));
184
185    file.Close();
186
187    return des;
188 }
189
190 //_____________________________________________________________________________
191 TObjArray* AliTriggerDescriptor::GetAvailableDescriptors( const char* filename )
192 {
193    // Return an array of descriptor in the file
194
195    TString path;
196    if( !filename[0] ) {
197       path += gSystem->Getenv( "ALICE_ROOT" );
198       path += fgkDescriptorFileName;
199    }
200    else
201       path += filename;
202
203    if( gSystem->AccessPathName( path.Data() ) ) {
204       AliErrorGeneral( "AliTriggerDescriptor", Form( "file (%s) not found", path.Data() ) );
205       return NULL;
206    }
207
208    TObjArray* desArray = new TObjArray();
209
210    TFile file( path.Data(), "READ" );
211    if( file.IsZombie() ) {
212       AliErrorGeneral( "AliTriggerDescriptor", Form( "Error opening file (%s)", path.Data() ) );
213       return NULL;
214    }
215
216    file.ReadAll();
217
218    TKey* key;
219    TIter next( file.GetListOfKeys() );
220    while( (key = (TKey*)next()) ) {
221       TObject* obj = key->ReadObj();
222       if( obj->InheritsFrom( "AliTriggerDescriptor" ) ) {
223          desArray->AddLast( obj );
224       }
225    }
226    file.Close();
227
228    return desArray;
229 }
230
231 //_____________________________________________________________________________
232 void AliTriggerDescriptor::WriteDescriptor( const char* filename )
233 {
234    // Load one pre-created Descriptors from database/file that match
235    // with the input string 'descriptor'
236    // Ej: "Pb-Pb" or "p-p-DIMUON CALIBRATION-CENTRAL-BARREL"
237
238    // Load the selected descriptor
239    TString path;
240    if( !filename[0] ) {
241       path += gSystem->Getenv("ALICE_ROOT");
242       path += fgkDescriptorFileName;
243    }
244    else
245       path += filename;
246
247    TFile file( path.Data(), "UPDATE" );
248    if( file.IsZombie() ) {
249       AliErrorGeneral( "AliTriggerDescriptor", 
250                         Form( "Can't open file (%s)", path.Data() ) );
251       return;
252    }
253
254    Bool_t result = (Write( GetName(), TObject::kOverwrite ) != 0);
255    if( !result )
256       AliErrorGeneral( "AliTriggerDescriptor",
257                         Form( "Can't write entry to file <%s>!", path.Data() ) );
258    file.Close();
259 }
260
261 //_____________________________________________________________________________
262 Bool_t AliTriggerDescriptor::CheckInputsConditions( TString& configfile )
263 {
264    // To be used on the pre-creation of Descriptors to check if the
265    // conditions have valid inputs names.
266    //
267    // Initiate detectors modules from a Config file
268    // Ask to each active module present in the fDetectorCluster
269    // to create a Trigger detector and retrive the inputs from it
270    // to create a list of inputs.
271    // Each condition in the descriptor is then checked agains 
272    // the list of inputs
273
274
275    if (!gAlice) {
276       AliError( "no gAlice object. Restart aliroot and try again." );
277       return kFALSE;
278    }
279    if (gAlice->Modules()->GetEntries() > 0) {
280       AliError( "gAlice was already run. Restart aliroot and try again." );
281       return kFALSE;
282    }
283
284    AliInfo( Form( "initializing gAlice with config file %s",
285             configfile.Data() ) );
286    StdoutToAliInfo( StderrToAliError(
287       gAlice->Init( configfile.Data() );
288    ););
289
290    AliRunLoader* runLoader = gAlice->GetRunLoader();
291    if( !runLoader ) {
292       AliError( Form( "gAlice has no run loader object. "
293                       "Check your config file: %s", configfile.Data() ) );
294       return kFALSE;
295    }
296
297    // get the possible inputs to check the condition
298    TObjArray inputs;
299    TObjArray* detArray = runLoader->GetAliRun()->Detectors();
300
301    TString detStr = fDetectorCluster;
302    for( Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++ ) {
303       AliModule* det = (AliModule*) detArray->At(iDet);
304       if( !det || !det->IsActive() ) continue;
305       if( IsSelected( det->GetName(), detStr ) ) {
306          AliInfo( Form( "Creating inputs for %s", det->GetName() ) );
307          AliTriggerDetector* dtrg = det->CreateTriggerDetector();
308          dtrg->CreateInputs();
309          TObjArray* detInp = dtrg->GetInputs();
310          for( Int_t i=0; i<detInp->GetEntriesFast(); i++ ) {
311             AliInfo( Form( "Adding input %s", ((AliTriggerInput*)detInp->At(i))->GetName() ) );
312             inputs.AddLast( detInp->At(i) );
313          }
314       }
315    }
316
317    // check if the condition is compatible with the triggers inputs
318    Int_t ncond = fConditions.GetEntriesFast();
319    for( Int_t j=0; j<ncond; j++ ) {
320       AliTriggerCondition* cond = (AliTriggerCondition*)(fConditions.At( j ));
321       if( !(cond->CheckInputs( inputs )) ) return kFALSE;
322    }
323
324    return kTRUE;
325 }
326
327
328 //_____________________________________________________________________________
329 void AliTriggerDescriptor::Print( const Option_t*  ) const
330 {
331    // Print
332    cout << "Trigger Descriptor:"  << endl;
333    cout << "  Name:             " << GetName() << endl; 
334    cout << "  Description:      " << GetTitle() << endl;
335    cout << "  Detector Cluster: " << fDetectorCluster << endl;
336
337 //   Int_t ninputs = fInputs->GetEntriesFast();
338 //   for( Int_t i=0; i<ninputs; i++ ) {
339 //      AliTriggerInput* in = (AliTriggerInput*)fInputs->At(i)
340 //      in->Print();
341 //   }
342
343    Int_t ncond = fConditions.GetEntriesFast();
344    for( Int_t i=0; i<ncond; i++ ) {
345       AliTriggerCondition* in = (AliTriggerCondition*)fConditions.At(i);
346       in->Print();
347    }
348    cout << endl;
349 }
350
351
352 //////////////////////////////////////////////////////////////////////////////
353 // Helper method
354
355 //_____________________________________________________________________________
356 Bool_t AliTriggerDescriptor::IsSelected( TString detName, TString& detectors ) const
357 {
358    // check whether detName is contained in detectors
359    // if yes, it is removed from detectors
360
361    // check if all detectors are selected
362    if( (detectors.CompareTo("ALL") == 0 ) ||
363         detectors.BeginsWith("ALL ") ||
364         detectors.EndsWith(" ALL") ||
365         detectors.Contains(" ALL ") ) {
366       detectors = "ALL";
367       return kTRUE;
368    }
369
370    // search for the given detector
371    Bool_t result = kFALSE;
372    if( (detectors.CompareTo( detName ) == 0) ||
373         detectors.BeginsWith( detName+" " ) ||
374         detectors.EndsWith( " "+detName ) ||
375         detectors.Contains( " "+detName+" " ) ) {
376       detectors.ReplaceAll( detName, "" );
377       result = kTRUE;
378    }
379
380    // clean up the detectors string
381    while( detectors.Contains("  ") )  detectors.ReplaceAll( "  ", " " );
382    while( detectors.BeginsWith(" ") ) detectors.Remove( 0, 1 );
383    while( detectors.EndsWith(" ") )   detectors.Remove( detectors.Length()-1, 1 );
384
385    return result;
386 }