<commit_before>/**************************************************************************
 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
 *                                                                        *
 * Author: The ALICE Off-line Project.                                    *
 * Contributors are mentioned in the code where appropriate.              *
 *                                                                        *
 * Permission to use, copy, modify and distribute this software and its   *
 * documentation strictly for non-commercial purposes is hereby granted   *
 * without fee, provided that the above copyright notice appears in all   *
 * copies and that both the copyright notice and this permission notice   *
 * appear in the supporting documentation. The authors make no claims     *
 * about the suitability of this software for any purpose. It is          *
 * provided "as is" without express or implied warranty.                  *
 **************************************************************************/

/* $Id$ */

//-----------------------------------------------------------------------------
/// \namespace AliMUONCDB
///
/// Helper functions to experience the OCDB
///
/// They allow to read magnetic field, mapping and recoParam from OCDB
///
/// And also to generate dummy (but complete) containers for all the
/// calibration data types we have for tracker and trigger, and to write
/// them into OCDB.
///
/// For more information, please see READMEcalib
///
/// \author <NAME>
//-----------------------------------------------------------------------------

#include "AliMUONCDB.h"

#include "AliMUON1DArray.h"
#include "AliMUON1DMap.h"
#include "AliMUON2DMap.h"
#include "AliMUON2DStoreValidator.h"
#include "AliMUONCalibParamND.h"
#include "AliMUONCalibParamNF.h"
#include "AliMUONCalibParamNI.h"
#include "AliMUONCalibrationData.h"
#include "AliMUONConstants.h"
#include "AliMUONGlobalCrateConfig.h"
#include "AliMUONLogger.h"
#include "AliMUONPadStatusMaker.h"
#include "AliMUONPadStatusMapMaker.h"
#include "AliMUONRecoParam.h"
#include "AliMUONRegionalTriggerConfig.h"
#include "AliMUONRejectList.h"
#include "AliMUONTrackerData.h"
#include "AliMUONTrackerIO.h"
#include "AliMUONTriggerEfficiencyCells.h"
#include "AliMUONTriggerLut.h"
#include "AliMUONVCalibParam.h"
#include "AliMUONVCalibParam.h"
#include "AliMUONVStore.h"

#include "AliMpCDB.h"
#include "AliMpConstants.h"
#include "AliMpDEStore.h"
#include "AliMpDDLStore.h"
#include "AliMpManuStore.h"
#include "AliMpDEManager.h"
#include "AliMpDetElement.h"
#include "AliMpFiles.h"
#include "AliMpDCSNamer.h"
#include "AliMpManuIterator.h"
#include "AliMpSegmentation.h"
#include "AliMpStationType.h"
#include "AliMpVSegmentation.h"

#include "AliCodeTimer.h"
#include "AliCDBEntry.h"
#include "AliCDBManager.h"
#include "AliGRPManager.h"
#include "AliDCSValue.h"
#include "AliLog.h"
#include "AliMpBusPatch.h"

#include <Riostream.h>
#include <TArrayI.h>
#include <TClass.h>
#include <TFile.h>
#include <TH1F.h>
#include <TList.h>
#include <TMap.h>
#include <TObjString.h>
#include <TROOT.h>
#include <TRandom.h>
#include <TStopwatch.h>
#include <TSystem.h>
#include <TMath.h>
#include <TGeoGlobalMagField.h>
#include <TClonesArray.h>
#include <sstream>
#include <set>

namespace
{
  //_____________________________________________________________________________
AliMUONVStore* Create2DMap()
{
  return new AliMUON2DMap(true);
}

