1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////////////////
20 // This class for running the Central Trigger Processor //
23 // Load Configuration //
24 // Make a list the trigger detectors involved (from the classes) //
25 // For the each event //
26 // Run the Trigger for the each detector //
28 // Check the trigger classes //
29 // Create the class mask //
33 ///////////////////////////////////////////////////////////////////////////////
36 #include <TObjString.h>
37 #include <TObjArray.h>
38 #include <TStopwatch.h>
44 #include "AliRunLoader.h"
45 #include "AliLoader.h"
46 #include "AliModule.h"
48 #include "AliTriggerInput.h"
49 #include "AliTriggerDetector.h"
50 #include "AliTriggerConfiguration.h"
51 #include "AliTriggerClass.h"
52 #include "AliTriggerCluster.h"
53 #include "AliCentralTrigger.h"
54 #include "AliDetectorEventHeader.h"
55 #include "AliHeader.h"
57 #include "AliCDBManager.h"
58 #include "AliCDBPath.h"
59 #include "AliCDBEntry.h"
65 ClassImp( AliCentralTrigger )
67 //_____________________________________________________________________________
68 AliCentralTrigger::AliCentralTrigger() :
78 // Default constructor
82 //_____________________________________________________________________________
83 AliCentralTrigger::AliCentralTrigger( TString & config ) :
93 // Default constructor
94 LoadConfiguration( config );
97 //_____________________________________________________________________________
98 AliCentralTrigger::~AliCentralTrigger()
101 DeleteConfiguration();
104 //_____________________________________________________________________________
105 void AliCentralTrigger::DeleteConfiguration()
107 // Delete the active configuration
109 fClassMaskNext50 = 0;
111 fL0TriggerInputs = 0;
112 fL1TriggerInputs = 0;
113 fL2TriggerInputs = 0;
114 if (fConfiguration) {
115 if (IsOwner()) delete fConfiguration;
116 fConfiguration = 0x0;
120 //_____________________________________________________________________________
121 void AliCentralTrigger::Reset()
123 // Reset Class Mask and classes
125 fClassMaskNext50 = 0;
127 fL0TriggerInputs = 0;
128 fL1TriggerInputs = 0;
129 fL2TriggerInputs = 0;
131 if (fConfiguration) {
132 const TObjArray& classesArray = fConfiguration->GetClasses();
133 Int_t nclasses = classesArray.GetEntriesFast();
134 for( Int_t j=0; j<nclasses; j++ ) {
135 AliTriggerClass* trclass = (AliTriggerClass*)classesArray.At( j );
140 //_____________________________________________________________________________
141 void AliCentralTrigger::MakeBranch( TString name, TTree * tree )
143 // Make a branch to store only trigger class mask event by event
147 AliDebug( 1, "Got Tree from folder." );
148 TBranch* branch = tree->GetBranch( name );
149 if( branch == 0x0 ) {
150 //branch = tree->Branch( name, &(this->fClassMask), "fClassMask/l:fClusterMask/i:fL0TriggerInputs/i:fL1TriggerInputs/i:fL2TriggerInputs/s" );
151 AliDebug( 1, "Creating new branch" );
152 branch = tree->Branch( name, &(this->fClassMask), "fClassMask/l:fClassMaskNext50/l:fClusterMask/i:fL0TriggerInputs/i:fL1TriggerInputs/i:fL2TriggerInputs/s" );
153 branch->SetAutoDelete( kFALSE );
156 AliDebug( 1, "Got Branch from Tree" );
157 branch->SetAddress( &(this->fClassMask) );
161 //_____________________________________________________________________________
162 Bool_t AliCentralTrigger::LoadConfiguration( TString & config )
164 // Load one and only one pre-created COnfiguration from database/file that match
165 // with the input string 'config'
166 // Ej: "p-p", "Pb-Pb" or "p-p-DIMUON CALIBRATION-CENTRAL-BARREL"
168 // Delete the active configuration, if any
169 DeleteConfiguration();
171 // Load the selected configuration
172 if (!config.IsNull()) {
173 fConfiguration = AliTriggerConfiguration::LoadConfiguration( config );
178 AliError( Form( "Valid TriggerConfiguration (%s) is not found ! Disabling the trigger simulation !", config.Data() ) );
183 // Load one and only one trigger descriptor from CDB
184 AliInfo( "Getting trigger configuration from OCDB!" );
186 AliCDBPath path( "GRP", "CTP", "Config" );
188 AliCDBEntry *entry=AliCDBManager::Instance()->Get(path.GetPath());
190 if( !entry ) AliFatal( "Couldn't load trigger description data from CDB!" );
192 fConfiguration = (AliTriggerConfiguration *)entry->GetObject();
196 AliError( "No valid configuration is found in the CDB ! Disabling the trigger simulation !" );
202 //_____________________________________________________________________________
203 TString AliCentralTrigger::GetDetectors()
205 // return TString with the detectors (modules) to be used for triggering
210 result = fConfiguration->GetTriggeringModules();
215 //_____________________________________________________________________________
216 Bool_t AliCentralTrigger::RunTrigger( AliRunLoader* runLoader, const char *detectors )
220 if( !fConfiguration ) {
221 AliError( "No trigger configuration loaded, skipping trigger" );
225 TTree *tree = runLoader->TreeCT();
227 AliError( "No folder with trigger loaded, skipping trigger" );
231 TStopwatch stopwatch;
234 AliInfo( Form(" Triggering Detectors: %s \n", GetDetectors().Data() ) );
235 AliInfo( Form(" Detectors with digits: %s \n", detectors ) );
237 // Process each event
238 for( Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++ ) {
239 runLoader->GetEvent( iEvent );
240 // Get detectors involve
241 TString detStr = GetDetectors();
242 TString detWithDigits = detectors;
243 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
246 fClassMaskNext50 = 0;
248 // Reset configuration object (inputs and classes)
249 fConfiguration->Reset();
250 TObjArray trgdetArray; // use as garbage collector
251 for( Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++ ) {
252 AliModule* det = (AliModule*) detArray->At( iDet );
253 if( !det || !det->IsActive() ) continue;
254 if( IsSelected(det->GetName(), detStr) &&
255 IsSelected(det->GetName(), detWithDigits) ) {
257 AliDebug(1,Form("Triggering from digits for %s", det->GetName() ) );
258 AliTriggerDetector* trgdet = det->CreateTriggerDetector();
259 trgdet->AssignInputs(fConfiguration->GetInputs());
260 TStopwatch stopwatchDet;
261 stopwatchDet.Start();
263 AliDebug(1, Form("Execution time for %s: R:%.2fs C:%.2fs",
264 det->GetName(), stopwatchDet.RealTime(), stopwatchDet.CpuTime() ) );
266 trgdetArray.AddLast( trgdet );
268 // Write trigger detector in Event folder in Digits file
269 TString loadername = det->GetName();
270 loadername.Append( "Loader" );
271 AliLoader * loader = runLoader->GetLoader( loadername );
273 AliDataLoader * dataLoader = loader->GetDigitsDataLoader();
274 if( !dataLoader->IsFileOpen() ) {
275 if( dataLoader->OpenFile( "UPDATE" ) ) {
276 AliWarning( Form( "\n\nCan't write trigger for %s\n", det->GetName() ) );
280 if( gFile && !gFile->IsWritable() ) {
281 gFile->ReOpen( "UPDATE" );
284 trgdet->Write( "Trigger", TObject::kOverwrite );
285 dataLoader->CloseFile();
287 else AliWarning( Form( "Not loader found for %s", det->GetName() ) );
291 // Check trigger conditions and create the trigger class mask
293 // Calculate trigger Input pattern
296 // Clear trigger detectors
297 trgdetArray.SetOwner();
298 trgdetArray.Delete();
300 if( (detStr.CompareTo( "ALL" ) != 0) && !detStr.IsNull() ) {
301 AliError( Form("the following detectors were not found: %s",
308 AliDebug(1, Form("Event:%d Class Mask:0x%llX 0x%llX", iEvent,fClassMask,fClassMaskNext50 ) );
312 // cout << endl << " Print " << endl;
316 runLoader->WriteTrigger( "OVERWRITE" );
320 //----------------------------------------------------------------------------
321 void AliCentralTrigger::TriggerInputs()
323 // Find which inputs are in configuration
324 // and calculate input pattern
329 const TObjArray& inputsArray = fConfiguration->GetInputs();
330 Int_t ninputs = inputsArray.GetEntriesFast();
331 for( Int_t j=0; j<ninputs; j++ ) {
332 AliTriggerInput* input = (AliTriggerInput*)inputsArray.At( j );
333 if(input->GetValue()){
334 UChar_t level=input->GetLevel();
335 if(level == 0) fL0TriggerInputs |= (input->GetMask());
336 else if(level == 1) fL1TriggerInputs |= (input->GetMask());
337 else if(level == 2) fL2TriggerInputs |= (input->GetMask());
339 AliError(Form("Unknown input level:%c:",level));
345 //_____________________________________________________________________________
346 ULong64_t AliCentralTrigger::TriggerClasses()
348 // Check trigger conditions and create the trigger class
349 // and trigger cluster masks
350 // Updated to 100 classes
351 // It seems nobody uses return value
353 fClassMaskNext50 = 0;
355 if (fConfiguration) {
356 const TObjArray& classesArray = fConfiguration->GetClasses();
357 Int_t nclasses = classesArray.GetEntriesFast();
358 for( Int_t j=0; j<nclasses; j++ ) {
359 AliTriggerClass* trclass = (AliTriggerClass*)classesArray.At( j );
360 trclass->Trigger( fConfiguration->GetInputs(), fConfiguration->GetFunctions() );
361 fClassMask |= trclass->GetValue();
362 fClassMaskNext50 |= trclass->GetValueNext50();
363 if (trclass->GetStatus()) {
364 AliTriggerCluster *trclust = trclass->GetCluster();
365 fClusterMask |= AliDAQ::DetectorPattern(trclust->GetDetectorsInCluster());
371 //_____________________________________________________________________________
372 TObjArray* AliCentralTrigger::GetFiredClasses() const
374 // return only the true conditions
376 TObjArray* result = new TObjArray();
378 if (fConfiguration) {
379 const TObjArray& classesArray = fConfiguration->GetClasses();
380 Int_t nclasses = classesArray.GetEntriesFast();
381 for( Int_t j=0; j<nclasses; j++ ) {
382 AliTriggerClass* trclass = (AliTriggerClass*)classesArray.At( j );
383 if( trclass->GetStatus() ) result->AddLast( trclass );
390 //_____________________________________________________________________________
391 void AliCentralTrigger::Print( const Option_t* ) const
394 cout << "Central Trigger: " << endl;
395 cout << " Trigger Class Mask: 1..50 0x" << hex << fClassMask << " 51-100 0x"<< fClassMaskNext50 << dec << endl;
396 if (fConfiguration) fConfiguration->Print();
401 //////////////////////////////////////////////////////////////////////////////
404 //_____________________________________________________________________________
405 Bool_t AliCentralTrigger::IsSelected( TString detName, TString& detectors ) const
407 // check whether detName is contained in detectors
408 // if yes, it is removed from detectors
410 // check if all detectors are selected
411 if( (detectors.CompareTo("ALL") == 0 ) ||
412 detectors.BeginsWith("ALL ") ||
413 detectors.EndsWith(" ALL") ||
414 detectors.Contains(" ALL ") ) {
419 // search for the given detector
420 Bool_t result = kFALSE;
421 if( (detectors.CompareTo( detName ) == 0) ||
422 detectors.BeginsWith( detName+" " ) ||
423 detectors.EndsWith( " "+detName ) ||
424 detectors.Contains( " "+detName+" " ) ) {
425 detectors.ReplaceAll( detName, "" );
429 // clean up the detectors string
430 while( detectors.Contains(" ") ) detectors.ReplaceAll( " ", " " );
431 while( detectors.BeginsWith(" ") ) detectors.Remove( 0, 1 );
432 while( detectors.EndsWith(" ") ) detectors.Remove( detectors.Length()-1, 1 );
437 //_____________________________________________________________________________
438 Bool_t AliCentralTrigger::CheckTriggeredDetectors() const
440 // Check the trigger mask, finds which trigger classes
441 // have been fired, load the corresponding trigger clusters and
442 // finally makes a list of the detectors that have been readout
443 // for each particular event. This list is then compared to the
444 // one stored in fClusterMask. Return value:
445 // true = two lists are equal
446 // false = two lists are not equal meaning wrong trigger config
449 if (!fConfiguration) {
450 AliError("The trigger confiration has not yet been loaded! Cross-check is not possible!");
455 // Make a cross-check so that to exclude wrong trigger configuration used
456 // Loop over the trigger classes
457 UInt_t clusterMask = 0;
458 const TObjArray& classesArray = fConfiguration->GetClasses();
459 Int_t nclasses = classesArray.GetEntriesFast();
460 for( Int_t j=0; j<nclasses; j++ ) {
461 AliTriggerClass* trclass = (AliTriggerClass*)classesArray.At( j );
462 if (trclass->GetMask() & fClassMask) { // class was fired
463 AliTriggerCluster *trclust = trclass->GetCluster();
464 clusterMask |= AliDAQ::DetectorPattern(trclust->GetDetectorsInCluster());
466 if (trclass->GetMaskNext50() & fClassMaskNext50) { // class was fired
467 AliTriggerCluster *trclust = trclass->GetCluster();
468 clusterMask |= AliDAQ::DetectorPattern(trclust->GetDetectorsInCluster());
471 // Compare the stored cluster mask with the one
472 // that we get from trigger classes
473 if (clusterMask != fClusterMask) {
474 if ((clusterMask & fClusterMask) == clusterMask) {
475 AliInfo(Form("Cluster mask from trigger classes (%x) and from data (%x) differ. Concurrent DAQ run(s) could be the reason.",
476 (UInt_t)clusterMask,(UInt_t)fClusterMask));
480 AliError(Form("Wrong cluster mask from trigger classes (%x), expecting (%x)! Loaded trigger configuration is possibly wrong!",
481 (UInt_t)clusterMask,(UInt_t)fClusterMask));