#include <Riostream.h>
#include "AliLHCClockPhase.h"
#include "AliDCSValue.h"
#include "AliLog.h"
using std::endl;
using std::cout;
ClassImp(AliLHCClockPhase)
AliLHCClockPhase::AliLHCClockPhase():
TObject(),
fPhaseB1(),
fPhaseB2()
{
fPhaseB1.SetOwner();
fPhaseB2.SetOwner();
}
void AliLHCClockPhase::AddPhaseB1DP(UInt_t timestamp, Float_t phase)
{
fPhaseB1.AddLast(new AliDCSValue(phase,timestamp));
}
void AliLHCClockPhase::AddPhaseB2DP(UInt_t timestamp, Float_t phase)
{
fPhaseB2.AddLast(new AliDCSValue(phase,timestamp));
}
const AliDCSValue* AliLHCClockPhase::GetPhaseB1DP(Int_t index) const
{
AliDCSValue *value = (AliDCSValue*)fPhaseB1.At(index);
if (!value) {
AliFatal(Form("Invalid index of the beam1 data point: %d (0 -> %d)",
index,fPhaseB1.GetEntries()));
return NULL;
}
return value;
}
const AliDCSValue* AliLHCClockPhase::GetPhaseB2DP(Int_t index) const
{
AliDCSValue *value = (AliDCSValue*)fPhaseB2.At(index);
if (!value) {
AliFatal(Form("Invalid index of the beam2 data point: %d (0 -> %d)",
index,fPhaseB2.GetEntries()));
return NULL;
}
return value;
}
Float_t AliLHCClockPhase::GetMeanPhaseB1() const
{
Int_t n = 0;
Float_t phase = 0;
for(Int_t i = 0; i < fPhaseB1.GetEntries(); ++i) {
AliDCSValue *value = (AliDCSValue*)fPhaseB1.At(i);
if (value) {
phase += value->GetFloat();
++n;
}
}
if (n == 0) {
AliError("No beam1 measurements found! Assuming 0 phase shift!");
return 0;
}
phase /= n;
return phase;
}
Float_t AliLHCClockPhase::GetMeanPhaseB2() const
{
Int_t n = 0;
Float_t phase = 0;
for(Int_t i = 0; i < fPhaseB2.GetEntries(); ++i) {
AliDCSValue *value = (AliDCSValue*)fPhaseB2.At(i);
if (value) {
phase += value->GetFloat();
++n;
}
}
if (n == 0) {
AliError("No beam2 measurements found! Assuming 0 phase shift!");
return 0;
}
phase /= n;
return phase;
}
Float_t AliLHCClockPhase::GetMeanPhase() const
{
return (GetMeanPhaseB1() + GetMeanPhaseB2())/2;
}
Float_t AliLHCClockPhase::GetPhaseB1(UInt_t timestamp) const
{
Long64_t deltat = 0xffffffff;
Float_t phase = 0;
for(Int_t i = 0; i < fPhaseB1.GetEntries(); ++i) {
AliDCSValue *value = (AliDCSValue*)fPhaseB1.At(i);
if (value) {
if (TMath::Abs((Long64_t)timestamp-(Long64_t)value->GetTimeStamp()) <= deltat) {
phase = value->GetFloat();
deltat = TMath::Abs((Long64_t)timestamp-(Long64_t)value->GetTimeStamp());
}
}
}
if (deltat == 0xffffffff) {
AliError(Form("Can't get the beam1 phase shift at time-stamp = %u",timestamp));
return 0;
}
return phase;
}
Float_t AliLHCClockPhase::GetPhaseB2(UInt_t timestamp) const
{
Long64_t deltat = 0xffffffff;
Float_t phase = 0;
for(Int_t i = 0; i < fPhaseB2.GetEntries(); ++i) {
AliDCSValue *value = (AliDCSValue*)fPhaseB2.At(i);
if (value) {
if (TMath::Abs((Long64_t)timestamp-(Long64_t)value->GetTimeStamp()) <= deltat) {
phase = value->GetFloat();
deltat = TMath::Abs((Long64_t)timestamp-(Long64_t)value->GetTimeStamp());
}
}
}
if (deltat == 0xffffffff) {
AliError(Form("Can't get the beam2 phase shift at time-stamp = %u",timestamp));
return 0;
}
return phase;
}
Float_t AliLHCClockPhase::GetPhase(UInt_t timestamp) const
{
return (GetPhaseB1(timestamp) + GetPhaseB2(timestamp))/2;
}
void AliLHCClockPhase::Print( const Option_t* ) const
{
cout << "AliLHCClockPhase object:" << endl;
cout << "Beam1:" << endl;
for(Int_t i = 0; i < fPhaseB1.GetEntries(); ++i) {
AliDCSValue *value = (AliDCSValue*)fPhaseB1.At(i);
if (value) cout << "TS=" << value->GetTimeStamp() << " " << value->GetFloat() << endl;
}
cout << "Beam1 mean phase=" << GetMeanPhaseB1() << " ns" << endl;
cout << "Beam2:" << endl;
for(Int_t i = 0; i < fPhaseB2.GetEntries(); ++i) {
AliDCSValue *value = (AliDCSValue*)fPhaseB2.At(i);
if (value) cout << "TS=" << value->GetTimeStamp() << " " << value->GetFloat() << endl;
}
cout << "Beam2 mean phase=" << GetMeanPhaseB2() << " ns" << endl;
cout << "Mean phase (beam1 & beam2) =" << GetMeanPhase() << " ns" << endl;
}