  //_____________________________________________________________________________
void getBoundaries(const AliMUONVStore& store, Int_t dim,
                   Float_t* xmin, Float_t* xmax)
{
  /// Assuming the store contains AliMUONVCalibParam objects, compute the
  /// limits of the value contained in the VCalibParam, for each of its dimensions
  /// xmin and xmax must be of dimension dim
  
  for ( Int_t i = 0; i < dim; ++i ) 
  {
    xmin[i]=1E30;
    xmax[i]=-1E30;
  }
  
  TIter next(store.CreateIterator());
  AliMUONVCalibParam* value;
  
  while ( ( value = dynamic_cast<AliMUONVCalibParam*>(next() ) ) )
  {
    Int_t detElemId = value->ID0();
    Int_t manuId = value->ID1();
    
    const AliMpVSegmentation* seg = 
      AliMpSegmentation::Instance()->GetMpSegmentationByElectronics(detElemId,manuId);
        
    if (!seg) continue;
    
    for ( Int_t manuChannel = 0; manuChannel < value->Size(); ++manuChannel )
    {
      AliMpPad pad = seg->PadByLocation(manuId,manuChannel,kFALSE);
      if (!pad.IsValid()) continue;
      
      for ( Int_t i = 0; i < dim; ++i ) 
      {
        Float_t x0 = value->ValueAsFloat(manuChannel,i);
      
        xmin[i] = TMath::Min(xmin[i],x0);
        xmax[i] = TMath::Max(xmax[i],x0);
      }
    }
  }

  for ( Int_t i = 0; i < dim; ++i ) 
  {
    if ( TMath::Abs(xmin[i]-xmax[i]) < 1E-3 ) 
    {
      xmin[i] -= 1;
      xmax[i] += 1;
    }
  }
}

//_____________________________________________________________________________
Double_t GetRandom(Double_t mean, Double_t sigma, Bool_t mustBePositive)
{
  Double_t x(-1);
  if ( mustBePositive ) 
  {
    while ( x < 0 ) 
    {
      x = gRandom->Gaus(mean,sigma);
    }
  }
  else
  {
    x = gRandom->Gaus(mean,sigma);
  }
  return x;
}

}

//_____________________________________________________________________________
Bool_t AliMUONCDB::CheckOCDB(Bool_t pathOnly)
{
  /// Check that OCDB path and run number are properly set
  
  AliCDBManager* man = AliCDBManager::Instance();
  
  // first OCDB path
  if (!man->IsDefaultStorageSet()) {
    AliErrorGeneral("AliMUONCDB", "OCDB path must be properly set");
    return kFALSE;
  }
  
  // then run number if required
  if (pathOnly) return kTRUE;
  if (man->GetRun() < 0) {
    AliErrorGeneral("AliMUONCDB", "Run number must be properly set");
    return kFALSE;
  }
  
  return kTRUE;
  
}

//_____________________________________________________________________________
Bool_t AliMUONCDB::CheckMapping(Bool_t segmentationOnly)
{
  /// Check that the mapping has been loaded
  
  // first the segmentation
  if (!AliMpSegmentation::Instance(false)) {
    AliErrorGeneral("AliMUONCDB", "Mapping segmentation must be loaded first");
    return kFALSE;
  }
  
  // then the others if required
  if (segmentationOnly) return kTRUE;
  if (!AliMpDDLStore::Instance(false) || !AliMpDEStore::Instance(false) || !AliMpManuStore::Instance(false)) {
    AliErrorGeneral("AliMUONCDB", "Full mapping must be loaded first");
    return kFALSE;
  }
  
  return kTRUE;
  
}

//_____________________________________________________________________________
Bool_t AliMUONCDB::LoadField()
{
  /// Load magnetic field (existing field will be deleted).
  /// OCDB path and run number are supposed to be set.
  
  AliInfoGeneral("AliMUONCDB","Loading field map from GRP...");
  
  if (!AliMUONCDB::CheckOCDB()) return kFALSE;
  
  AliGRPManager grpMan;
  
  // make sure the old field is deleted even if it is locked
  if(TGeoGlobalMagField::Instance()->IsLocked()) delete TGeoGlobalMagField::Instance();
  
  if (!grpMan.ReadGRPEntry() || !grpMan.SetMagField()) {
    AliErrorGeneral("AliMUONCDB", "failed to load magnetic field from OCDB");
    return kFALSE;
  }
  
  return kTRUE;
  
}

