ATLAS Offline Software
Loading...
Searching...
No Matches
CaloWriteNoise.cxx File Reference
#include <iostream>
#include <iomanip>
#include <climits>
#include <fstream>
#include <boost/program_options.hpp>
#include <chai/Database.h>
#include <chai/Container.h>
#include <chai/Log.h>
#include <chai/Types.h>
#include <CaloCondBlobObjs/CaloCondBlobFlt.h>

Go to the source code of this file.

Macros

#define ENCODE_RUNLUMIBLOCK(run, lumiblock)
 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration.
#define DECODE_RUN(since)
#define DECODE_LUMIBLOCK(since)
#define NUM_SCELLS   34048
#define NORM_FACTOR_DT25   29.
#define NORM_FACTOR_DT50   53.

Functions

chai::BlobData blobToBlobData (const coral::Blob &coralBlob)
coral::Blob BlobDataToBlob (const chai::BlobData &blob)
int main (int argc, char **argv)

Macro Definition Documentation

◆ DECODE_LUMIBLOCK

#define DECODE_LUMIBLOCK ( since)
Value:
static_cast<uint32_t>(since & 0xFFFFFFFF)

Definition at line 29 of file CaloWriteNoise.cxx.

◆ DECODE_RUN

#define DECODE_RUN ( since)
Value:
static_cast<uint32_t>(since >> 32)

Definition at line 28 of file CaloWriteNoise.cxx.

◆ ENCODE_RUNLUMIBLOCK

#define ENCODE_RUNLUMIBLOCK ( run,
lumiblock )
Value:
(static_cast<uint64_t>(run) << 32) | static_cast<uint64_t>(lumiblock)
int run(int argc, char *argv[])

Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration.

CHAI based filling of CaloNoise

Environment Variables needed:

Definition at line 27 of file CaloWriteNoise.cxx.

◆ NORM_FACTOR_DT25

#define NORM_FACTOR_DT25   29.

Definition at line 31 of file CaloWriteNoise.cxx.

◆ NORM_FACTOR_DT50

#define NORM_FACTOR_DT50   53.

Definition at line 32 of file CaloWriteNoise.cxx.

◆ NUM_SCELLS

#define NUM_SCELLS   34048

Definition at line 30 of file CaloWriteNoise.cxx.

Function Documentation

◆ BlobDataToBlob()

coral::Blob BlobDataToBlob ( const chai::BlobData & blob)

Definition at line 49 of file CaloWriteNoise.cxx.

49 {
50 long size = blob.m_bytes.size();
51 coral::Blob data(size);
52 if(size > 0) {
53 memcpy(data.startingAddress(), blob.m_bytes.data(), size);
54 }
55
56 return data;
57}
size_t size() const
Number of registered mappings.

◆ blobToBlobData()

chai::BlobData blobToBlobData ( const coral::Blob & coralBlob)

Definition at line 36 of file CaloWriteNoise.cxx.

36 {
37 const void* data = coralBlob.startingAddress();
38 long size = coralBlob.size();
39
40 // Create vector and copy data
41 std::vector<uint8_t> bytes(size);
42 if (size > 0 && data != nullptr) {
43 memcpy(bytes.data(), data, size);
44 }
45
46 return chai::BlobData(std::move(bytes));
47}

◆ main()

int main ( int argc,
char ** argv )

Definition at line 59 of file CaloWriteNoise.cxx.

