]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/STEER/AliCentralTrigger.cxx
Get QA object only upon successful opening of the QA.root
[u/mrichter/AliRoot.git] / STEER / STEER / AliCentralTrigger.cxx
CommitLineData
a5a091ce 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 the Central Trigger Processor //
21// //
22// //
51f6d619 23// Load Configuration //
24// Make a list the trigger detectors involved (from the classes) //
a5a091ce 25// For the each event //
26// Run the Trigger for the each detector //
27// Get the inputs //
51f6d619 28// Check the trigger classes //
a5a091ce 29// Create the class mask //
30// Save result //
31// //
32// //
33///////////////////////////////////////////////////////////////////////////////
34
35#include <TString.h>
36#include <TObjString.h>
37#include <TObjArray.h>
38#include <TStopwatch.h>
bacbe0fd 39#include <TFile.h>
40#include <TTree.h>
a5a091ce 41
42#include "AliLog.h"
43#include "AliRun.h"
44#include "AliRunLoader.h"
c93255fe 45#include "AliLoader.h"
a5a091ce 46#include "AliModule.h"
47
48#include "AliTriggerInput.h"
49#include "AliTriggerDetector.h"
51f6d619 50#include "AliTriggerConfiguration.h"
51#include "AliTriggerClass.h"
52#include "AliTriggerCluster.h"
a5a091ce 53#include "AliCentralTrigger.h"
bacbe0fd 54#include "AliDetectorEventHeader.h"
55#include "AliHeader.h"
56
51f6d619 57#include "AliCDBManager.h"
58#include "AliCDBPath.h"
59#include "AliCDBEntry.h"
a5a091ce 60
66b0310c 61using std::endl;
62using std::cout;
63using std::hex;
64using std::dec;
a5a091ce 65ClassImp( AliCentralTrigger )
66
67//_____________________________________________________________________________
68AliCentralTrigger::AliCentralTrigger() :
69 TObject(),
75e3794b 70 fClassMask(0),
305085a6 71 fClassMaskNext50(0),
51f6d619 72 fClusterMask(0),
d3f1f232 73 fL0TriggerInputs(0),
74 fL1TriggerInputs(0),
75 fL2TriggerInputs(0),
51f6d619 76 fConfiguration(NULL)
a5a091ce 77{
78 // Default constructor
596735df 79 SetOwner();
a5a091ce 80}
81
82//_____________________________________________________________________________
51f6d619 83AliCentralTrigger::AliCentralTrigger( TString & config ) :
a5a091ce 84 TObject(),
75e3794b 85 fClassMask(0),
305085a6 86 fClassMaskNext50(0),
51f6d619 87 fClusterMask(0),
d3f1f232 88 fL0TriggerInputs(0),
89 fL1TriggerInputs(0),
90 fL2TriggerInputs(0),
51f6d619 91 fConfiguration(NULL)
a5a091ce 92{
93 // Default constructor
51f6d619 94 LoadConfiguration( config );
bacbe0fd 95}
96
a5a091ce 97//_____________________________________________________________________________
98AliCentralTrigger::~AliCentralTrigger()
99{
51f6d619 100 // Destructor
101 DeleteConfiguration();
bacbe0fd 102}
103
104//_____________________________________________________________________________
51f6d619 105void AliCentralTrigger::DeleteConfiguration()
bacbe0fd 106{
51f6d619 107 // Delete the active configuration
108 fClassMask = 0;
305085a6 109 fClassMaskNext50 = 0;
51f6d619 110 fClusterMask = 0;
d3f1f232 111 fL0TriggerInputs = 0;
112 fL1TriggerInputs = 0;
113 fL2TriggerInputs = 0;
596735df 114 if (fConfiguration) {
115 if (IsOwner()) delete fConfiguration;
116 fConfiguration = 0x0;
117 }
a5a091ce 118}
119
bacbe0fd 120//_____________________________________________________________________________
121void AliCentralTrigger::Reset()
122{
51f6d619 123 // Reset Class Mask and classes
bacbe0fd 124 fClassMask = 0;
305085a6 125 fClassMaskNext50 = 0;
51f6d619 126 fClusterMask = 0;
d3f1f232 127 fL0TriggerInputs = 0;
128 fL1TriggerInputs = 0;
129 fL2TriggerInputs = 0;
51f6d619 130
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 );
136 trclass->Reset();
137 }
bacbe0fd 138 }
139}
bacbe0fd 140//_____________________________________________________________________________
141void AliCentralTrigger::MakeBranch( TString name, TTree * tree )
142{
143 // Make a branch to store only trigger class mask event by event
305085a6 144 // Run2 payload
bacbe0fd 145
146 if( tree ) {
147 AliDebug( 1, "Got Tree from folder." );
148 TBranch* branch = tree->GetBranch( name );
149 if( branch == 0x0 ) {
305085a6 150 //branch = tree->Branch( name, &(this->fClassMask), "fClassMask/l:fClusterMask/i:fL0TriggerInputs/i:fL1TriggerInputs/i:fL2TriggerInputs/s" );
bacbe0fd 151 AliDebug( 1, "Creating new branch" );
305085a6 152 branch = tree->Branch( name, &(this->fClassMask), "fClassMask/l:fClassMaskNext50/l:fClusterMask/i:fL0TriggerInputs/i:fL1TriggerInputs/i:fL2TriggerInputs/s" );
bacbe0fd 153 branch->SetAutoDelete( kFALSE );
154 }
155 else {
156 AliDebug( 1, "Got Branch from Tree" );
157 branch->SetAddress( &(this->fClassMask) );
158 }
159 }
160}
a5a091ce 161//_____________________________________________________________________________
51f6d619 162Bool_t AliCentralTrigger::LoadConfiguration( TString & config )
a5a091ce 163{
51f6d619 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"
167
168 // Delete the active configuration, if any
169 DeleteConfiguration();
170
171 // Load the selected configuration
172 if (!config.IsNull()) {
173 fConfiguration = AliTriggerConfiguration::LoadConfiguration( config );
596735df 174 SetOwner();
51f6d619 175 if(fConfiguration)
176 return kTRUE;
177 else {
178 AliError( Form( "Valid TriggerConfiguration (%s) is not found ! Disabling the trigger simulation !", config.Data() ) );
179 return kFALSE;
180 }
181 }
182 else {
183 // Load one and only one trigger descriptor from CDB
7e88424f 184 AliInfo( "Getting trigger configuration from OCDB!" );
51f6d619 185
186 AliCDBPath path( "GRP", "CTP", "Config" );
187
188 AliCDBEntry *entry=AliCDBManager::Instance()->Get(path.GetPath());
596735df 189 SetOwner(kFALSE);
51f6d619 190 if( !entry ) AliFatal( "Couldn't load trigger description data from CDB!" );
191
192 fConfiguration = (AliTriggerConfiguration *)entry->GetObject();
193 if(fConfiguration)
194 return kTRUE;
195 else {
196 AliError( "No valid configuration is found in the CDB ! Disabling the trigger simulation !" );
197 return kFALSE;
198 }
a5a091ce 199 }
a5a091ce 200}
201
202//_____________________________________________________________________________
203TString AliCentralTrigger::GetDetectors()
204{
51f6d619 205 // return TString with the detectors (modules) to be used for triggering
a5a091ce 206
207 TString result;
208
51f6d619 209 if (fConfiguration)
210 result = fConfiguration->GetTriggeringModules();
a5a091ce 211
212 return result;
213}
214
215//_____________________________________________________________________________
8480396b 216Bool_t AliCentralTrigger::RunTrigger( AliRunLoader* runLoader, const char *detectors )
a5a091ce 217{
218 // run the trigger
bacbe0fd 219
51f6d619 220 if( !fConfiguration ) {
221 AliError( "No trigger configuration loaded, skipping trigger" );
bacbe0fd 222 return kFALSE;
223 }
224
225 TTree *tree = runLoader->TreeCT();
226 if( !tree ) {
51f6d619 227 AliError( "No folder with trigger loaded, skipping trigger" );
a5a091ce 228 return kFALSE;
229 }
230
231 TStopwatch stopwatch;
232 stopwatch.Start();
bacbe0fd 233
01eed601 234 AliInfo( Form(" Triggering Detectors: %s \n", GetDetectors().Data() ) );
235 AliInfo( Form(" Detectors with digits: %s \n", detectors ) );
236
a5a091ce 237 // Process each event
238 for( Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++ ) {
bacbe0fd 239 runLoader->GetEvent( iEvent );
a5a091ce 240 // Get detectors involve
241 TString detStr = GetDetectors();
8480396b 242 TString detWithDigits = detectors;
a5a091ce 243 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
244 // Reset Mask
245 fClassMask = 0;
305085a6 246 fClassMaskNext50 = 0;
895affe8 247 fClusterMask = 0;
248 // Reset configuration object (inputs and classes)
249 fConfiguration->Reset();
bacbe0fd 250 TObjArray trgdetArray; // use as garbage collector
a5a091ce 251 for( Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++ ) {
252 AliModule* det = (AliModule*) detArray->At( iDet );
253 if( !det || !det->IsActive() ) continue;
8480396b 254 if( IsSelected(det->GetName(), detStr) &&
255 IsSelected(det->GetName(), detWithDigits) ) {
bacbe0fd 256
01eed601 257 AliDebug(1,Form("Triggering from digits for %s", det->GetName() ) );
a5a091ce 258 AliTriggerDetector* trgdet = det->CreateTriggerDetector();
a4d25339 259 trgdet->AssignInputs(fConfiguration->GetInputs());
a5a091ce 260 TStopwatch stopwatchDet;
261 stopwatchDet.Start();
262 trgdet->Trigger();
01eed601 263 AliDebug(1, Form("Execution time for %s: R:%.2fs C:%.2fs",
a5a091ce 264 det->GetName(), stopwatchDet.RealTime(), stopwatchDet.CpuTime() ) );
bacbe0fd 265
bacbe0fd 266 trgdetArray.AddLast( trgdet );
267
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 );
272 if( loader ) {
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() ) );
277 }
278 }
279 dataLoader->Cd();
280 if( gFile && !gFile->IsWritable() ) {
281 gFile->ReOpen( "UPDATE" );
282 dataLoader->Cd();
283 }
284 trgdet->Write( "Trigger", TObject::kOverwrite );
285 dataLoader->CloseFile();
286 }
287 else AliWarning( Form( "Not loader found for %s", det->GetName() ) );
a5a091ce 288 }
289 }
290
291 // Check trigger conditions and create the trigger class mask
51f6d619 292 TriggerClasses();
d3f1f232 293 // Calculate trigger Input pattern
294 TriggerInputs();
bacbe0fd 295
a5a091ce 296 // Clear trigger detectors
297 trgdetArray.SetOwner();
298 trgdetArray.Delete();
299
300 if( (detStr.CompareTo( "ALL" ) != 0) && !detStr.IsNull() ) {
301 AliError( Form("the following detectors were not found: %s",
302 detStr.Data()));
e229562b 303 //JF return kFALSE;
a5a091ce 304 }
305
bacbe0fd 306 // Save trigger mask
307 tree->Fill();
305085a6 308 AliDebug(1, Form("Event:%d Class Mask:0x%llX 0x%llX", iEvent,fClassMask,fClassMaskNext50 ) );
a5a091ce 309 } // end event loop
a5a091ce 310
bacbe0fd 311 Reset();
312// cout << endl << " Print " << endl;
313// Print();
314
315 // Write
316 runLoader->WriteTrigger( "OVERWRITE" );
a5a091ce 317
bacbe0fd 318 return kTRUE;
319}
d3f1f232 320//----------------------------------------------------------------------------
321void AliCentralTrigger::TriggerInputs()
322{
323 // Find which inputs are in configuration
324 // and calculate input pattern
325 fL0TriggerInputs=0;
326 fL1TriggerInputs=0;
327 fL2TriggerInputs=0;
328 if(fConfiguration){
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());
338 else{
339 AliError(Form("Unknown input level:%c:",level));
340 }
341 }
342 }
343 }
344}
a5a091ce 345//_____________________________________________________________________________
305085a6 346ULong64_t AliCentralTrigger::TriggerClasses()
a5a091ce 347{
51f6d619 348 // Check trigger conditions and create the trigger class
349 // and trigger cluster masks
305085a6 350 // Updated to 100 classes
351 // It seems nobody uses return value
7e88424f 352 fClassMask = 0;
305085a6 353 fClassMaskNext50 = 0;
7e88424f 354 fClusterMask = 0;
51f6d619 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();
305085a6 362 fClassMaskNext50 |= trclass->GetValueNext50();
7e88424f 363 if (trclass->GetStatus()) {
364 AliTriggerCluster *trclust = trclass->GetCluster();
365 fClusterMask |= AliDAQ::DetectorPattern(trclust->GetDetectorsInCluster());
366 }
51f6d619 367 }
368 }
305085a6 369 return 0;
a5a091ce 370}
371//_____________________________________________________________________________
51f6d619 372TObjArray* AliCentralTrigger::GetFiredClasses() const
a5a091ce 373{
374 // return only the true conditions
375
376 TObjArray* result = new TObjArray();
377
51f6d619 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 );
384 }
a5a091ce 385 }
386
387 return result;
388}
389
a5a091ce 390//_____________________________________________________________________________
391void AliCentralTrigger::Print( const Option_t* ) const
392{
393 // Print
394 cout << "Central Trigger: " << endl;
305085a6 395 cout << " Trigger Class Mask: 1..50 0x" << hex << fClassMask << " 51-100 0x"<< fClassMaskNext50 << dec << endl;
51f6d619 396 if (fConfiguration) fConfiguration->Print();
a5a091ce 397 cout << endl;
398}
399
400
401//////////////////////////////////////////////////////////////////////////////
402// Helper method
403
404//_____________________________________________________________________________
405Bool_t AliCentralTrigger::IsSelected( TString detName, TString& detectors ) const
406{
407 // check whether detName is contained in detectors
408 // if yes, it is removed from detectors
409
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 ") ) {
415 detectors = "ALL";
416 return kTRUE;
417 }
418
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, "" );
426 result = kTRUE;
427 }
428
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 );
433
434 return result;
435}
67327b72 436
437//_____________________________________________________________________________
7e88424f 438Bool_t AliCentralTrigger::CheckTriggeredDetectors() const
67327b72 439{
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
7e88424f 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
447 // is loaded.
67327b72 448
449 if (!fConfiguration) {
7e88424f 450 AliError("The trigger confiration has not yet been loaded! Cross-check is not possible!");
451 return kFALSE;
67327b72 452 }
7e88424f 453 else {
67327b72 454
7e88424f 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());
67327b72 465 }
305085a6 466 if (trclass->GetMaskNext50() & fClassMaskNext50) { // class was fired
467 AliTriggerCluster *trclust = trclass->GetCluster();
468 clusterMask |= AliDAQ::DetectorPattern(trclust->GetDetectorsInCluster());
469 }
67327b72 470 }
7e88424f 471 // Compare the stored cluster mask with the one
472 // that we get from trigger classes
7e88424f 473 if (clusterMask != fClusterMask) {
454d6738 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));
477 return kTRUE;
478 }
479 else {
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));
482 return kFALSE;
483 }
7e88424f 484 }
67327b72 485 }
486
7e88424f 487 return kTRUE;
67327b72 488}