//_____________________________________________________________________________
Bool_t AliMUONCDB::LoadMapping(Bool_t segmentationOnly)
{
  /// Load mapping (existing mapping will be unloaded).
  /// OCDB path and run number are supposed to be set.
  
  AliInfoGeneral("AliMUONCDB", "Loading mapping from OCDB...");
  
  if (!AliMUONCDB::CheckOCDB()) return kFALSE;
  
  // in case it has already been set
  AliMpCDB::UnloadAll();
  
  if (segmentationOnly) {
    
    if (!AliMpCDB::LoadMpSegmentation(kTRUE)){
      AliErrorGeneral("AliMUONCDB", "failed to load segmentation from OCDB");
      return kFALSE;
    }
    
  } else {
    
    if (!AliMpCDB::LoadAll(kTRUE)) {
      AliErrorGeneral("AliMUONCDB", "failed to load mapping from OCDB");
      return kFALSE;
    }
    
  }
  
  return kTRUE;
  
}

//_____________________________________________________________________________
AliMUONRecoParam* AliMUONCDB::LoadRecoParam()
{
  /// Load and return reconstruction parameters.
  /// OCDB path is supposed to be set.
  
  AliInfoGeneral("AliMUONCDB", "Loading RecoParam from OCDB...");
  
  if (!AliMUONCDB::CheckOCDB()) return kFALSE;
  
  AliMUONRecoParam* recoParam = 0x0;
  AliCDBEntry* entry = AliCDBManager::Instance()->Get("MUON/Calib/RecoParam");
  
  if(entry) {
    
    // load recoParam according OCDB content (single or array)
    if (!(recoParam = dynamic_cast<AliMUONRecoParam*>(entry->GetObject()))) {
      
      TObjArray* recoParamArray = static_cast<TObjArray*>(entry->GetObject());
//      recoParamArray->SetOwner(kTRUE); // FIXME: this should be done, but is causing a problem at the end of the reco... investigate why...
      
      for(Int_t i = 0; i < recoParamArray->GetEntriesFast(); i++) {
	recoParam = static_cast<AliMUONRecoParam*>(recoParamArray->UncheckedAt(i));
	if (recoParam->IsDefault()) break;
	recoParam = 0x0;
      }
      
    }
    
  }
  
  if (!recoParam) AliErrorGeneral("AliMUONCDB", "failed to load RecoParam from OCDB");
  
  return recoParam;
  
}

//_____________________________________________________________________________
TClonesArray* AliMUONCDB::LoadAlignmentData()
{
  /// Load and return the array of alignment objects.
  
  AliInfoGeneral("AliMUONCDB", "Loading Alignemnt from OCDB...");
  
  if (!AliMUONCDB::CheckOCDB()) return kFALSE;
  
  TClonesArray* alignmentArray = 0x0;
  AliCDBEntry* entry = AliCDBManager::Instance()->Get("MUON/Align/Data");
  
  if (entry) {
    // load alignement array
    alignmentArray = dynamic_cast<TClonesArray*>(entry->GetObject());
  }
  
  if (!alignmentArray) { 
    AliErrorGeneral("AliMUONCDB", "failed to load Alignemnt from OCDB");
  }  
  
  return alignmentArray;
}