59 {
60
61 boost::program_options::options_description description( "Options" );
62
63 description.add_options()
64 ( "help,h", "produce help message" )
65 ( "runstart,r", boost::program_options::value<uint32_t>()->default_value(0), "run number to start IOV" )
66 ( "lbstart,l", boost::program_options::value<uint32_t>()->default_value(0), "lumi block to start IOV" )
67 ( "runend,R", boost::program_options::value<uint32_t>()->default_value(0), "run number to end IOV (infty if 0)" )
68 ( "lbend,L", boost::program_options::value<uint32_t>()->default_value(0), "lumi block to end IOV (infty if 0)" )
69 ( "input,i", boost::program_options::value<std::string>()->default_value("calonoise.txt"), "name of input text file" )
70 ( "output,t", boost::program_options::value<std::string>()->default_value(""), "name of output text file" )
71 ( "mu,,", boost::program_options::value<uint32_t>()->default_value(0), "mu for which to fill noise" )
72 ( "dt,,", boost::program_options::value<float>()->default_value(25.), "bunch spacing to use" )
73 ( "folder,f", boost::program_options::value<std::string>()->default_value("/LAR/NoiseOfl/CellNoise"), "name of Folder" )
74 ( "tag,a", boost::program_options::value<std::string>()->default_value("LARNoiseOflCellNoisenoise-mc16-EposA3-ofc25mu140-25ns"), "name of folder tag" )
75 ( "crest,C", boost::program_options::value<std::string>()->default_value(""), "CREST URL string for storing" )
76 ( "crestfs,c", boost::program_options::value<std::string>()->default_value("/tmp/crest"), "CREST FS string for storing" )
77 ( "globalTag,g", boost::program_options::value<std::string>()->default_value(""), "Global tag in CREST" )
78 ( "head,H", boost::program_options::bool_switch()->default_value(false), "Use HEAD tag" )
79 ( "loglevel,o", boost::program_options::value<int>()->default_value(3), "chai logLevel" )
80 ( "readonly,D", boost::program_options::bool_switch()->default_value(false), "Only read the DB" );
81
82 boost::program_options::variables_map arguments;
83 try {
84 boost::program_options::store( boost::program_options::parse_command_line( argc, argv, description ), arguments );
85 boost::program_options::notify( arguments );
86 }
87 catch ( boost::program_options::error & ex ) {
88 std::cerr << ex.what() << std::endl;
89 description.print( std::cout );
90 return 1;
91 }
92
93 if ( arguments.count("help") ) {
94 std::cout << "LAr noise filling application" << std::endl;
95 description.print( std::cout );
96 return 0;
97 }
98
99 chai::setLogLevel(static_cast<chai::LogLevel>(arguments["loglevel"].as<int>()));
100
101 std::string globalTag(arguments["globalTag"].as<std::string>());
102 std::string folder(arguments["folder"].as<std::string>());
103 std::string ftag(arguments["tag"].as<std::string>());
104 bool isRead(arguments["readonly"].as<bool>());
105 unsigned int runs(arguments["runstart"].as<uint32_t>());
106 unsigned int rune=(arguments["runend"].as<uint32_t>()) > 0 ? arguments["runend"].as<uint32_t>() : UINT_MAX;
107 unsigned int lbs=(arguments["lbstart"].as<uint32_t>());
108 unsigned int lbe=(arguments["lbend"].as<uint32_t>()) > 0 ? arguments["lbend"].as<uint32_t>() : UINT_MAX;
109 unsigned int mu=(arguments["mu"].as<uint32_t>());
110 float dt=(arguments["dt"].as<float>());
111 std::string server(arguments["crest"].as<std::string>());
112 std::string fsdir(arguments["crestfs"].as<std::string>());
113
114 if(!isRead){
115 // read the input file
116 std::vector<std::pair<float,float> > noisetable;
117 std::ifstream inpf(arguments["input"].as<std::string>());
118 // SC case
119 // assuming onlId subHash gain noiseA noiseB
120 unsigned int chanId, subh, igain;
121 unsigned int prevId=0;
122 float noisea=0, noiseb=0.;
123 while(inpf >> chanId >> subh >> igain >> noisea >> noiseb) {
124 if(igain != 0 || chanId <= prevId) { //something wrong
125 std::cout<<"Wrong line in input file, starting with "<<chanId<<std::endl;
126 continue;
127 }
128 noisetable.push_back(std::make_pair(noisea, noiseb));
129 prevId=chanId;
130 }
131 if(noisetable.size() != NUM_SCELLS) {
132 std::cout<<"Bad length of noise values, exiting !!"<<std::endl;
133 return -1;
134 } else {
135 std::cout<<"Read out " << NUM_SCELLS << " noise values"<<std::endl;
136 inpf.close();
137 }
138
139 // Create CaloBlobs to write out
140 coral::Blob blob(5*sizeof(uint32_t)+2*noisetable.size()*sizeof(float));
141 // filling the proper type
142 uint32_t* pDestination = static_cast<uint32_t*> (blob.startingAddress());
143 pDestination[0] = 1;
144
146 //Blob Defintion Vector
147 std::vector<float> gVec;
148 gVec.push_back(0.);//a
149 gVec.push_back(0.);//b
150 std::vector<std::vector<float> > defVec;
151 defVec.push_back(gVec);
152 flt->init(defVec,noisetable.size(),1);
153
154 // Fill the flt
155 try {
156 for(unsigned h=0; h<noisetable.size(); ++h) {
157 flt->setData(h,0,0,noisetable[h].first);
158 if (mu > 0 && dt > 0) {
159 // new normalization
160 if (dt > 25)
161 flt->setData(h,0,1,noisetable[h].second / std::sqrt(mu/NORM_FACTOR_DT50*10.) ); // multiply by 10. to get units stored in cond DB.
162 else
163 flt->setData(h,0,1,noisetable[h].second / std::sqrt(mu/NORM_FACTOR_DT25*10.) ); // multiply by 10. t o get units stored in cond DB.
164 } else {
165 flt->setData(h,0,1,noisetable[h].second );
166 }
167 }
168 }
169 catch (const CaloCond::IndexOutOfRange& e) {
170 std::cerr << std::endl << "Error: " << e.what() << std::endl;
171 return 1;
172 }
173
174 // Build the connection string
175 std::string connectionString = (server.size()==0) ? "crest_fs:" + fsdir : "crest:" + server;
176
177 std::cout << "Connecting to: " << connectionString << std::endl << std::endl;
178
179 try {
180 // Connect to CREST database
181 chai::Database db(connectionString);
182
183 // Delete the dest. tag if it exists
184 try {
185 if (db.getTag(ftag)) {
186 db.deleteTag(ftag);
187 }
188 } catch (const std::exception& e) {
189 // Ignore errors
190 }
191
192
193 // 1. Create a tag with payload specification
194 std::cout << "Creating tag "<< ftag << std::endl;
195
196 chai::PayloadSpec spec(
197 chai::FieldSpec({
198 {"CaloCondBlob16M", chai::Blob}
199 }),
200 chai::ChannelSpec({ {0,""} })
201 );
202
203 chai::Tag::Metadata chaiMD{
204 .iovType=chai::Tag::IovType::RunNumberLumiBlock,
205 .objectType="crest-json-single-iov",
206 .synchronization=chai::Tag::Synchronization::All,
207 .status=chai::Tag::Status::Unlocked,
208 .nodeDescription =
209 chai::Tag::buildNodeDescription(
210 chai::Tag::IovType::RunNumberLumiBlock,
211 "CondAttrListCollection",
212 1238547719u
213 )
214 };
215 auto tag = db.createTag(
216 ftag,
217 "SC noise tag",
218 spec,
219 chaiMD
220 );
221
222 std::cout << "Tag created: " << tag->getName() << std::endl;
223 std::cout << "IOV Type: RunNumberLumiBlock" << std::endl;
224
225 // 2. Create container
226
227 chai::Container container(spec);
228
229 container[0].push(chai::BlobData(blobToBlobData(blob)));
230
231 std::cout << "Container created with " << container.numChannels() << " channels" << std::endl;
232
233 // 3. Write IOVs for selected run/lumiblock interval
234 std::cout << "Writing IOVs for selected run/lumiblock interval..." << std::endl;
235
236 // IOV:
239 tag->addPayload(container, since, until);
240
241 std::cout << "All IOVs written successfully!" << std::endl << std::endl;
242 } catch (const std::exception& e) {
243 std::cerr << std::endl << "Error: " << e.what() << std::endl;
244 return 1;
245 }
246
247 }
248 try {
249 // ====================================================================
250 // READ SECTION
251 // ====================================================================
252 std::cout << "================================================================================" << std::endl;
253 std::cout << "READ: Retrieving Tag and Reading Back Payloads" << std::endl;
254 std::cout << "================================================================================" << std::endl;
255 // Build the connection string
256 std::string connectionString = (server.size()==0) ? "crest_fs:" + fsdir : "crest:" + server;
257
258 std::cout << "Connecting to: " << connectionString << std::endl << std::endl;
259
260 chai::Database db(connectionString);
261
262 // 1. Get tag by name
263 std::cout << "Getting tag by name "<<ftag<<" ..." << std::endl;
264 auto retrievedTag = db.getTag(ftag);
265 std::cout << "Tag retrieved: " << retrievedTag->getName() << std::endl;
266 std::cout << "Description: " << retrievedTag->getDescription() << std::endl;
267 std::cout << "Number of IOVs: " << retrievedTag->getSize() << std::endl;
268
269 // 2. Read back IOVs from tag
270 std::cout << "Iterating over IOVs and payloads..." << std::endl << std::endl;
271
272 // 3. Open output file if asked
273 std::ofstream onpf;
274 if(arguments["output"].as<std::string>().size()>0) onpf.open(arguments["output"].as<std::string>());
275
276 for (const auto& iov : retrievedTag->getIovs(0)) {
277 uint64_t since = iov.getSince();
278 uint32_t run = DECODE_RUN(since);
280
281 std::cout << "IOV: Run " << run << ", Lumiblock " << lumiblock << " (encoded: " << since << ")" << std::endl;
282 std::cout << " Payload hash: " << iov.getPayloadHash().substr(0, 16) << "..." << std::endl;
283
284 // 3. Get payload as a container
285 auto payloadContainer = retrievedTag->getPayload(iov.getPayloadHash());
286
287 std::cout << " Payload has " << payloadContainer.numChannels() << " channel(s)" << std::endl;
288
289 // 4. For each channel, print values and access by name
290 for (uint64_t channelId : payloadContainer.channelIds()) {
291
292 // Access by index (positional)
293 if(payloadContainer.hasChannel(channelId)) {
294 const auto cblob = BlobDataToBlob(payloadContainer.get<chai::BlobData>(channelId,"CaloCondBlob16M"));
295 std::cout << " Channel " << channelId << " size: " << cblob.size() << std::endl;
297 std::cout << "nGains: " << rflt->getNGains() << " nChannels: " << rflt->getNChans() << std::endl;
298 for(unsigned h=0; h<rflt->getNChans(); ++h) {
299 std::cout<< h << " noisea: " << rflt->getData(h,0,0) << " noiseb: " << rflt->getData(h,0,1) << std::endl;
300 }
301 } else {
302 std::cout << " Channel " << channelId << " does not have data in this IOV" << std::endl;
303 }
304 }
305
306 std::cout << "Done"<<std::endl;
307 }
308
309 } catch (const std::exception& e) {
310 std::cerr << std::endl << "Error: " << e.what() << std::endl;
311 return 1;
312 }
313
314 return 0;
315}
chai::BlobData blobToBlobData(const coral::Blob &coralBlob)
#define DECODE_RUN(since)
#define DECODE_LUMIBLOCK(since)
#define ENCODE_RUNLUMIBLOCK(run, lumiblock)
Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration.
#define NUM_SCELLS
#define NORM_FACTOR_DT25
coral::Blob BlobDataToBlob(const chai::BlobData &blob)
#define NORM_FACTOR_DT50
Header file for AthHistogramAlgorithm.
uint32_t getNGains() const
Returns the number of gains stored for each channel.
uint32_t getNChans() const
Returns the number of channels stored in the BLOB.
T getData(const unsigned int channel, const unsigned int adc, const unsigned int idx) const
Returns a single T belonging to a channel/gain.
Class for storing a number of floats (Flt) and functions on those.
static CaloCondBlobFlt * getInstance(coral::Blob &blob)
Returns a pointer to a non-const CaloCondBlobFlt.
std::string description
glabal timer - how long have I taken so far?
Definition hcg.cxx:93
const SG::AuxVectorData * container() const
Return the container holding this element.
setEventNumber uint32_t