]> git.uio.no Git - u/mrichter/AliRoot.git/blame - STEER/AliCentralTrigger.cxx
Importing the code for relative ITS-TPC alignment (Mikolaj)
[u/mrichter/AliRoot.git] / 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"
45#include "AliModule.h"
46
47#include "AliTriggerInput.h"
48#include "AliTriggerDetector.h"
51f6d619 49#include "AliTriggerConfiguration.h"
50#include "AliTriggerClass.h"
51#include "AliTriggerCluster.h"
a5a091ce 52#include "AliCentralTrigger.h"
bacbe0fd 53#include "AliDetectorEventHeader.h"
54#include "AliHeader.h"
55
51f6d619 56#include "AliCDBManager.h"
57#include "AliCDBPath.h"
58#include "AliCDBEntry.h"
a5a091ce 59
60ClassImp( AliCentralTrigger )
61
62//_____________________________________________________________________________
63AliCentralTrigger::AliCentralTrigger() :
64 TObject(),
75e3794b 65 fClassMask(0),
51f6d619 66 fClusterMask(0),
67 fConfiguration(NULL)
a5a091ce 68{
69 // Default constructor
596735df 70 SetOwner();
a5a091ce 71}
72
73//_____________________________________________________________________________
51f6d619 74AliCentralTrigger::AliCentralTrigger( TString & config ) :
a5a091ce 75 TObject(),
75e3794b 76 fClassMask(0),
51f6d619 77 fClusterMask(0),
78 fConfiguration(NULL)
a5a091ce 79{
80 // Default constructor
51f6d619 81 LoadConfiguration( config );
bacbe0fd 82}
83
a5a091ce 84//_____________________________________________________________________________
85AliCentralTrigger::~AliCentralTrigger()
86{
51f6d619 87 // Destructor
88 DeleteConfiguration();
bacbe0fd 89}
90
91//_____________________________________________________________________________
51f6d619 92void AliCentralTrigger::DeleteConfiguration()
bacbe0fd 93{
51f6d619 94 // Delete the active configuration
95 fClassMask = 0;
96 fClusterMask = 0;
596735df 97 if (fConfiguration) {
98 if (IsOwner()) delete fConfiguration;
99 fConfiguration = 0x0;
100 }
a5a091ce 101}
102
bacbe0fd 103//_____________________________________________________________________________
104void AliCentralTrigger::Reset()
105{
51f6d619 106 // Reset Class Mask and classes
bacbe0fd 107 fClassMask = 0;
51f6d619 108 fClusterMask = 0;
109
110 if (fConfiguration) {
111 const TObjArray& classesArray = fConfiguration->GetClasses();
112 Int_t nclasses = classesArray.GetEntriesFast();
113 for( Int_t j=0; j<nclasses; j++ ) {
114 AliTriggerClass* trclass = (AliTriggerClass*)classesArray.At( j );
115 trclass->Reset();
116 }
bacbe0fd 117 }
118}
119
120//_____________________________________________________________________________
121void AliCentralTrigger::MakeBranch( TString name, TTree * tree )
122{
123 // Make a branch to store only trigger class mask event by event
124
125 if( tree ) {
126 AliDebug( 1, "Got Tree from folder." );
127 TBranch* branch = tree->GetBranch( name );
128 if( branch == 0x0 ) {
129 AliDebug( 1, "Creating new branch" );
130 branch = tree->Branch( name, &(this->fClassMask), "fClassMask/l" );
131 branch->SetAutoDelete( kFALSE );
132 }
133 else {
134 AliDebug( 1, "Got Branch from Tree" );
135 branch->SetAddress( &(this->fClassMask) );
136 }
137 }
138}
139
a5a091ce 140//_____________________________________________________________________________
51f6d619 141Bool_t AliCentralTrigger::LoadConfiguration( TString & config )
a5a091ce 142{
51f6d619 143 // Load one and only one pre-created COnfiguration from database/file that match
144 // with the input string 'config'
145 // Ej: "p-p", "Pb-Pb" or "p-p-DIMUON CALIBRATION-CENTRAL-BARREL"
146
147 // Delete the active configuration, if any
148 DeleteConfiguration();
149
150 // Load the selected configuration
151 if (!config.IsNull()) {
152 fConfiguration = AliTriggerConfiguration::LoadConfiguration( config );
596735df 153 SetOwner();
51f6d619 154 if(fConfiguration)
155 return kTRUE;
156 else {
157 AliError( Form( "Valid TriggerConfiguration (%s) is not found ! Disabling the trigger simulation !", config.Data() ) );
158 return kFALSE;
159 }
160 }
161 else {
162 // Load one and only one trigger descriptor from CDB
163 AliInfo( "GETTING TRIGGER DESCRIPTORS FROM CDB!!!" );
164
165 AliCDBPath path( "GRP", "CTP", "Config" );
166
167 AliCDBEntry *entry=AliCDBManager::Instance()->Get(path.GetPath());
596735df 168 SetOwner(kFALSE);
51f6d619 169 if( !entry ) AliFatal( "Couldn't load trigger description data from CDB!" );
170
171 fConfiguration = (AliTriggerConfiguration *)entry->GetObject();
172 if(fConfiguration)
173 return kTRUE;
174 else {
175 AliError( "No valid configuration is found in the CDB ! Disabling the trigger simulation !" );
176 return kFALSE;
177 }
a5a091ce 178 }
a5a091ce 179}
180
181//_____________________________________________________________________________
182TString AliCentralTrigger::GetDetectors()
183{
51f6d619 184 // return TString with the detectors (modules) to be used for triggering
a5a091ce 185
186 TString result;
187
51f6d619 188 if (fConfiguration)
189 result = fConfiguration->GetTriggeringModules();
a5a091ce 190
191 return result;
192}
193
194//_____________________________________________________________________________
8480396b 195Bool_t AliCentralTrigger::RunTrigger( AliRunLoader* runLoader, const char *detectors )
a5a091ce 196{
197 // run the trigger
bacbe0fd 198
51f6d619 199 if( !fConfiguration ) {
200 AliError( "No trigger configuration loaded, skipping trigger" );
bacbe0fd 201 return kFALSE;
202 }
203
204 TTree *tree = runLoader->TreeCT();
205 if( !tree ) {
51f6d619 206 AliError( "No folder with trigger loaded, skipping trigger" );
a5a091ce 207 return kFALSE;
208 }
209
210 TStopwatch stopwatch;
211 stopwatch.Start();
bacbe0fd 212
a5a091ce 213 // Process each event
214 for( Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++ ) {
bacbe0fd 215 AliInfo( Form(" ***** Processing event %d *****\n", iEvent) );
216 runLoader->GetEvent( iEvent );
a5a091ce 217 // Get detectors involve
218 TString detStr = GetDetectors();
8480396b 219 AliInfo( Form(" Triggering Detectors: %s \n", detStr.Data() ) );
220 TString detWithDigits = detectors;
221 AliInfo( Form(" Detectors with digits: %s \n", detWithDigits.Data() ) );
a5a091ce 222 TObjArray* detArray = runLoader->GetAliRun()->Detectors();
223 // Reset Mask
224 fClassMask = 0;
895affe8 225 fClusterMask = 0;
226 // Reset configuration object (inputs and classes)
227 fConfiguration->Reset();
bacbe0fd 228 TObjArray trgdetArray; // use as garbage collector
a5a091ce 229 for( Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++ ) {
230 AliModule* det = (AliModule*) detArray->At( iDet );
231 if( !det || !det->IsActive() ) continue;
8480396b 232 if( IsSelected(det->GetName(), detStr) &&
233 IsSelected(det->GetName(), detWithDigits) ) {
bacbe0fd 234
235 AliInfo( Form("Triggering from digits for %s", det->GetName() ) );
a5a091ce 236 AliTriggerDetector* trgdet = det->CreateTriggerDetector();
51f6d619 237 trgdet->CreateInputs(fConfiguration->GetInputs());
a5a091ce 238 TStopwatch stopwatchDet;
239 stopwatchDet.Start();
240 trgdet->Trigger();
241 AliInfo( Form("Execution time for %s: R:%.2fs C:%.2fs",
242 det->GetName(), stopwatchDet.RealTime(), stopwatchDet.CpuTime() ) );
bacbe0fd 243
bacbe0fd 244 trgdetArray.AddLast( trgdet );
245
246 // Write trigger detector in Event folder in Digits file
247 TString loadername = det->GetName();
248 loadername.Append( "Loader" );
249 AliLoader * loader = runLoader->GetLoader( loadername );
250 if( loader ) {
251 AliDataLoader * dataLoader = loader->GetDigitsDataLoader();
252 if( !dataLoader->IsFileOpen() ) {
253 if( dataLoader->OpenFile( "UPDATE" ) ) {
254 AliWarning( Form( "\n\nCan't write trigger for %s\n", det->GetName() ) );
255 }
256 }
257 dataLoader->Cd();
258 if( gFile && !gFile->IsWritable() ) {
259 gFile->ReOpen( "UPDATE" );
260 dataLoader->Cd();
261 }
262 trgdet->Write( "Trigger", TObject::kOverwrite );
263 dataLoader->CloseFile();
264 }
265 else AliWarning( Form( "Not loader found for %s", det->GetName() ) );
a5a091ce 266 }
267 }
268
269 // Check trigger conditions and create the trigger class mask
51f6d619 270 TriggerClasses();
bacbe0fd 271
a5a091ce 272 // Clear trigger detectors
273 trgdetArray.SetOwner();
274 trgdetArray.Delete();
275
276 if( (detStr.CompareTo( "ALL" ) != 0) && !detStr.IsNull() ) {
277 AliError( Form("the following detectors were not found: %s",
278 detStr.Data()));
e229562b 279 //JF return kFALSE;
a5a091ce 280 }
281
bacbe0fd 282 // Save trigger mask
283 tree->Fill();
284 AliInfo( Form("**************** Central Trigger Class Mask:0x%X", fClassMask ) );
a5a091ce 285 } // end event loop
a5a091ce 286
bacbe0fd 287 Reset();
288// cout << endl << " Print " << endl;
289// Print();
290
291 // Write
292 runLoader->WriteTrigger( "OVERWRITE" );
a5a091ce 293
bacbe0fd 294 return kTRUE;
295}
a5a091ce 296
297//_____________________________________________________________________________
51f6d619 298ULong64_t AliCentralTrigger::TriggerClasses()
a5a091ce 299{
51f6d619 300 // Check trigger conditions and create the trigger class
301 // and trigger cluster masks
302 if (fConfiguration) {
303 const TObjArray& classesArray = fConfiguration->GetClasses();
304 Int_t nclasses = classesArray.GetEntriesFast();
305 for( Int_t j=0; j<nclasses; j++ ) {
306 AliTriggerClass* trclass = (AliTriggerClass*)classesArray.At( j );
307 trclass->Trigger( fConfiguration->GetInputs(), fConfiguration->GetFunctions() );
308 fClassMask |= trclass->GetValue();
309 if (trclass->GetStatus())
310 fClusterMask |= (trclass->GetCluster())->GetClusterMask();
311 }
312 }
313 return fClassMask;
a5a091ce 314}
315//_____________________________________________________________________________
51f6d619 316TObjArray* AliCentralTrigger::GetFiredClasses() const
a5a091ce 317{
318 // return only the true conditions
319
320 TObjArray* result = new TObjArray();
321
51f6d619 322 if (fConfiguration) {
323 const TObjArray& classesArray = fConfiguration->GetClasses();
324 Int_t nclasses = classesArray.GetEntriesFast();
325 for( Int_t j=0; j<nclasses; j++ ) {
326 AliTriggerClass* trclass = (AliTriggerClass*)classesArray.At( j );
327 if( trclass->GetStatus() ) result->AddLast( trclass );
328 }
a5a091ce 329 }
330
331 return result;
332}
333
a5a091ce 334//_____________________________________________________________________________
335void AliCentralTrigger::Print( const Option_t* ) const
336{
337 // Print
338 cout << "Central Trigger: " << endl;
339 cout << " Trigger Class Mask: 0x" << hex << fClassMask << dec << endl;
51f6d619 340 if (fConfiguration) fConfiguration->Print();
a5a091ce 341 cout << endl;
342}
343
344
345//////////////////////////////////////////////////////////////////////////////
346// Helper method
347
348//_____________________________________________________________________________
349Bool_t AliCentralTrigger::IsSelected( TString detName, TString& detectors ) const
350{
351 // check whether detName is contained in detectors
352 // if yes, it is removed from detectors
353
354 // check if all detectors are selected
355 if( (detectors.CompareTo("ALL") == 0 ) ||
356 detectors.BeginsWith("ALL ") ||
357 detectors.EndsWith(" ALL") ||
358 detectors.Contains(" ALL ") ) {
359 detectors = "ALL";
360 return kTRUE;
361 }
362
363 // search for the given detector
364 Bool_t result = kFALSE;
365 if( (detectors.CompareTo( detName ) == 0) ||
366 detectors.BeginsWith( detName+" " ) ||
367 detectors.EndsWith( " "+detName ) ||
368 detectors.Contains( " "+detName+" " ) ) {
369 detectors.ReplaceAll( detName, "" );
370 result = kTRUE;
371 }
372
373 // clean up the detectors string
374 while( detectors.Contains(" ") ) detectors.ReplaceAll( " ", " " );
375 while( detectors.BeginsWith(" ") ) detectors.Remove( 0, 1 );
376 while( detectors.EndsWith(" ") ) detectors.Remove( detectors.Length()-1, 1 );
377
378 return result;
379}
67327b72 380
381//_____________________________________________________________________________
382TString AliCentralTrigger::GetTriggeredDetectors() const
383{
384 // Check the trigger mask, finds which trigger classes
385 // have been fired, load the corresponding trigger clusters and
386 // finally makes a list of the detectors that have been readout
387 // for each particular event
388
389 if (!fConfiguration) {
390 AliError("The trigger confiration has not yet been loaded!");
391 return "";
392 }
393
394 // Now loop over the trigger classes
395 const TObjArray& classesArray = fConfiguration->GetClasses();
396 Int_t nclasses = classesArray.GetEntriesFast();
397 UChar_t clustMask = 0;
398 for( Int_t j=0; j<nclasses; j++ ) {
399 AliTriggerClass* trclass = (AliTriggerClass*)classesArray.At( j );
400 if (trclass->GetMask() & fClassMask) { // class was fired
401 AliTriggerCluster *clust = trclass->GetCluster();
402 clustMask |= clust->GetClusterMask();
403 }
404 }
405
406 // Compare the stored cluster mask with the one
407 // that we get from trigger classes
408 // To be enables after we store the cluster mask in the trigger tree
409 // if (clustMask != fClusterMask)
410 // AliError(Form("Wrong cluster mask from trigger classes (%x), expecting (%x)!",(UInt_t)clustMask,(UInt_t)fClusterMask));
411
412 // Now loop over clusters and produce the string
413 // with the triggered detectors
414 TString trigDets;
415 const TObjArray& clustArray = fConfiguration->GetClusters();
416 Int_t nclust = clustArray.GetEntriesFast();
417 for( Int_t i=0; i<nclust; i++ ) {
418 AliTriggerCluster* clust = (AliTriggerCluster*)clustArray.At( i );
419 if (clustMask & clust->GetClusterMask()) { // the cluster was fired
420 TString detStr = clust->GetDetectorsInCluster();
421 TObjArray* det = detStr.Tokenize(" ");
422 Int_t ndet = det->GetEntriesFast();
423 for( Int_t j=0; j<ndet; j++ ) {
424 TString &detj = ((TObjString*)det->At(j))->String();
425 if((trigDets.CompareTo(detj) == 0) ||
426 trigDets.BeginsWith(detj) ||
427 trigDets.EndsWith(detj) ||
428 trigDets.Contains( " "+detj+" " )) continue;
429 trigDets.Append( " " );
430 trigDets.Append( detj );
431 }
432 }
433 }
434
435 return trigDets;
436}