//_____________________________________________________________________________
AliMUONVStore* 
AliMUONCDB::Diff(AliMUONVStore& store1, AliMUONVStore& store2, 
                 const char* opt)
{
  /// creates a store which contains store1-store2
  /// if opt="abs" the difference is absolute one,
  /// if opt="rel" then what is stored is (store1-store2)/store1<commit_msg>Correct some return values<commit_after>/**************************************************************************
 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
 *                                                                        *
 * Author: The ALICE Off-line Project.                                    *
 * Contributors are mentioned in the code where appropriate.              *
 *                                                                        *
 * Permission to use, copy, modify and distribute this software and its   *
 * documentation strictly for non-commercial purposes is hereby granted   *
 * without fee, provided that the above copyright notice appears in all   *
 * copies and that both the copyright notice and this permission notice   *
 * appear in the supporting documentation. The authors make no claims     *
 * about the suitability of this software for any purpose. It is          *
 * provided "as is" without express or implied warranty.                  *
 **************************************************************************/

/* $Id$ */

//-----------------------------------------------------------------------------
/// \namespace AliMUONCDB
///
/// Helper functions to experience the OCDB
///
/// They allow to read magnetic field, mapping and recoParam from OCDB
///
/// And also to generate dummy (but complete) containers for all the
/// calibration data types we have for tracker and trigger, and to write
/// them into OCDB.
///
/// For more information, please see READMEcalib
///
/// \author <NAME>
//-----------------------------------------------------------------------------

#include "AliMUONCDB.h"

#include "AliMUON1DArray.h"
#include "AliMUON1DMap.h"
#include "AliMUON2DMap.h"
#include "AliMUON2DStoreValidator.h"
#include "AliMUONCalibParamND.h"
#include "AliMUONCalibParamNF.h"
#include "AliMUONCalibParamNI.h"
#include "AliMUONCalibrationData.h"
#include "AliMUONConstants.h"
#include "AliMUONGlobalCrateConfig.h"
#include "AliMUONLogger.h"
#include "AliMUONPadStatusMaker.h"
#include "AliMUONPadStatusMapMaker.h"
#include "AliMUONRecoParam.h"
#include "AliMUONRegionalTriggerConfig.h"
#include "AliMUONRejectList.h"
#include "AliMUONTrackerData.h"
#include "AliMUONTrackerIO.h"
#include "AliMUONTriggerEfficiencyCells.h"
#include "AliMUONTriggerLut.h"
#include "AliMUONVCalibParam.h"
#include "AliMUONVCalibParam.h"
#include "AliMUONVStore.h"

#include "AliMpCDB.h"
#include "AliMpConstants.h"
#include "AliMpDEStore.h"
#include "AliMpDDLStore.h"
#include "AliMpManuStore.h"
#include "AliMpDEManager.h"
#include "AliMpDetElement.h"
#include "AliMpFiles.h"
#include "AliMpDCSNamer.h"
#include "AliMpManuIterator.h"
#include "AliMpSegmentation.h"
#include "AliMpStationType.h"
#include "AliMpVSegmentation.h"

#include "AliCodeTimer.h"
#include "AliCDBEntry.h"
#include "AliCDBManager.h"
#include "AliGRPManager.h"
#include "AliDCSValue.h"
#include "AliLog.h"
#include "AliMpBusPatch.h"

#include <Riostream.h>
#include <TArrayI.h>
#include <TClass.h>
#include <TFile.h>
#include <TH1F.h>
#include <TList.h>
#include <TMap.h>
#include <TObjString.h>
#include <TROOT.h>
#include <TRandom.h>
#include <TStopwatch.h>
#include <TSystem.h>
#include <TMath.h>
#include <TGeoGlobalMagField.h>
#include <TClonesArray.h>
#include <sstream>
#include <set>

namespace
{
  //_____________________________________________________________________________
AliMUONVStore* Create2DMap()
{
  return new AliMUON2DMap(true);
}

  //_____________________________________________________________________________
void getBoundaries(const AliMUONVStore& store, Int_t dim,
                   Float_t* xmin, Float_t* xmax)
{
  /// Assuming the store contains AliMUONVCalibParam objects, compute the
  /// limits of the value contained in the VCalibParam, for each of its dimensions
  /// xmin and xmax must be of dimension dim
  
  for ( Int_t i = 0; i < dim; ++i ) 
  {
    xmin[i]=1E30;
    xmax[i]=-1E30;
  }
  
  TIter next(store.CreateIterator());
  AliMUONVCalibParam* value;
  
  while ( ( value = dynamic_cast<AliMUONVCalibParam*>(next() ) ) )
  {
    Int_t detElemId = value->ID0();
    Int_t manuId = value->ID1();
    
    const AliMpVSegmentation* seg = 
      AliMpSegmentation::Instance()->GetMpSegmentationByElectronics(detElemId,manuId);
        
    if (!seg) continue;
    
    for ( Int_t manuChannel = 0; manuChannel < value->Size(); ++manuChannel )
    {
      AliMpPad pad = seg->PadByLocation(manuId,manuChannel,kFALSE);
      if (!pad.IsValid()) continue;
      
      for ( Int_t i = 0; i < dim; ++i ) 
      {
        Float_t x0 = value->ValueAsFloat(manuChannel,i);
      
        xmin[i] = TMath::Min(xmin[i],x0);
        xmax[i] = TMath::Max(xmax[i],x0);
      }
    }
  }

  for ( Int_t i = 0; i < dim; ++i ) 
  {
    if ( TMath::Abs(xmin[i]-xmax[i]) < 1E-3 ) 
    {
      xmin[i] -= 1;
      xmax[i] += 1;
    }
  }
}

//_____________________________________________________________________________
Double_t GetRandom(Double_t mean, Double_t sigma, Bool_t mustBePositive)
{
  Double_t x(-1);
  if ( mustBePositive ) 
  {
    while ( x < 0 ) 
    {
      x = gRandom->Gaus(mean,sigma);
    }
  }
  else
  {
    x = gRandom->Gaus(mean,sigma);
  }
  return x;
}

}

//_____________________________________________________________________________
Bool_t AliMUONCDB::CheckOCDB(Bool_t pathOnly)
{
  /// Check that OCDB path and run number are properly set
  
  AliCDBManager* man = AliCDBManager::Instance();
  
  // first OCDB path
  if (!man->IsDefaultStorageSet()) {
    AliErrorGeneral("AliMUONCDB", "OCDB path must be properly set");
    return kFALSE;
  }
  
  // then run number if required
  if (pathOnly) return kTRUE;
  if (man->GetRun() < 0) {
    AliErrorGeneral("AliMUONCDB", "Run number must be properly set");
    return kFALSE;
  }
  
  return kTRUE;
  
}

//_____________________________________________________________________________
Bool_t AliMUONCDB::CheckMapping(Bool_t segmentationOnly)
{
  /// Check that the mapping has been loaded
  
  // first the segmentation
  if (!AliMpSegmentation::Instance(false)) {
    AliErrorGeneral("AliMUONCDB", "Mapping segmentation must be loaded first");
    return kFALSE;
  }
  
  // then the others if required
  if (segmentationOnly) return kTRUE;
  if (!AliMpDDLStore::Instance(false) || !AliMpDEStore::Instance(false) || !AliMpManuStore::Instance(false)) {
    AliErrorGeneral("AliMUONCDB", "Full mapping must be loaded first");
    return kFALSE;
  }
  
  return kTRUE;
  
}

//_____________________________________________________________________________
Bool_t AliMUONCDB::LoadField()
{
  /// Load magnetic field (existing field will be deleted).
  /// OCDB path and run number are supposed to be set.
  
  AliInfoGeneral("AliMUONCDB","Loading field map from GRP...");
  
  if (!AliMUONCDB::CheckOCDB()) return kFALSE;
  
  AliGRPManager grpMan;
  
  // make sure the old field is deleted even if it is locked
  if(TGeoGlobalMagField::Instance()->IsLocked()) delete TGeoGlobalMagField::Instance();
  
  if (!grpMan.ReadGRPEntry() || !grpMan.SetMagField()) {
    AliErrorGeneral("AliMUONCDB", "failed to load magnetic field from OCDB");
    return kFALSE;
  }
  
  return kTRUE;
  
}

//_____________________________________________________________________________
Bool_t AliMUONCDB::LoadMapping(Bool_t segmentationOnly)
{
  /// Load mapping (existing mapping will be unloaded).
  /// OCDB path and run number are supposed to be set.
  
  AliInfoGeneral("AliMUONCDB", "Loading mapping from OCDB...");
  
  if (!AliMUONCDB::CheckOCDB()) return kFALSE;
  
  // in case it has already been set
  AliMpCDB::UnloadAll();
  
  if (segmentationOnly) {
    
    if (!AliMpCDB::LoadMpSegmentation(kTRUE)){
      AliErrorGeneral("AliMUONCDB", "failed to load segmentation from OCDB");
      return kFALSE;
    }
    
  } else {
    
    if (!AliMpCDB::LoadAll(kTRUE)) {
      AliErrorGeneral("AliMUONCDB", "failed to load mapping from OCDB");
      return kFALSE;
    }
    
  }
  
  return kTRUE;
  
}

//_____________________________________________________________________________
AliMUONRecoParam* AliMUONCDB::LoadRecoParam()
{
  /// Load and return reconstruction parameters.
  /// OCDB path is supposed to be set.
  
  AliInfoGeneral("AliMUONCDB", "Loading RecoParam from OCDB...");
  
  if (!AliMUONCDB::CheckOCDB()) return 0x0;
  
  AliMUONRecoParam* recoParam = 0x0;
  AliCDBEntry* entry = AliCDBManager::Instance()->Get("MUON/Calib/RecoParam");
  
  if(entry) {
    
    // load recoParam according OCDB content (single or array)
    if (!(recoParam = dynamic_cast<AliMUONRecoParam*>(entry->GetObject()))) {
      
      TObjArray* recoParamArray = static_cast<TObjArray*>(entry->GetObject());
//      recoParamArray->SetOwner(kTRUE); // FIXME: this should be done, but is causing a problem at the end of the reco... investigate why...
      
      for(Int_t i = 0; i < recoParamArray->GetEntriesFast(); i++) {
	recoParam = static_cast<AliMUONRecoParam*>(recoParamArray->UncheckedAt(i));
	if (recoParam->IsDefault()) break;
	recoParam = 0x0;
      }
      
    }
    
  }
  
  if (!recoParam) AliErrorGeneral("AliMUONCDB", "failed to load RecoParam from OCDB");
  
  return recoParam;
  
}

//_____________________________________________________________________________
TClonesArray* AliMUONCDB::LoadAlignmentData()
{
  /// Load and return the array of alignment objects.
  
  AliInfoGeneral("AliMUONCDB", "Loading Alignemnt from OCDB...");
  
  if (!AliMUONCDB::CheckOCDB()) return 0x0;
  
  TClonesArray* alignmentArray = 0x0;
  AliCDBEntry* entry = AliCDBManager::Instance()->Get("MUON/Align/Data");
  
  if (entry) {
    // load alignement array
    alignmentArray = dynamic_cast<TClonesArray*>(entry->GetObject());
  }
  
  if (!alignmentArray) { 
    AliErrorGeneral("AliMUONCDB", "failed to load Alignemnt from OCDB");
  }  
  
  return alignmentArray;
}

//_____________________________________________________________________________
AliMUONVStore* 
AliMUONCDB::Diff(AliMUONVStore& store1, AliMUONVStore& store2, 
                 const char* opt)
{
  /// creates a store which contains store1-store2
  /// if opt="abs" the difference is absolute one,
  /// if opt="rel" then what is stored is (store1-store2)/store1
{'fraction_non_alphanumeric': 0.1421142162818955, 'fraction_numerical': 0.005151883353584447, 'mean_word_length': 3.357475645912749, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4510142', 'n_tokens_mistral': 6723, 'n_tokens_neox': 6435, 'n_words': 1582}