ATLAS Offline Software
Loading...
Searching...
No Matches
AFP_LBsOutOfRange.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
6
8#include <dqm_core/AlgorithmManager.h>
9#include <dqm_core/exceptions.h>
10
11#include <CoolApplication/DatabaseSvcFactory.h>
12#include <CoolKernel/ChannelSelection.h>
13#include <CoolKernel/IDatabase.h>
14#include <CoolKernel/IDatabaseSvc.h>
15#include <CoolKernel/IField.h>
16#include <CoolKernel/IFolder.h>
17#include <CoolKernel/IObject.h>
18#include <CoolKernel/IObjectIterator.h>
19#include <CoolKernel/IRecord.h>
20#include <CoolKernel/ValidityKey.h>
21#include <CoolKernel/pointers.h>
22#include <CoolKernel/types.h>
23
24#include <TDirectory.h>
25#include <TH1.h>
26#include <TFile.h>
27
28namespace {
30 constexpr uint64_t LB_MASK = 0xffffffff;
31 uint64_t getTime( const cool::IObject& obj, const std::string& name ) {
32 return obj.payload()[ name ].data<cool::UInt63>();
33 }
34 uint64_t getStartTime( const cool::IObject& obj ) {
35 return getTime( obj, "StartTime" );
36 }
37 uint64_t getEndTime( const cool::IObject& obj ) {
38 return getTime( obj, "EndTime" );
39 }
40 uint32_t getLB( const cool::IObject& obj ) {
41 return obj.since() & LB_MASK;
42 }
43} // namespace
44
46 dqm_core::AlgorithmManager::instance().registerAlgorithm( "AFP_LBsOutOfRange", this );
47}
48
51
56
59 auto& iovs = m_iov_cache[ std::make_pair( run, channel ) ];
60 if ( iovs.empty() ) {
61 auto& dbService = cool::DatabaseSvcFactory::databaseService();
62 // Get map of LB timestamps to LB numbers
63 uint64_t runStartTime = 0xFFffFFffFFffFFff;
64 uint64_t runEndTime = 0;
65 std::map<uint64_t, uint32_t> lbs;
66 {
67 auto databaseName = std::string( "COOLONL_TRIGGER/CONDBR2" );
68 auto folderName = std::string( "/TRIGGER/LUMI/LBLB" );
69 auto databasePtr = dbService.openDatabase( databaseName, true );
70 auto folderPtr = databasePtr->getFolder( folderName );
71 uint64_t run64 = run;
72 auto since = cool::ValidityKey( run64 << 32 );
73 auto until = cool::ValidityKey( ( run64 + 1 ) << 32 );
74 auto iterPtr = folderPtr->browseObjects( since, until, cool::ChannelSelection::all() );
75 while ( iterPtr->goToNext() ) {
76 auto& object = iterPtr->currentRef();
77 auto lbStartTime = getStartTime( object );
78 auto lbEndTime = getEndTime( object );
79 lbs.emplace( lbStartTime, getLB( object ) );
80 if ( lbStartTime < runStartTime ) runStartTime = lbStartTime;
81 if ( lbEndTime > runEndTime ) runEndTime = lbEndTime;
82 }
83 if ( lbs.empty() ) throw afp::CantReadCool( ERS_HERE, std::move(databaseName), std::move(folderName) );
84 }
85 // Get IOVs where the station was in physics position,
86 // merge the neighbouring ones,
87 // and translate the timestamps to LB numbers
88 {
89 auto databaseName = std::string( "COOLOFL_DCS/CONDBR2" );
90 auto folderName = std::string( "/AFP/DCS/STATION" );
91 auto databasePtr = dbService.openDatabase( databaseName, true );
92 auto folderPtr = databasePtr->getFolder( folderName );
93 auto iterPtr = folderPtr->browseObjects( runStartTime, runEndTime, cool::ChannelSelection( channel ) );
94 uint64_t since = 0, until = 0;
95 iovs.reserve( iterPtr->size() / 2 );
96 while ( iterPtr->goToNext() ) {
97 auto& object = iterPtr->currentRef();
98 auto inphysics = object.payload()[ "inphysics" ].data<cool::Bool>();
99 if ( inphysics ) {
100 if ( since == 0 ) since = object.since();
101 until = object.until();
102 } else if ( since != 0 ) {
103 uint32_t start = lbs.lower_bound( since )->second;
104 uint32_t end = ( --lbs.upper_bound( until ) )->second;
105 iovs.emplace_back( start, end );
106 since = 0;
107 }
108 }
109 }
110 // Ensure the list is not empty
111 if ( iovs.empty() ) {
112 iovs.emplace_back( 0u, 0u );
113 }
114 }
115 return iovs;
116}
117
118dqm_core::Result*
120 const TObject& object,
121 const dqm_core::AlgorithmConfig& config ) {
122 if ( !object.IsA()->InheritsFrom( "TH1" ) ) {
123 throw dqm_core::BadConfig( ERS_HERE, name, "does not inherit from TH1" );
124 }
125
126 auto histogram = static_cast<const TH1*>( &object );
127
128 if ( histogram->GetDimension() > 1 ) {
129 throw dqm_core::BadConfig( ERS_HERE, name, "histogram has more than 1 dimension" );
130 }
131
132 auto gthreshold = static_cast<uint32_t>( dqm_algorithms::tools::GetFromMap( "NbadBins", config.getGreenThresholds() ) );
133 auto rthreshold = static_cast<uint32_t>( dqm_algorithms::tools::GetFromMap( "NbadBins", config.getRedThresholds() ) );
134
135 auto channel = static_cast<uint32_t>( dqm_algorithms::tools::GetFirstFromMap( "channel", config.getParameters() ) );
136 auto RANGE_D = static_cast<double>( dqm_algorithms::tools::GetFirstFromMap( "RANGE_D", config.getParameters(), -100 ) );
137 auto RANGE_U = static_cast<double>( dqm_algorithms::tools::GetFirstFromMap( "RANGE_U", config.getParameters(), 100 ) );
138 auto ignoreval = static_cast<double>( dqm_algorithms::tools::GetFirstFromMap( "ignoreval", config.getParameters(), 0 ) );
139 auto minLBs = static_cast<uint32_t>( dqm_algorithms::tools::GetFirstFromMap( "MinLBs", config.getParameters(), 0 ) );
140
141 auto run = dqm_algorithms::tools::GetFirstFromMap( "run_number", config.getParameters(), 0 );
142 auto& iovs = run > 0 ? fetchIOVs( run, channel ) : IOVSet{ { 0, 0 } };
143
144 auto range = dqm_algorithms::tools::GetBinRange( histogram, config.getParameters() );
145 uint32_t xmin = range[ 0 ];
146 uint32_t xmax = range[ 1 ] + 1;
147 auto xaxis = histogram->GetXaxis();
148 std::vector<uint32_t> badbins;
149 uint32_t checkedLBs = 0;
150 for ( auto& iov : iovs ) {
151 auto xstart = std::max<uint32_t>( xaxis->FindFixBin( iov.first ), xmin );
152 auto xend = std::min<uint32_t>( xaxis->FindFixBin( iov.second ), xmax );
153 for ( uint32_t ix = xstart; ix < xend; ++ix ) {
154 double binvalue = histogram->GetBinContent( ix );
155 if ( binvalue == ignoreval ) continue;
156 ++checkedLBs;
157 if ( RANGE_D < binvalue && binvalue < RANGE_U ) continue;
158 badbins.push_back( ix );
159 }
160 }
161
162 auto publish = static_cast<bool>( dqm_algorithms::tools::GetFirstFromMap( "PublishBins", config.getParameters(), true ) );
163 auto Nmaxpublish = static_cast<uint32_t>( dqm_algorithms::tools::GetFirstFromMap( "MaxPublish", config.getParameters(), 20 ) );
164 auto result = new dqm_core::Result();
165
166 // publish problematic bins
167 result->tags_[ "NBadBins" ] = badbins.size();
168 if ( publish ) {
169 auto npublish = std::min<uint32_t>( Nmaxpublish, badbins.size() );
170 for ( uint32_t i = 0; i < npublish; ++i ) {
171 uint32_t ix = badbins[ i ];
172 uint32_t lb = std::floor( xaxis->GetBinCenter( ix ) );
173 auto tag = ( std::ostringstream() << "LB " << lb ).str();
174
175 result->tags_[ tag ] = histogram->GetBinContent( ix );
176 }
177 }
178
179 if ( checkedLBs < minLBs )
180 result->status_ = dqm_core::Result::Undefined;
181 else if ( badbins.size() > rthreshold )
182 result->status_ = dqm_core::Result::Red;
183 else if ( badbins.size() > gthreshold )
184 result->status_ = dqm_core::Result::Yellow;
185 else
186 result->status_ = dqm_core::Result::Green;
187
188 return result;
189}
190
192 out << "AFP_LBsOutOfRange: Print out the bad LBs, during which AFP was in physics position, and which are out of range [RANDE_D,RANGE_U] (default [-100,100])\n"
193 << "Required Parameter: channel: channel number of the station in COOLOFL_DCS/CONDBR2 /AFP/DCS/STATION\n"
194 << "Optional Parameter: ignoreval: value to be ignored for being processed (default 0)\n"
195 << "Optional Parameter: MaxPublish: Max number of bins to save (default 20)\n"
196 << "Optional Parameter: MinLBs: Minimum number of LBs in physics with value not equal to ignoreval to assign color (default 0)" << std::endl;
197}
std::map< std::string, double > instance
std::string histogram
Definition chains.cxx:52
AFP_LBsOutOfRange * clone() override
dqm_core::Result * execute(const std::string &name, const TObject &object, const dqm_core::AlgorithmConfig &config) override
void printDescriptionTo(std::ostream &out) override
std::vector< std::pair< int, int > > IOVSet
const IOVSet & fetchIOVs(uint32_t run, uint32_t channel)
std::map< std::pair< uint32_t, uint32_t >, IOVSet > m_iov_cache
int lb
Definition globals.cxx:23
double xmax
Definition listroot.cxx:61
double xmin
Definition listroot.cxx:60
float getTime(const xAOD::TrackParticle &p)
Accessor utility function for getting the track time.
std::vector< int > GetBinRange(const TH1 *histogram, const std::map< std::string, double > &params)
double GetFirstFromMap(const std::string &paramName, const std::map< std::string, double > &params)
const T & GetFromMap(const std::string &pname, const std::map< std::string, T > &params)
Definition run.py:1
setEventNumber uint32_t
#define IsA
Declare the TObject style functions.