]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - STEER/AliCentralTrigger.cxx
Fix for copy/paste error
[u/mrichter/AliRoot.git] / STEER / AliCentralTrigger.cxx
index 5ad12eb63550821b0fac576a5f532a3c2640eebc..8aa55035bffa253ffd5a108e12a8d1c12083f045 100644 (file)
 // This class for running the Central Trigger Processor                      //
 //                                                                           //
 //                                                                           //
-//    Load Descriptors                                                       //
-//    Make a list the trigger detectors involve from the descriptors         //
+//    Load Configuration                                                     //
+//    Make a list the trigger detectors involved (from the classes)          //
 //    For the each event                                                     //
 //           Run the Trigger for the each detector                           //
 //           Get the inputs                                                  //
-//           Check the condition classes                                     //
+//           Check the trigger classes                                       //
 //           Create the class mask                                           //
 //           Save result                                                     //
 //                                                                           //
@@ -36,6 +36,8 @@
 #include <TObjString.h>
 #include <TObjArray.h>
 #include <TStopwatch.h>
+#include <TFile.h>
+#include <TTree.h>
 
 #include "AliLog.h"
 #include "AliRun.h"
 
 #include "AliTriggerInput.h"
 #include "AliTriggerDetector.h"
-#include "AliTriggerCondition.h"
-#include "AliTriggerDescriptor.h"
+#include "AliTriggerConfiguration.h"
+#include "AliTriggerClass.h"
+#include "AliTriggerCluster.h"
 #include "AliCentralTrigger.h"
+#include "AliDetectorEventHeader.h"
+#include "AliHeader.h"
+
+#include "AliCDBManager.h"
+#include "AliCDBPath.h"
+#include "AliCDBEntry.h"
 
 ClassImp( AliCentralTrigger )
 
 //_____________________________________________________________________________
 AliCentralTrigger::AliCentralTrigger() :
    TObject(),
-   fClassMask(0)
+   fClassMask(0),
+   fClusterMask(0),
+   fConfiguration(NULL)
 {
    // Default constructor
-//   LoadDescriptor("Pb-Pb");
 }
 
 //_____________________________________________________________________________
-AliCentralTrigger::AliCentralTrigger( TString & descriptor ) :
+AliCentralTrigger::AliCentralTrigger( TString & config ) :
    TObject(),
-   fClassMask(0)
+   fClassMask(0),
+   fClusterMask(0),
+   fConfiguration(NULL)
 {
    // Default constructor
-   LoadDescriptor( descriptor );
+   LoadConfiguration( config );
 }
 
 //_____________________________________________________________________________
 AliCentralTrigger::~AliCentralTrigger()
 {
-   // Destructor
-   fDescriptors.SetOwner();
-   fDescriptors.Delete();
+  // Destructor
+  DeleteConfiguration();
 }
 
 //_____________________________________________________________________________
