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 "AliModule.h"
47 #include "AliTriggerInput.h"
48 #include "AliTriggerDetector.h"
49 #include "AliTriggerConfiguration.h"
50 #include "AliTriggerClass.h"
51 #include "AliTriggerCluster.h"
52 #include "AliCentralTrigger.h"
53 #include "AliDetectorEventHeader.h"
54 #include "AliHeader.h"
56 #include "AliCDBManager.h"
57 #include "AliCDBPath.h"
58 #include "AliCDBEntry.h"
60 ClassImp( AliCentralTrigger )
62 //_____________________________________________________________________________
63 AliCentralTrigger::AliCentralTrigger() :
72 // Default constructor
76 //_____________________________________________________________________________
77 AliCentralTrigger::AliCentralTrigger( TString & config ) :
86 // Default constructor
87 LoadConfiguration( config );
90 //_____________________________________________________________________________
91 AliCentralTrigger::~AliCentralTrigger()
94 DeleteConfiguration();
97 //_____________________________________________________________________________
98 void AliCentralTrigger::DeleteConfiguration()
100 // Delete the active configuration
103 fL0TriggerInputs = 0;
104 fL1TriggerInputs = 0;
105 fL2TriggerInputs = 0;
106 if (fConfiguration) {
107 if (IsOwner()) delete fConfiguration;
108 fConfiguration = 0x0;
112 //_____________________________________________________________________________
113 void AliCentralTrigger::Reset()
115 // Reset Class Mask and classes
118 fL0TriggerInputs = 0;
119 fL1TriggerInputs = 0;
120 fL2TriggerInputs = 0;
122 if (fConfiguration) {
123 const TObjArray& classesArray = fConfiguration->GetClasses();
124 Int_t nclasses = classesArray.GetEntriesFast();
125 for( Int_t j=0; j<nclasses; j++ ) {
126 AliTriggerClass* trclass = (AliTriggerClass*)classesArray.At( j );
132 //_____________________________________________________________________________
133 void AliCentralTrigger::MakeBranch( TString name, TTree * tree )
135 // Make a branch to store only trigger class mask event by event
138 AliDebug( 1, "Got Tree from folder." );
139 TBranch* branch = tree->GetBranch( name );
140 if( branch == 0x0 ) {
141 AliDebug( 1, "Creating new branch" );
142 branch = tree->Branch( name, &(this->fClassMask), "fClassMask/l:fClusterMask/i:fL0TriggerInputs/i:fL1TriggerInputs/i:fL2TriggerInputs/s" );
143 branch->SetAutoDelete( kFALSE );
146 AliDebug( 1, "Got Branch from Tree" );
147 branch->SetAddress( &(this->fClassMask) );
152 //_____________________________________________________________________________
153 Bool_t AliCentralTrigger::LoadConfiguration( TString & config )
155 // Load one and only one pre-created COnfiguration from database/file that match
156 // with the input string 'config'
157 // Ej: "p-p", "Pb-Pb" or "p-p-DIMUON CALIBRATION-CENTRAL-BARREL"
159 // Delete the active configuration, if any
160 DeleteConfiguration();
162 // Load the selected configuration
163 if (!config.IsNull()) {
164 fConfiguration = AliTriggerConfiguration::LoadConfiguration( config );
169 AliError( Form( "Valid TriggerConfiguration (%s) is not found ! Disabling the trigger simulation !", config.Data() ) );
174 // Load one and only one trigger descriptor from CDB
175 AliInfo( "Getting trigger configuration from OCDB!" );
177 AliCDBPath path( "GRP", "CTP", "Config" );
179 AliCDBEntry *entry=AliCDBManager::Instance()->Get(path.GetPath());
181 if( !entry ) AliFatal( "Couldn't load trigger description data from CDB!" );
183 fConfiguration = (AliTriggerConfiguration *)entry->GetObject();
187 AliError( "No valid configuration is found in the CDB ! Disabling the trigger simulation !" );
193 //_____________________________________________________________________________
194 TString AliCentralTrigger::GetDetectors()
196 // return TString with the detectors (modules) to be used for triggering
201 result = fConfiguration->GetTriggeringModules();
206 //_____________________________________________________________________________
207 Bool_t AliCentralTrigger::RunTrigger( AliRunLoader* runLoader, const char *detectors )
211 if( !fConfiguration ) {
212 AliError( "No trigger configuration loaded, skipping trigger" );
216 TTree *tree = runLoader->TreeCT();
218 AliError( "No folder with trigger loaded, skipping trigger" );
222 TStopwatch stopwatch;
225 AliInfo( Form(" Triggering Detectors: %s \n", GetDetectors().Data() ) );
226 AliInfo( Form(" Detectors with digits: %s \n", detectors ) );
228 // Process each event
229 for( Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++ ) {
230 AliInfo( Form("Processing event %d", iEvent) );
231 runLoader->GetEvent( iEvent );
232 // Get detectors involve
233 TString detStr = GetDetectors();
234 TString detWithDigits = detectors;
235 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
239 // Reset configuration object (inputs and classes)
240 fConfiguration->Reset();
241 TObjArray trgdetArray; // use as garbage collector
242 for( Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++ ) {
243 AliModule* det = (AliModule*) detArray->At( iDet );
244 if( !det || !det->IsActive() ) continue;
245 if( IsSelected(det->GetName(), detStr) &&
246 IsSelected(det->GetName(), detWithDigits) ) {
248 AliDebug(1,Form("Triggering from digits for %s", det->GetName() ) );
249 AliTriggerDetector* trgdet = det->CreateTriggerDetector();
250 trgdet->AssignInputs(fConfiguration->GetInputs());
251 TStopwatch stopwatchDet;
252 stopwatchDet.Start();
254 AliDebug(1, Form("Execution time for %s: R:%.2fs C:%.2fs",
255 det->GetName(), stopwatchDet.RealTime(), stopwatchDet.CpuTime() ) );
257 trgdetArray.AddLast( trgdet );
259 // Write trigger detector in Event folder in Digits file
260 TString loadername = det->GetName();
261 loadername.Append( "Loader" );
262 AliLoader * loader = runLoader->GetLoader( loadername );
264 AliDataLoader * dataLoader = loader->GetDigitsDataLoader();
265 if( !dataLoader->IsFileOpen() ) {
266 if( dataLoader->OpenFile( "UPDATE" ) ) {
267 AliWarning( Form( "\n\nCan't write trigger for %s\n", det->GetName() ) );
271 if( gFile && !gFile->IsWritable() ) {
272 gFile->ReOpen( "UPDATE" );
275 trgdet->Write( "Trigger", TObject::kOverwrite );
276 dataLoader->CloseFile();
278 else AliWarning( Form( "Not loader found for %s", det->GetName() ) );
282 // Check trigger conditions and create the trigger class mask
284 // Calculate trigger Input pattern
287 // Clear trigger detectors
288 trgdetArray.SetOwner();
289 trgdetArray.Delete();
291 if( (detStr.CompareTo( "ALL" ) != 0) && !detStr.IsNull() ) {
292 AliError( Form("the following detectors were not found: %s",
299 AliInfo( Form("Trigger Class Mask:0x%X", fClassMask ) );
303 // cout << endl << " Print " << endl;
307 runLoader->WriteTrigger( "OVERWRITE" );
311 //----------------------------------------------------------------------------
312 void AliCentralTrigger::TriggerInputs()
314 // Find which inputs are in configuration
315 // and calculate input pattern
320 const TObjArray& inputsArray = fConfiguration->GetInputs();
321 Int_t ninputs = inputsArray.GetEntriesFast();
322 for( Int_t j=0; j<ninputs; j++ ) {
323 AliTriggerInput* input = (AliTriggerInput*)inputsArray.At( j );
324 if(input->GetValue()){
325 UChar_t level=input->GetLevel();
326 if(level == 0) fL0TriggerInputs |= (input->GetMask());
327 else if(level == 1) fL1TriggerInputs |= (input->GetMask());
328 else if(level == 2) fL2TriggerInputs |= (input->GetMask());
330 AliError(Form("Unknown input level:%c:",level));
336 //_____________________________________________________________________________
337 ULong64_t AliCentralTrigger::TriggerClasses()
339 // Check trigger conditions and create the trigger class
340 // and trigger cluster masks
343 if (fConfiguration) {
344 const TObjArray& classesArray = fConfiguration->GetClasses();
345 Int_t nclasses = classesArray.GetEntriesFast();
346 for( Int_t j=0; j<nclasses; j++ ) {
347 AliTriggerClass* trclass = (AliTriggerClass*)classesArray.At( j );
348 trclass->Trigger( fConfiguration->GetInputs(), fConfiguration->GetFunctions() );
349 fClassMask |= trclass->GetValue();
350 if (trclass->GetStatus()) {
351 AliTriggerCluster *trclust = trclass->GetCluster();
352 fClusterMask |= AliDAQ::DetectorPattern(trclust->GetDetectorsInCluster());
358 //_____________________________________________________________________________
359 TObjArray* AliCentralTrigger::GetFiredClasses() const
361 // return only the true conditions
363 TObjArray* result = new TObjArray();
365 if (fConfiguration) {
366 const TObjArray& classesArray = fConfiguration->GetClasses();
367 Int_t nclasses = classesArray.GetEntriesFast();
368 for( Int_t j=0; j<nclasses; j++ ) {
369 AliTriggerClass* trclass = (AliTriggerClass*)classesArray.At( j );
370 if( trclass->GetStatus() ) result->AddLast( trclass );
377 //_____________________________________________________________________________
378 void AliCentralTrigger::Print( const Option_t* ) const
381 cout << "Central Trigger: " << endl;
382 cout << " Trigger Class Mask: 0x" << hex << fClassMask << dec << endl;
383 if (fConfiguration) fConfiguration->Print();
388 //////////////////////////////////////////////////////////////////////////////
391 //_____________________________________________________________________________
392 Bool_t AliCentralTrigger::IsSelected( TString detName, TString& detectors ) const
394 // check whether detName is contained in detectors
395 // if yes, it is removed from detectors
397 // check if all detectors are selected
398 if( (detectors.CompareTo("ALL") == 0 ) ||
399 detectors.BeginsWith("ALL ") ||
400 detectors.EndsWith(" ALL") ||
401 detectors.Contains(" ALL ") ) {
406 // search for the given detector
407 Bool_t result = kFALSE;
408 if( (detectors.CompareTo( detName ) == 0) ||
409 detectors.BeginsWith( detName+" " ) ||
410 detectors.EndsWith( " "+detName ) ||
411 detectors.Contains( " "+detName+" " ) ) {
412 detectors.ReplaceAll( detName, "" );
416 // clean up the detectors string
417 while( detectors.Contains(" ") ) detectors.ReplaceAll( " ", " " );
418 while( detectors.BeginsWith(" ") ) detectors.Remove( 0, 1 );
419 while( detectors.EndsWith(" ") ) detectors.Remove( detectors.Length()-1, 1 );
424 //_____________________________________________________________________________
425 Bool_t AliCentralTrigger::CheckTriggeredDetectors() const
427 // Check the trigger mask, finds which trigger classes
428 // have been fired, load the corresponding trigger clusters and
429 // finally makes a list of the detectors that have been readout
430 // for each particular event. This list is then compared to the
431 // one stored in fClusterMask. Return value:
432 // true = two lists are equal
433 // false = two lists are not equal meaning wrong trigger config
436 if (!fConfiguration) {
437 AliError("The trigger confiration has not yet been loaded! Cross-check is not possible!");
442 // Make a cross-check so that to exclude wrong trigger configuration used
443 // Loop over the trigger classes
444 UInt_t clusterMask = 0;
445 const TObjArray& classesArray = fConfiguration->GetClasses();
446 Int_t nclasses = classesArray.GetEntriesFast();
447 for( Int_t j=0; j<nclasses; j++ ) {
448 AliTriggerClass* trclass = (AliTriggerClass*)classesArray.At( j );
449 if (trclass->GetMask() & fClassMask) { // class was fired
450 AliTriggerCluster *trclust = trclass->GetCluster();
451 clusterMask |= AliDAQ::DetectorPattern(trclust->GetDetectorsInCluster());
454 // Compare the stored cluster mask with the one
455 // that we get from trigger classes
456 if (clusterMask != fClusterMask) {
457 if ((clusterMask & fClusterMask) == clusterMask) {
458 AliInfo(Form("Cluster mask from trigger classes (%x) and from data (%x) differ. Concurrent DAQ run(s) could be the reason.",
459 (UInt_t)clusterMask,(UInt_t)fClusterMask));
463 AliError(Form("Wrong cluster mask from trigger classes (%x), expecting (%x)! Loaded trigger configuration is possibly wrong!",
464 (UInt_t)clusterMask,(UInt_t)fClusterMask));