]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliTriggerDescriptor.cxx
just Fixing Log info
[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 //   AliTriggerDescriptor::WriteDescriptor( &descrip );
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 }
104
105 //_____________________________________________________________________________
106 Bool_t AliTriggerDescriptor::AddDetectorCluster( TString & cluster )
107 {
108    // Add a List of Detectors to be read together (Detector Cluster)
109    // Ej "TO VO ZDC MUON" or "ALL"
110
111    TString olddet = fDetectorCluster;
112    TString newdet = cluster;
113    for( Int_t iDet = 0; iDet < fgkNDetectors; iDet++ ) {
114       if( IsSelected( fgkDetectorName[iDet], newdet ) && !IsSelected( fgkDetectorName[iDet], olddet ) ) {
115          // Add the detector
116          fDetectorCluster.Append( " " );
117          fDetectorCluster.Append( fgkDetectorName[iDet] );
118       }
119    }
120
121    // check if there are no trigger detectors (E.g. TPC)
122    if ((newdet.CompareTo("ALL") != 0) && !newdet.IsNull()) {
123       AliError( Form("the following detectors are not trigger detectors: %s",
124                 newdet.Data() ) );
125       return kFALSE;
126    }
127
128    return kTRUE;
129 }
130
131 //_____________________________________________________________________________
132 void AliTriggerDescriptor::AddCondition( TString & cond, TString & name, TString & description, Long_t mask   )
133 {
134    // Add a new condition
135    AliTriggerCondition* acond = new AliTriggerCondition( cond, name, description, mask );
136    fConditions.AddLast( acond );
137 }
138
139
140 //_____________________________________________________________________________
141 AliTriggerDescriptor* AliTriggerDescriptor::LoadDescriptor( TString & descriptor, const char* filename )
142 {
143    // Load one pre-created Descriptors from database/file that match
144    // with the input string 'descriptor'
145    // Ej: "Pb-Pb" or "p-p-DIMUON CALIBRATION-CENTRAL-BARREL"
146
147    // Load the selected descriptor
148    TString path;
149    if( !filename[0] ) {
150       path += gSystem->Getenv("ALICE_ROOT");
151       path += fgkDescriptorFileName;
152    }
153    else
154       path += filename;
155
156    if( gSystem->AccessPathName( path.Data() ) ) {
157       AliErrorGeneral( "AliTriggerDescriptor", Form( "file (%s) not found", path.Data() ) );
158       return NULL;
159    }
160
161    TFile file( path.Data(), "READ" );
162    AliTriggerDescriptor* des = (AliTriggerDescriptor*)(file.Get( descriptor.Data() ));
163
164    file.Close();
165
166    return des;
167 }
168
169 //_____________________________________________________________________________
170 TObjArray* AliTriggerDescriptor::GetAvailableDescriptors( const char* filename )
171 {
172    // Return an array of descriptor in the file
173    
174    TString path;
175    if( !filename[0] ) {
176       path += gSystem->Getenv( "ALICE_ROOT" );
177       path += fgkDescriptorFileName;
178    }
179    else
180       path += filename;
181
182    if( gSystem->AccessPathName( path.Data() ) ) {
183       AliErrorGeneral( "AliTriggerDescriptor", Form( "file (%s) not found", path.Data() ) );
184       return NULL;
185    }
186
187    TObjArray* desArray = new TObjArray();
188    
189    TFile file( path.Data(), "READ" );
190    if( file.IsZombie() ) {
191       AliErrorGeneral( "AliTriggerDescriptor", Form( "Error opening file (%s)", path.Data() ) );
192       return NULL;
193    }
194    
195    file.ReadAll();
196
197    TKey* key;
198    TIter next( file.GetListOfKeys() );
199    while( (key = (TKey*)next()) ) {
200       TObject* obj = key->ReadObj();
201       if( obj->InheritsFrom( "AliTriggerDescriptor" ) ) {
202          desArray->AddLast( obj );
203       }
204    }
205    file.Close();
206
207    return desArray;
208 }
209
210 //_____________________________________________________________________________
211 void AliTriggerDescriptor::WriteDescriptor( const char* filename )
212 {
213    // Load one pre-created Descriptors from database/file that match
214    // with the input string 'descriptor'
215    // Ej: "Pb-Pb" or "p-p-DIMUON CALIBRATION-CENTRAL-BARREL"
216
217    // Load the selected descriptor
218    TString path;
219    if( !filename[0] ) {
220       path += gSystem->Getenv("ALICE_ROOT");
221       path += fgkDescriptorFileName;
222    }
223    else
224       path += filename;
225
226    TFile file( path.Data(), "UPDATE" );
227    if( file.IsZombie() ) {
228       AliErrorGeneral( "AliTriggerDescriptor", 
229                         Form( "Can't open file (%s)", path.Data() ) );
230       return;
231    }
232    
233    Bool_t result = (Write( GetName(), TObject::kOverwrite ) != 0);
234    if( !result )
235       AliErrorGeneral( "AliTriggerDescriptor",
236                         Form( "Can't write entry to file <%s>!", path.Data() ) );
237    file.Close();
238 }
239
240
241
242
243 //_____________________________________________________________________________
244 Bool_t AliTriggerDescriptor::CheckInputsConditions( TString& configfile )
245 {
246    // To be used on the pre-creation of Descriptors to check if the
247    // conditions have valid inputs names.
248    //
249    // Initiate detectors modules from a Config file
250    // Ask to each active module present in the fDetectorCluster
251    // to create a Trigger detector and retrive the inputs from it
252    // to create a list of inputs.
253    // Each condition in the descriptor is then checked agains 
254    // the list of inputs
255
256
257    if (!gAlice) {
258       AliError( "no gAlice object. Restart aliroot and try again." );
259       return kFALSE;
260    }
261    if (gAlice->Modules()->GetEntries() > 0) {
262       AliError( "gAlice was already run. Restart aliroot and try again." );
263       return kFALSE;
264    }
265
266    AliInfo( Form( "initializing gAlice with config file %s",
267             configfile.Data() ) );
268    StdoutToAliInfo( StderrToAliError(
269       gAlice->Init( configfile.Data() );
270    ););
271
272    AliRunLoader* runLoader = gAlice->GetRunLoader();
273    if( !runLoader ) {
274       AliError( Form( "gAlice has no run loader object. "
275                       "Check your config file: %s", configfile.Data() ) );
276       return kFALSE;
277    }
278
279    // get the possible inputs to check the condition
280    TObjArray inputs;
281    TObjArray* detArray = runLoader->GetAliRun()->Detectors();
282
283    TString detStr = fDetectorCluster;
284    for( Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++ ) {
285       AliModule* det = (AliModule*) detArray->At(iDet);
286       if( !det || !det->IsActive() ) continue;
287       if( IsSelected( det->GetName(), detStr ) ) {
288          AliInfo( Form( "Creating inputs for %s", det->GetName() ) );
289          AliTriggerDetector* dtrg = det->CreateTriggerDetector();
290          dtrg->CreateInputs();
291          TObjArray* detInp = dtrg->GetInputs();
292          for( Int_t i=0; i<detInp->GetEntriesFast(); i++ ) {
293             AliInfo( Form( "Adding input %s", ((AliTriggerInput*)detInp->At(i))->GetName() ) );
294             inputs.AddLast( detInp->At(i) );
295          }
296       }
297    }
298
299    // check if the condition is compatible with the triggers inputs
300    Int_t ncond = fConditions.GetEntriesFast();
301    for( Int_t j=0; j<ncond; j++ ) {
302       AliTriggerCondition* cond = (AliTriggerCondition*)(fConditions.At( j ));
303       if( !(cond->CheckInputs( inputs )) ) return kFALSE;
304    }
305
306    return kTRUE;
307 }
308
309
310 //_____________________________________________________________________________
311 void AliTriggerDescriptor::Print( const Option_t*  ) const
312 {
313    // Print
314    cout << "Trigger Descriptor:"  << endl;
315    cout << "  Name:             " << GetName() << endl; 
316    cout << "  Description:      " << GetTitle() << endl;
317    cout << "  Detector Cluster: " << fDetectorCluster << endl;
318
319 //   Int_t ninputs = fInputs->GetEntriesFast();
320 //   for( Int_t i=0; i<ninputs; i++ ) {
321 //      AliTriggerInput* in = (AliTriggerInput*)fInputs->At(i)
322 //      in->Print();
323 //   }
324
325    Int_t ncond = fConditions.GetEntriesFast();
326    for( Int_t i=0; i<ncond; i++ ) {
327       AliTriggerCondition* in = (AliTriggerCondition*)fConditions.At(i);
328       in->Print();
329    }
330    cout << endl;
331 }
332
333
334 //////////////////////////////////////////////////////////////////////////////
335 // Helper method
336
337 //_____________________________________________________________________________
338 Bool_t AliTriggerDescriptor::IsSelected( TString detName, TString& detectors ) const
339 {
340    // check whether detName is contained in detectors
341    // if yes, it is removed from detectors
342
343    // check if all detectors are selected
344    if( (detectors.CompareTo("ALL") == 0 ) ||
345         detectors.BeginsWith("ALL ") ||
346         detectors.EndsWith(" ALL") ||
347         detectors.Contains(" ALL ") ) {
348       detectors = "ALL";
349       return kTRUE;
350    }
351
352    // search for the given detector
353    Bool_t result = kFALSE;
354    if( (detectors.CompareTo( detName ) == 0) ||
355         detectors.BeginsWith( detName+" " ) ||
356         detectors.EndsWith( " "+detName ) ||
357         detectors.Contains( " "+detName+" " ) ) {
358       detectors.ReplaceAll( detName, "" );
359       result = kTRUE;
360    }
361
362    // clean up the detectors string
363    while( detectors.Contains("  ") )  detectors.ReplaceAll( "  ", " " );
364    while( detectors.BeginsWith(" ") ) detectors.Remove( 0, 1 );
365    while( detectors.EndsWith(" ") )   detectors.Remove( detectors.Length()-1, 1 );
366
367    return result;
368 }