]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliTriggerDescriptor.cxx
Added method GetDeltaForBranch getting the delta transformation, for a sensitive...
[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 <TList.h>
66 #include <TMap.h>
67 #include <TFile.h>
68
69 #include "AliLog.h"
70 #include "AliRun.h"
71 #include "AliRunLoader.h"
72 #include "AliModule.h"
73
74 #include "AliCDBManager.h"
75 #include "AliCDBPath.h"
76 #include "AliCDBEntry.h"
77
78 #include "AliTriggerInput.h"
79 #include "AliTriggerDetector.h"
80 #include "AliTriggerCondition.h"
81 #include "AliTriggerDescriptor.h"
82
83
84 ClassImp(AliTriggerDescriptor)
85
86 //_____________________________________________________________________________
87 const char* AliTriggerDescriptor::fgkDetectorName[AliTriggerDescriptor::fgkNDetectors] =
88              { "ITS", "TRD", "PHOS", "EMCAL", "MUON", "ZDC", "T0", "VZERO", "ACORDE", "TOF" };
89
90 const TString AliTriggerDescriptor::fgkDescriptorFileName("/data/triggerDescriptors.root");
91
92 //_____________________________________________________________________________
93 AliTriggerDescriptor::AliTriggerDescriptor():
94   TNamed(),
95   fDetectorCluster(""),
96   fConditions()
97 {
98 }
99
100 //_____________________________________________________________________________
101 AliTriggerDescriptor::AliTriggerDescriptor( TString & name, TString & description ):
102   TNamed( name, description ),
103   fDetectorCluster(""),
104   fConditions()
105 {
106 }
107
108 //_____________________________________________________________________________
109 AliTriggerDescriptor::AliTriggerDescriptor( const AliTriggerDescriptor& des ):
110   TNamed( des ),
111   fDetectorCluster( des.fDetectorCluster ),
112   fConditions()
113 {
114    // Copy constructor
115    Int_t ncond = des.fConditions.GetEntriesFast();
116    for( Int_t j=0; j<ncond; j++ ) {
117       AddCondition( new AliTriggerCondition( *(AliTriggerCondition*)(des.fConditions.At( j )) ) );
118    }
119 }
120
121 //______________________________________________________________________________
122 AliTriggerDescriptor& AliTriggerDescriptor::operator=(const AliTriggerDescriptor& des)
123 {
124    // AliTriggerDescriptor assignment operator.
125
126    if (this != &des) {
127       TNamed::operator=(des);
128       fDetectorCluster = des.fDetectorCluster;
129       fConditions.Delete();
130       Int_t ncond = des.fConditions.GetEntriesFast();
131       for( Int_t j=0; j<ncond; j++ ) {
132          AddCondition( new AliTriggerCondition( *(AliTriggerCondition*)(des.fConditions.At( j )) ) );
133       }
134    }
135    return *this;
136 }
137
138 //_____________________________________________________________________________
139 Bool_t AliTriggerDescriptor::AddDetectorCluster( TString & cluster )
140 {
141    // Add a List of Detectors to be read together (Detector Cluster)
142    // Ej "TO VO ZDC MUON" or "ALL"
143
144    TString olddet = fDetectorCluster;
145    TString newdet = cluster;
146    for( Int_t iDet = 0; iDet < fgkNDetectors; iDet++ ) {
147       if( IsSelected( fgkDetectorName[iDet], newdet ) && !IsSelected( fgkDetectorName[iDet], olddet ) ) {
148          // Add the detector
149          fDetectorCluster.Append( " " );
150          fDetectorCluster.Append( fgkDetectorName[iDet] );
151       }
152    }
153
154    // check if there are no trigger detectors (E.g. TPC)
155    if ((newdet.CompareTo("ALL") != 0) && !newdet.IsNull()) {
156       AliError( Form("the following detectors are not trigger detectors: %s",
157                 newdet.Data() ) );
158       return kFALSE;
159    }
160
161    return kTRUE;
162 }
163
164 //_____________________________________________________________________________
165 void AliTriggerDescriptor::AddCondition( TString & cond, TString & name, TString & description, ULong64_t mask   )
166 {
167    // Add a new condition
168    AliTriggerCondition* acond = new AliTriggerCondition( cond, name, description, mask );
169    fConditions.AddLast( acond );
170 }
171
172 //_____________________________________________________________________________
173 AliTriggerDescriptor* AliTriggerDescriptor::LoadDescriptor( TString & descriptor, const char* filename )
174 {
175    // Load one pre-created Descriptors from database/file that match
176    // with the input string 'descriptor'
177    // Ej: "Pb-Pb" or "p-p-DIMUON CALIBRATION-CENTRAL-BARREL"
178    // Load the selected descriptor
179   /*TString path;
180    if( !filename[0] ) {
181       path += gSystem->Getenv("ALICE_ROOT");
182       path += fgkDescriptorFileName;
183    }
184    else
185       path += filename;
186
187    if( gSystem->AccessPathName( path.Data() ) ) {
188       AliErrorGeneral( "AliTriggerDescriptor", Form( "file (%s) not found", path.Data() ) );
189       return NULL;
190    }
191
192    TFile file( path.Data(), "READ" );
193    AliTriggerDescriptor* des = (AliTriggerDescriptor*)(file.Get( descriptor.Data() ));
194
195    file.Close();
196
197    return des;*/
198   AliTriggerDescriptor *des = 0x0;
199   cout<<"GETTING TRIGGER DESCRIPTORS FROM CDB!!!"<<endl;
200  
201   AliCDBPath path("GRP","CTP","Trigger");
202         
203   AliCDBEntry *entry=AliCDBManager::Instance()->Get(path.GetPath());
204   if(!entry) AliFatalClass("Couldn't load trigger description data from CDB!");
205
206   TList *list = (TList *) entry->GetObject();
207   for(Int_t i =  0; i < list->GetEntries(); i++) {
208     TMap *map = (TMap *)list->At(i);
209     des = (AliTriggerDescriptor *)map->GetValue(descriptor.Data());
210     if(des) return des;
211   }
212
213   return des;
214 }
215
216 //_____________________________________________________________________________
217 TObjArray* AliTriggerDescriptor::GetAvailableDescriptors( const char* filename )
218 {
219    // Return an array of descriptor in the file
220
221    TString path;
222    if( !filename[0] ) {
223       path += gSystem->Getenv( "ALICE_ROOT" );
224       path += fgkDescriptorFileName;
225    }
226    else
227       path += filename;
228
229    if( gSystem->AccessPathName( path.Data() ) ) {
230       AliErrorGeneral( "AliTriggerDescriptor", Form( "file (%s) not found", path.Data() ) );
231       return NULL;
232    }
233
234    TObjArray* desArray = new TObjArray();
235
236    TFile file( path.Data(), "READ" );
237    if( file.IsZombie() ) {
238       AliErrorGeneral( "AliTriggerDescriptor", Form( "Error opening file (%s)", path.Data() ) );
239       return NULL;
240    }
241
242    file.ReadAll();
243
244    TKey* key;
245    TIter next( file.GetListOfKeys() );
246    while( (key = (TKey*)next()) ) {
247       TObject* obj = key->ReadObj();
248       if( obj->InheritsFrom( "AliTriggerDescriptor" ) ) {
249          desArray->AddLast( obj );
250       }
251    }
252    file.Close();
253
254    return desArray;
255 }
256
257 //_____________________________________________________________________________
258 void AliTriggerDescriptor::WriteDescriptor( const char* filename )
259 {
260    // Load one pre-created Descriptors from database/file that match
261    // with the input string 'descriptor'
262    // Ej: "Pb-Pb" or "p-p-DIMUON CALIBRATION-CENTRAL-BARREL"
263
264    // Load the selected descriptor
265    TString path;
266    if( !filename[0] ) {
267       path += gSystem->Getenv("ALICE_ROOT");
268       path += fgkDescriptorFileName;
269    }
270    else
271       path += filename;
272
273    TFile file( path.Data(), "UPDATE" );
274    if( file.IsZombie() ) {
275       AliErrorGeneral( "AliTriggerDescriptor", 
276                         Form( "Can't open file (%s)", path.Data() ) );
277       return;
278    }
279
280    Bool_t result = (Write( GetName(), TObject::kOverwrite ) != 0);
281    if( !result )
282       AliErrorGeneral( "AliTriggerDescriptor",
283                         Form( "Can't write entry to file <%s>!", path.Data() ) );
284    file.Close();
285 }
286
287 //_____________________________________________________________________________
288 Bool_t AliTriggerDescriptor::CheckInputsConditions( TString& configfile )
289 {
290    // To be used on the pre-creation of Descriptors to check if the
291    // conditions have valid inputs names.
292    //
293    // Initiate detectors modules from a Config file
294    // Ask to each active module present in the fDetectorCluster
295    // to create a Trigger detector and retrive the inputs from it
296    // to create a list of inputs.
297    // Each condition in the descriptor is then checked agains 
298    // the list of inputs
299
300
301    if (!gAlice) {
302       AliError( "no gAlice object. Restart aliroot and try again." );
303       return kFALSE;
304    }
305    if (gAlice->Modules()->GetEntries() > 0) {
306       AliError( "gAlice was already run. Restart aliroot and try again." );
307       return kFALSE;
308    }
309
310    AliInfo( Form( "initializing gAlice with config file %s",
311             configfile.Data() ) );
312    StdoutToAliInfo( StderrToAliError(
313       gAlice->Init( configfile.Data() );
314    ););
315
316    AliRunLoader* runLoader = gAlice->GetRunLoader();
317    if( !runLoader ) {
318       AliError( Form( "gAlice has no run loader object. "
319                       "Check your config file: %s", configfile.Data() ) );
320       return kFALSE;
321    }
322
323    // get the possible inputs to check the condition
324    TObjArray inputs;
325    TObjArray* detArray = runLoader->GetAliRun()->Detectors();
326
327    TString detStr = fDetectorCluster;
328    for( Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++ ) {
329       AliModule* det = (AliModule*) detArray->At(iDet);
330       if( !det || !det->IsActive() ) continue;
331       if( IsSelected( det->GetName(), detStr ) ) {
332          AliInfo( Form( "Creating inputs for %s", det->GetName() ) );
333          AliTriggerDetector* dtrg = det->CreateTriggerDetector();
334          dtrg->CreateInputs();
335          TObjArray* detInp = dtrg->GetInputs();
336          for( Int_t i=0; i<detInp->GetEntriesFast(); i++ ) {
337             AliInfo( Form( "Adding input %s", ((AliTriggerInput*)detInp->At(i))->GetName() ) );
338             inputs.AddLast( detInp->At(i) );
339          }
340       }
341    }
342
343    // check if the condition is compatible with the triggers inputs
344    Int_t ncond = fConditions.GetEntriesFast();
345    Bool_t check = kTRUE;
346    ULong64_t mask = 0L;
347    for( Int_t j=0; j<ncond; j++ ) {
348       AliTriggerCondition* cond = (AliTriggerCondition*)(fConditions.At( j ));
349       if( !(cond->CheckInputs( inputs )) ) check = kFALSE;
350       else AliInfo( Form( "Condition (%s) inputs names OK, class mask (0x%Lx)",
351                     cond->GetName(), cond->GetMask( ) ) );
352       // check if condition mask is duplicated
353       if( mask & cond->GetMask() ) {
354          AliError( Form("Condition (%s). The class mask (0x%Lx) is ambiguous. It was previous defined",
355                    cond->GetName(), cond->GetMask()  ) );
356          check = kFALSE;
357       }
358       mask |= cond->GetMask();
359    }
360
361    return check;
362 }
363
364
365 //_____________________________________________________________________________
366 void AliTriggerDescriptor::Print( const Option_t*  ) const
367 {
368    // Print
369    cout << "Trigger Descriptor:"  << endl;
370    cout << "  Name:             " << GetName() << endl; 
371    cout << "  Description:      " << GetTitle() << endl;
372    cout << "  Detector Cluster: " << fDetectorCluster << endl;
373
374 //   Int_t ninputs = fInputs->GetEntriesFast();
375 //   for( Int_t i=0; i<ninputs; i++ ) {
376 //      AliTriggerInput* in = (AliTriggerInput*)fInputs->At(i)
377 //      in->Print();
378 //   }
379
380    Int_t ncond = fConditions.GetEntriesFast();
381    for( Int_t i=0; i<ncond; i++ ) {
382       AliTriggerCondition* in = (AliTriggerCondition*)fConditions.At(i);
383       in->Print();
384    }
385    cout << endl;
386 }
387
388
389 //////////////////////////////////////////////////////////////////////////////
390 // Helper method
391
392 //_____________________________________________________________________________
393 Bool_t AliTriggerDescriptor::IsSelected( TString detName, TString& detectors ) const
394 {
395    // check whether detName is contained in detectors
396    // if yes, it is removed from detectors
397
398    // check if all detectors are selected
399    if( (detectors.CompareTo("ALL") == 0 ) ||
400         detectors.BeginsWith("ALL ") ||
401         detectors.EndsWith(" ALL") ||
402         detectors.Contains(" ALL ") ) {
403       detectors = "ALL";
404       return kTRUE;
405    }
406
407    // search for the given detector
408    Bool_t result = kFALSE;
409    if( (detectors.CompareTo( detName ) == 0) ||
410         detectors.BeginsWith( detName+" " ) ||
411         detectors.EndsWith( " "+detName ) ||
412         detectors.Contains( " "+detName+" " ) ) {
413       detectors.ReplaceAll( detName, "" );
414       result = kTRUE;
415    }
416
417    // clean up the detectors string
418    while( detectors.Contains("  ") )  detectors.ReplaceAll( "  ", " " );
419    while( detectors.BeginsWith(" ") ) detectors.Remove( 0, 1 );
420    while( detectors.EndsWith(" ") )   detectors.Remove( detectors.Length()-1, 1 );
421
422    return result;
423 }