-Bool_t AliCentralTrigger::LoadDescriptor( TString & descriptor )
+void AliCentralTrigger::DeleteConfiguration()
 {
-   // Load one or more pre-created Descriptors from database/file that match
-   // with the input string 'descriptor'
-   // Ej: "Pb-Pb" or "p-p-DIMUON CALIBRATION-CENTRAL-BARREL"
-
-   // Delete any descriptor
-   fDescriptors.Delete();
-
-   // Load the selected descriptors
-   TObjArray* desArray = descriptor.Tokenize( " " );
-   Int_t ndes = desArray->GetEntriesFast();
-   for( Int_t i=0; i<ndes; i++ ) {
-      TObjString* val = (TObjString*)desArray->At( i );
-      AliTriggerDescriptor* des = AliTriggerDescriptor::LoadDescriptor( val->String() );
-      if( des ) 
-         fDescriptors.AddLast( des );
-      else
-         AliWarning( Form( "Descriptor (%s) not found", val->String().Data() ) );
+  // Delete the active configuration
+  fClassMask = 0;
+  fClusterMask = 0;
+  if (fConfiguration) delete fConfiguration;
+}
+
+//_____________________________________________________________________________
+void AliCentralTrigger::Reset()
+{
+   // Reset Class Mask and classes
+   fClassMask = 0;
+   fClusterMask = 0;
+
+   if (fConfiguration) {
+     const TObjArray& classesArray = fConfiguration->GetClasses();
+     Int_t nclasses = classesArray.GetEntriesFast();
+     for( Int_t j=0; j<nclasses; j++ ) {
+       AliTriggerClass* trclass = (AliTriggerClass*)classesArray.At( j );
+       trclass->Reset();
+     }
    }
-   Bool_t desfound = kTRUE;
-   if( fDescriptors.GetEntriesFast() == 0 ) desfound = kFALSE;
+}
 
-   delete desArray;
+//_____________________________________________________________________________
+void AliCentralTrigger::MakeBranch( TString name, TTree * tree )
+{
+   // Make a branch to store only trigger class mask event by event
+
+   if( tree )  {
+      AliDebug( 1, "Got Tree from folder." );
+      TBranch* branch = tree->GetBranch( name );
+      if( branch == 0x0 ) {
+         AliDebug( 1, "Creating new branch" );
+         branch = tree->Branch( name, &(this->fClassMask), "fClassMask/l" );
+         branch->SetAutoDelete( kFALSE );
+      }
+      else {
+         AliDebug( 1, "Got Branch from Tree" );
+         branch->SetAddress( &(this->fClassMask) );
+      }
+   }
+}
 
-   return desfound;
+//_____________________________________________________________________________
+Bool_t AliCentralTrigger::LoadConfiguration( TString & config )
+{
+   // Load one and only one pre-created COnfiguration from database/file that match
+   // with the input string 'config'
+   // Ej: "p-p", "Pb-Pb" or "p-p-DIMUON CALIBRATION-CENTRAL-BARREL"
+
+   // Delete the active configuration, if any
+  DeleteConfiguration();
+
+   // Load the selected configuration
+   if (!config.IsNull()) {
+     fConfiguration = AliTriggerConfiguration::LoadConfiguration( config );
+     if(fConfiguration)
+       return kTRUE;
+     else {
+       AliError( Form( "Valid TriggerConfiguration (%s) is not found ! Disabling the trigger simulation !", config.Data() ) );
+       return kFALSE;
+     }
+   }
+   else {
+     // Load one and only one trigger descriptor from CDB
+     AliInfo( "GETTING TRIGGER DESCRIPTORS FROM CDB!!!" );
+     AliCDBPath path( "GRP", "CTP", "Config" );
+       
+     AliCDBEntry *entry=AliCDBManager::Instance()->Get(path.GetPath());
+     if( !entry ) AliFatal( "Couldn't load trigger description data from CDB!" );
+
+     fConfiguration = (AliTriggerConfiguration *)entry->GetObject();
+     if(fConfiguration)
+       return kTRUE;
+     else {
+       AliError( "No valid configuration is found in the CDB ! Disabling the trigger simulation !" );
+       return kFALSE;
+     }
+   }
 }
 
 //_____________________________________________________________________________
 TString AliCentralTrigger::GetDetectors()
 {
-   // return TString with the detectors to be trigger
-   // merging detectors from all descriptors
+   // return TString with the detectors (modules) to be used for triggering
 
    TString result;
 
-   Int_t ndes = fDescriptors.GetEntriesFast();
-   for( Int_t i=0; i<ndes; i++ ) {
-      TString detStr = ((AliTriggerDescriptor*)fDescriptors.At( i ))->GetDetectorCluster();
-      TObjArray* det = detStr.Tokenize(" ");
-      Int_t ndet = det->GetEntriesFast();
-      for( Int_t j=0; j<ndet; j++ ) {
-         if( result.Contains( ((TObjString*)det->At(j))->String() ) )continue;
-         result.Append( " " );
-         result.Append( ((TObjString*)det->At(j))->String() );
-      }
-   }
+   if (fConfiguration)
+     result = fConfiguration->GetTriggeringModules();
 
    return result;
 }
 
 //_____________________________________________________________________________
-Bool_t AliCentralTrigger::RunTrigger( AliRunLoader* runLoader )
+Bool_t AliCentralTrigger::RunTrigger( AliRunLoader* runLoader, const char *detectors )
 {
    // run the trigger
-   
-   if( fDescriptors.GetEntriesFast() == 0 ) {
-      AliError("not trigger descriptor loaded, skipping trigger");
+
+   if( !fConfiguration ) {
+      AliError( "No trigger configuration loaded, skipping trigger" );
+      return kFALSE;
+   }
+
+   TTree *tree = runLoader->TreeCT();
+   if( !tree ) {
+      AliError( "No folder with trigger loaded, skipping trigger" );
       return kFALSE;
    }
 
    TStopwatch stopwatch;
    stopwatch.Start();
-   
+
    // Process each event
    for( Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++ ) {
-      AliInfo( Form("\n ***** Processing event %d *****\n", iEvent) );
-      runLoader->GetEvent( iEvent );       
+      AliInfo( Form("  ***** Processing event %d *****\n", iEvent) );
+      runLoader->GetEvent( iEvent );
       // Get detectors involve
       TString detStr = GetDetectors();
+      AliInfo( Form(" Triggering Detectors: %s \n", detStr.Data() ) );
+      TString detWithDigits = detectors;
+      AliInfo( Form(" Detectors with digits: %s \n", detWithDigits.Data() ) );
       TObjArray* detArray = runLoader->GetAliRun()->Detectors();
       // Reset Mask
       fClassMask = 0;
-      TObjArray trgdetArray;
+      fClusterMask = 0;
+      // Reset configuration object (inputs and classes)
+      fConfiguration->Reset();
+      TObjArray trgdetArray; // use as garbage collector
       for( Int_t iDet = 0; iDet < detArray->GetEntriesFast(); iDet++ ) {
          AliModule* det = (AliModule*) detArray->At( iDet );
          if( !det || !det->IsActive() ) continue;
-         if( IsSelected(det->GetName(), detStr) ) {
-            AliInfo( Form("triggering from digits for %s", det->GetName() ) );
+         if( IsSelected(det->GetName(), detStr) &&
+            IsSelected(det->GetName(), detWithDigits) ) {
+
+            AliInfo( Form("Triggering from digits for %s", det->GetName() ) );
             AliTriggerDetector* trgdet = det->CreateTriggerDetector();
-            trgdet->CreateInputs();
-            trgdetArray.AddLast( trgdet );
+            trgdet->CreateInputs(fConfiguration->GetInputs());
             TStopwatch stopwatchDet;
             stopwatchDet.Start();
             trgdet->Trigger();
             AliInfo( Form("Execution time for %s: R:%.2fs C:%.2fs",
                      det->GetName(), stopwatchDet.RealTime(), stopwatchDet.CpuTime() ) );
-            // Get the inputs
-            TObjArray* detInp = trgdet->GetInputs();
-            for( Int_t i=0; i<detInp->GetEntriesFast(); i++ ) {
-               fInputs.AddLast( detInp->At(i) );
+
+            trgdetArray.AddLast( trgdet );
+
+            // Write trigger detector in Event folder in Digits file
+            TString loadername = det->GetName();
+            loadername.Append( "Loader" );
+            AliLoader * loader = runLoader->GetLoader( loadername );
+            if( loader ) {
+               AliDataLoader * dataLoader = loader->GetDigitsDataLoader();
+               if( !dataLoader->IsFileOpen() ) {
+                  if( dataLoader->OpenFile( "UPDATE" ) ) {
+                     AliWarning( Form( "\n\nCan't write trigger for %s\n", det->GetName() ) );
+                  }
+               }
+               dataLoader->Cd();
+               if( gFile && !gFile->IsWritable() ) {
+                  gFile->ReOpen( "UPDATE" );
+                  dataLoader->Cd();
+               }
+               trgdet->Write( "Trigger", TObject::kOverwrite );
+               dataLoader->CloseFile();
             }
+            else  AliWarning( Form( "Not loader found for %s", det->GetName() ) );
          }
       }
 
       // Check trigger conditions and create the trigger class mask
-      CheckConditions();
+      TriggerClasses();
 
-      fInputs.Clear();
-      
       // Clear trigger detectors
       trgdetArray.SetOwner();
       trgdetArray.Delete();
@@ -184,71 +270,68 @@ Bool_t AliCentralTrigger::RunTrigger( AliRunLoader* runLoader )
       if( (detStr.CompareTo( "ALL" ) != 0) && !detStr.IsNull() ) {
          AliError( Form("the following detectors were not found: %s",
                    detStr.Data()));
-         return kFALSE;
+         //JF return kFALSE;
       }
 
-      // Write trigger ???? -> Event Header
-      // Write();
-      Print();
-
+      // Save trigger mask
+      tree->Fill();
+      AliInfo( Form("**************** Central Trigger Class Mask:0x%X", fClassMask ) );
    } // end event loop
-   return kTRUE;
-}
 
+   Reset();
+//   cout << endl <<  " Print " << endl;
+//   Print();
+
+   // Write
+   runLoader->WriteTrigger( "OVERWRITE" );
 
+   return kTRUE;
+}
 
 //_____________________________________________________________________________
-Long_t AliCentralTrigger::CheckConditions()
+ULong64_t AliCentralTrigger::TriggerClasses()
 {
-   // Check trigger conditions and create the trigger class mask
-
-   Int_t ndes = fDescriptors.GetEntriesFast();
-   for( Int_t i=0; i<ndes; i++ ) {
-      TObjArray* condArray = ((AliTriggerDescriptor*)fDescriptors.At( i ))->GetTriggerConditions();
-      Int_t ncond = condArray->GetEntriesFast();
-      for( Int_t j=0; j<ncond; j++ ) {
-         AliTriggerCondition* cond = (AliTriggerCondition*)condArray->At( j );
-         if( !cond->CheckInputs( fInputs ) ) continue;
-         cond->Trigger( fInputs );
-    //     cond->Print();
-         fClassMask |= cond->GetValue();
-      }
-   }
-   return fClassMask;
+  // Check trigger conditions and create the trigger class
+  // and trigger cluster masks
+  if (fConfiguration) {
+    const TObjArray& classesArray = fConfiguration->GetClasses();
+    Int_t nclasses = classesArray.GetEntriesFast();
+    for( Int_t j=0; j<nclasses; j++ ) {
+      AliTriggerClass* trclass = (AliTriggerClass*)classesArray.At( j );
+      trclass->Trigger( fConfiguration->GetInputs(), fConfiguration->GetFunctions() );
+      fClassMask |= trclass->GetValue();
+      if (trclass->GetStatus())
+       fClusterMask |= (trclass->GetCluster())->GetClusterMask();
+    }
+  }
+  return fClassMask;
 }
 //_____________________________________________________________________________
-TObjArray* AliCentralTrigger::GetResultConditions()
+TObjArray* AliCentralTrigger::GetFiredClasses() const
 {
    // return only the true conditions
 
    TObjArray* result = new TObjArray();
 
-   Int_t ndes = fDescriptors.GetEntriesFast();
-   for( Int_t i=0; i<ndes; i++ ) {
-      TObjArray* condArray = ((AliTriggerDescriptor*)fDescriptors.At( i ))->GetTriggerConditions();
-      Int_t ncond = condArray->GetEntriesFast();
-      for( Int_t j=0; j<ncond; j++ ) {
-         AliTriggerCondition* cond = (AliTriggerCondition*)condArray->At( j );
-         if( cond->GetStatus() ) result->AddLast( cond );
-      }
+   if (fConfiguration) {
+     const TObjArray& classesArray = fConfiguration->GetClasses();
+     Int_t nclasses = classesArray.GetEntriesFast();
+     for( Int_t j=0; j<nclasses; j++ ) {
+       AliTriggerClass* trclass = (AliTriggerClass*)classesArray.At( j );
+       if( trclass->GetStatus() ) result->AddLast( trclass );
+     }
    }
 
    return result;
 }
 
-
 //_____________________________________________________________________________
 void AliCentralTrigger::Print( const Option_t*  ) const
 {
    // Print
    cout << "Central Trigger: " << endl;
    cout << "  Trigger Class Mask: 0x" << hex << fClassMask << dec << endl;
-
-   Int_t ndes = fDescriptors.GetEntriesFast();
-   for( Int_t i=0; i<ndes; i++ ) {
-      AliTriggerDescriptor* des = (AliTriggerDescriptor*)fDescriptors.At( i );
-      if( des ) des->Print();
-   }
+   if (fConfiguration) fConfiguration->Print();
    cout << endl;
 }
 
@@ -288,3 +371,60 @@ Bool_t AliCentralTrigger::IsSelected( TString detName, TString& detectors ) cons
 
    return result;
 }
+
+//_____________________________________________________________________________
+TString AliCentralTrigger::GetTriggeredDetectors() const
+{
+  // Check the trigger mask, finds which trigger classes
+  // have been fired, load the corresponding trigger clusters and
+  // finally makes a list of the detectors that have been readout
+  // for each particular event
+
+  if (!fConfiguration) {
+    AliError("The trigger confiration has not yet been loaded!");
+    return "";
+  }
+
+  // Now loop over the trigger classes
+  const TObjArray& classesArray = fConfiguration->GetClasses();
+  Int_t nclasses = classesArray.GetEntriesFast();
+  UChar_t clustMask = 0;
+  for( Int_t j=0; j<nclasses; j++ ) {
+    AliTriggerClass* trclass = (AliTriggerClass*)classesArray.At( j );
+    if (trclass->GetMask() & fClassMask) { // class was fired
+      AliTriggerCluster *clust = trclass->GetCluster();
+      clustMask |= clust->GetClusterMask();
+    }
+  }
+
+  // Compare the stored cluster mask with the one
+  // that we get from trigger classes
+  // To be enables after we store the cluster mask in the trigger tree
+  //  if (clustMask != fClusterMask)
+  //    AliError(Form("Wrong cluster mask from trigger classes (%x), expecting (%x)!",(UInt_t)clustMask,(UInt_t)fClusterMask));
+
+  // Now loop over clusters and produce the string
+  // with the triggered detectors
+  TString trigDets;
+  const TObjArray& clustArray = fConfiguration->GetClusters();
+  Int_t nclust = clustArray.GetEntriesFast();
+  for( Int_t i=0; i<nclust; i++ ) {
+    AliTriggerCluster* clust = (AliTriggerCluster*)clustArray.At( i );
+    if (clustMask & clust->GetClusterMask()) { // the cluster was fired
+      TString detStr = clust->GetDetectorsInCluster();
+      TObjArray* det = detStr.Tokenize(" ");
+      Int_t ndet = det->GetEntriesFast();
+      for( Int_t j=0; j<ndet; j++ ) {
+       TString &detj = ((TObjString*)det->At(j))->String();
+         if((trigDets.CompareTo(detj) == 0) || 
+           trigDets.BeginsWith(detj) ||
+           trigDets.EndsWith(detj) ||
+           trigDets.Contains( " "+detj+" " )) continue;
+         trigDets.Append( " " );
+         trigDets.Append( detj );
+      }
+    }
+  }
+
+  return trigDets;
+}