Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | Static Public Member Functions | Private Attributes | List of all members
JSUPERNOVA::JL0BackgroundSimulator Class Reference

Class to emulate L0 background for an arbitrarily sized detector. More...

#include <JL0BackgroundSimulator.hh>

Public Member Functions

 JL0BackgroundSimulator (TFile *in, bool twoDim=false)
 Default constructor. More...
 
 ~JL0BackgroundSimulator ()
 Destructor. More...
 
void configureTimeWindow (const int T_ms)
 Configure the duration of an output sample. More...
 
void setSeed (const UInt_t uSeed=0)
 Set TRandom seed. More...
 
void configureRatio (const int iSize, const int oSize)
 Set rebinning ratio. More...
 
bg_type generate ()
 Generate sample of L0 background L0 data are randomly sampled from a single L0 dataset. More...
 
bg_type generate_poisson (const int domRate_Hz=500)
 Generate pure poissionian background. More...
 
bg_type generate_shuffled (bool randomizeRun=false)
 Generate sample of L0 background The sampling of the L0 data is not sequential but random within the L0 dataset. More...
 
h2d_tbuild_H2D (const int k, const int start)
 Generate 2D histogram. More...
 
vector< double > build_NC (const int k, const int start)
 Build NC sequence. More...
 
h2d_tgenerate_H2D (int rb=1)
 Generate 2D sample. More...
 
bg_type generate_fitted (int rb=1)
 Generate fitted L1 sample. More...
 

Static Public Member Functions

static vector< double > rebin (const vector< double > &in, const int rb)
 Rebin vector. More...
 
static bg_type fit_H2D (h2d_t *in)
 Fit 2D sample. More...
 

Private Attributes

vector< vector< double > > rt
 
vector< vector< double > > nc
 
vector< h2d_t * > rt2d
 
int TWindow_ms
 
int genRatio
 
int outputSize
 
double genScale
 
TRandom * rnd
 

Detailed Description

Class to emulate L0 background for an arbitrarily sized detector.

Definition at line 57 of file JL0BackgroundSimulator.hh.

Constructor & Destructor Documentation

JSUPERNOVA::JL0BackgroundSimulator::JL0BackgroundSimulator ( TFile *  in,
bool  twoDim = false 
)
inline

Default constructor.

The input file contains a sequence of histograms with sampled L0 data; Each histogram is loaded as a vector, which will be considered as an individual L0 dataset.

Parameters
ininput file from JRipple

Definition at line 83 of file JL0BackgroundSimulator.hh.

83  {
84 
85  TWindow_ms = 100;
86  genRatio = 115;
87  genScale = 1;
88  // rebin = 1;
89 
90  TString rt_tag = ".RT_DET_SUM";
91  TString nc_tag = ".NC_DET_SUM";
92  TString rt2d_tag = ".RT2D_DET";
93 
94  rnd = new TRandom();
95 
96  TIter iter(in->GetListOfKeys());
97 
98  for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
99 
100  const TString tag(key->GetName());
101 
102  // match
103  if (tag.EndsWith(rt_tag)) {
104 
105  TString rt_hist_tag = tag;
106  TString run_tag = TString(tag, tag.Length() - rt_tag.Length());
107 
108  TString nc_hist_tag = run_tag + nc_tag;
109 
110  TH1D* RT = (TH1D*) in->Get(rt_hist_tag);
111  TH1D* NC = (TH1D*) in->Get(nc_hist_tag);
112 
113  vector<double> rt_buf = loadHistogram(RT);
114  vector<double> nc_buf = loadHistogram(NC);
115 
116  int n = nc_buf.size();
117 
118  rt.push_back( vector<double>() );
119  nc.push_back( vector<double>() );
120 
121  // build parallel vectors
122  // 1 NC bin <=> 100 RT bins
123  for (int i = 0; i < n; i++) {
124  // empty slices are discarded
125  if (nc_buf[i] > 0) {
126  nc.back().push_back( nc_buf[i] );
127  for (int j = 100 * i; j < 100 * (i + 1); j++) {
128  rt.back().push_back( rt_buf[j] );
129  }
130  }
131  }
132 
133  if (twoDim) {
134  TString rt2d_hist_tag = run_tag + rt2d_tag;
135  h2d_t* RT2D = (h2d_t*) in->Get(rt2d_hist_tag);
136 
137  if (RT2D != NULL) {
138  RT2D->SetDirectory(0);
139  rt2d.push_back(RT2D);
140  }
141  }
142 
143  }
144 
145  }
146 
147  }
TH2F h2d_t
Definition: JRipple.hh:7
vector< double > loadHistogram(TH1D *in)
Load histogram values to vector, each bin is converted to an element.
JSUPERNOVA::JL0BackgroundSimulator::~JL0BackgroundSimulator ( )
inline

Destructor.

Definition at line 152 of file JL0BackgroundSimulator.hh.

152  {
153  delete rnd;
154  }

Member Function Documentation

void JSUPERNOVA::JL0BackgroundSimulator::configureTimeWindow ( const int  T_ms)
inline

Configure the duration of an output sample.

Parameters
T_mssample duration in ms

Definition at line 161 of file JL0BackgroundSimulator.hh.

161  {
162  TWindow_ms = T_ms;
163  }
void JSUPERNOVA::JL0BackgroundSimulator::setSeed ( const UInt_t  uSeed = 0)
inline

Set TRandom seed.

Parameters
uSeedseed

Definition at line 170 of file JL0BackgroundSimulator.hh.

170  {
171  rnd->SetSeed(uSeed);
172  }
void JSUPERNOVA::JL0BackgroundSimulator::configureRatio ( const int  iSize,
const int  oSize 
)
inline

Set rebinning ratio.

Parameters
rebinvoid setRebin(const int rb) { rebin = rb; } Configure generation ratio
inputSizenumber of lines of input detector
outputSizenumber of lines of output detector

Definition at line 189 of file JL0BackgroundSimulator.hh.

189  {
190  outputSize = oSize;
191  // ratio is integer by design
192  genRatio = oSize / iSize;
193  // scaling factor to compensate the ratio remainder
194  genScale = oSize / (1.0 * iSize * genRatio);
195  }
bg_type JSUPERNOVA::JL0BackgroundSimulator::generate ( )
inline

Generate sample of L0 background L0 data are randomly sampled from a single L0 dataset.

Returns
2D vector with hit count and number of active channels as a function of time

Definition at line 204 of file JL0BackgroundSimulator.hh.

204  {
205 
207 
208  int k = rnd->Integer(rt.size());
209 
210  int limit = rt[k].size() - (genRatio * TWindow_ms);
211 
212  int start = rnd->Integer(limit);
213 
214  for (int i = 0; i < genRatio; i++) {
215 
216  for (int j = 0; j < TWindow_ms; j++) {
217 
218  int bin = start + (i * TWindow_ms) + j;
219 
220  out[0][j] += genScale * rt[k].at(bin );
221  out[1][j] += genScale * nc[k].at(bin / 100);
222 
223  }
224 
225  }
226 
227  return out;
228  }
vector< vector< double > > bg_type
bg_type JSUPERNOVA::JL0BackgroundSimulator::generate_poisson ( const int  domRate_Hz = 500)
inline

Generate pure poissionian background.

Definition at line 233 of file JL0BackgroundSimulator.hh.

233  {
234 
236 
237  double mu = outputSize * 18 * domRate_Hz * 1.0e-3;
238 
239  for (unsigned i = 0; i < out[0].size(); i++) {
240 
241  double count = rnd->Poisson(mu);
242 
243  out[0][i] = count;
244  out[1][i] = outputSize * 18 * 31;
245 
246  }
247 
248  return out;
249 
250  }
vector< vector< double > > bg_type
bg_type JSUPERNOVA::JL0BackgroundSimulator::generate_shuffled ( bool  randomizeRun = false)
inline

Generate sample of L0 background The sampling of the L0 data is not sequential but random within the L0 dataset.

Returns
2D vector with hit count and number of active channels as a function of time

Definition at line 258 of file JL0BackgroundSimulator.hh.

258  {
259 
261 
262  int nRuns = rt.size();
263 
264  int k = rnd->Integer(nRuns);
265 
266  // a series n = 'genRatio' elements is sampled randomly within the dataset
267  // all the elements start with the same offset to the timeslice beginning
268 
269  int offset = rnd->Integer(100);
270 
271  for (int i = 0; i < genRatio; i++) {
272 
273  int start = 100 * rnd->Integer(nc[k].size() - ceil(TWindow_ms / 100.0)) + offset;
274 
275  for (int j = 0; j < TWindow_ms; j++) {
276 
277  int bin = start + j;
278 
279  out[0][j] += genScale * rt[k].at(bin );
280  out[1][j] += genScale * nc[k].at(bin / 100);
281 
282  }
283 
284  if (randomizeRun) {
285  k = rnd->Integer(nRuns);
286  }
287 
288  }
289 
290  return out;
291  }
vector< vector< double > > bg_type
h2d_t* JSUPERNOVA::JL0BackgroundSimulator::build_H2D ( const int  k,
const int  start 
)
inline

Generate 2D histogram.

Definition at line 297 of file JL0BackgroundSimulator.hh.

297  {
298 
299  h2d_t* proto = rt2d[k];
300 
301  int ny = proto->GetNbinsY();
302  int ymin = proto->GetYaxis()->GetXmin();
303  int ymax = proto->GetYaxis()->GetXmax();
304 
305  h2d_t* out = new h2d_t("sample", NULL, TWindow_ms, 0, TWindow_ms, ny, ymin, ymax);
306 
307  for (int i = 0; i < genRatio; i++) {
308 
309  for (int j = 0; j < TWindow_ms; j++) {
310 
311  int xbin = 1 + start + (i * TWindow_ms) + j;
312 
313  for (int ybin = 1; ybin <= rt2d[k]->GetNbinsY(); ybin++) {
314 
315  double val = rt2d[k]->GetBinContent(xbin, ybin);
316 
317  double bcx = ((TAxis*)out->GetXaxis())->GetBinCenter(j + 1);
318  double bcy = ((TAxis*)out->GetYaxis())->GetBinCenter(ybin);
319 
320  out->Fill(bcx, bcy, val);
321 
322  }
323 
324  }
325 
326  }
327 
328  return out;
329 
330  }
TH2F h2d_t
Definition: JRipple.hh:7
vector<double> JSUPERNOVA::JL0BackgroundSimulator::build_NC ( const int  k,
const int  start 
)
inline

Build NC sequence.

Definition at line 336 of file JL0BackgroundSimulator.hh.

336  {
337 
339 
340  for (int i = 0; i < genRatio; i++) {
341 
342  for (int j = 0; j < TWindow_ms; j++) {
343 
344  int bin = start + (i * TWindow_ms) + j;
345 
346  out[j] += genScale * nc[k].at(bin / 100);
347 
348  }
349 
350  }
351 
352  return out;
353  }
static vector<double> JSUPERNOVA::JL0BackgroundSimulator::rebin ( const vector< double > &  in,
const int  rb 
)
inlinestatic

Rebin vector.

Definition at line 358 of file JL0BackgroundSimulator.hh.

358  {
359 
360  const int n = in.size();
361 
362  vector<double> out(n/rb);
363 
364  for (int i = 0; i < n; i++) {
365  out[i/rb] += in[i];
366  }
367 
368  return out;
369 
370  }
h2d_t* JSUPERNOVA::JL0BackgroundSimulator::generate_H2D ( int  rb = 1)
inline

Generate 2D sample.

Definition at line 375 of file JL0BackgroundSimulator.hh.

375  {
376 
377  h2d_t* out = NULL;
378 
379  if (rt2d.size() > 0) {
380 
381  int k = rnd->Integer(rt2d.size());
382 
383  int limit = rt2d[k]->GetNbinsX() - (genRatio * TWindow_ms);
384 
385  int start = rnd->Integer(limit);
386 
387  out = build_H2D(k, start);
388 
389  }
390 
391  if (rb != 1) { out->RebinX(rb); }
392 
393  return out;
394 
395  }
TH2F h2d_t
Definition: JRipple.hh:7
h2d_t * build_H2D(const int k, const int start)
Generate 2D histogram.
static bg_type JSUPERNOVA::JL0BackgroundSimulator::fit_H2D ( h2d_t in)
inlinestatic

Fit 2D sample.

Definition at line 401 of file JL0BackgroundSimulator.hh.

401  {
402 
403  int n = in->GetNbinsX();
404 
405  // 0 -> signal
406  // 1 -> fitted background
407  // 2 -> background fit error
408 
409  bg_type out(3, vector<double>(n));
410 
411  for (int bin = 1; bin <= n; bin++) {
412 
413  double sg = 0, bg = 0, er = 0;
414 
415  TH1D* h = in->ProjectionY("timeBin", bin, bin);
416 
417  if (h->GetEntries() > 0) {
418 
419  TF1 f("f", "[0]*exp(-0.5*(x-[1])*(x-[1])/([2]*[2]))/(TMath::Sqrt(2*TMath::Pi())*[2]) + [3]");
420 
421  f.SetParameter(0, h->GetMaximum());
422  f.SetParameter(1, h->GetMean());
423  f.SetParameter(2, h->GetRMS() * 0.25);
424  f.SetParameter(3, h->GetMinimum());
425 
426  h->Fit(&f, "Q", "same");
427 
428  bg = f.GetParameter(3) * h->GetNbinsX();
429  sg = h->GetSumOfWeights() - bg;
430  er = f.GetParError(3) * h->GetNbinsX();
431 
432  }
433 
434  int i = bin - 1;
435 
436  out[0][i] = sg;
437  out[1][i] = bg;
438  out[2][i] = er;
439 
440  delete h;
441 
442  }
443 
444  return out;
445 
446  }
vector< vector< double > > bg_type
bg_type JSUPERNOVA::JL0BackgroundSimulator::generate_fitted ( int  rb = 1)
inline

Generate fitted L1 sample.

Definition at line 453 of file JL0BackgroundSimulator.hh.

453  {
454 
455  bg_type out;
456 
457  int k = rnd->Integer(rt.size());
458 
459  int limit = rt[k].size() - (genRatio * TWindow_ms);
460 
461  int start = rnd->Integer(limit);
462 
463  h2d_t* sample = build_H2D(k, start);
464 
465  if (rb != 1) { sample->RebinX(rb); }
466 
467  bg_type fit = fit_H2D(sample);
468 
469  out.push_back(fit[0]);
470 
471  delete sample;
472 
473  if (rb == 1) {
474  out.push_back(build_NC(k, start));
475  } else {
476  out.push_back(rebin(build_NC(k, start), rb));
477  }
478 
479  out.push_back(fit[1]);
480  out.push_back(fit[2]);
481 
482  return out;
483 
484  }
TH2F h2d_t
Definition: JRipple.hh:7
vector< vector< double > > bg_type
h2d_t * build_H2D(const int k, const int start)
Generate 2D histogram.
static bg_type fit_H2D(h2d_t *in)
Fit 2D sample.
vector< double > build_NC(const int k, const int start)
Build NC sequence.
static vector< double > rebin(const vector< double > &in, const int rb)
Rebin vector.

Member Data Documentation

vector<vector<double> > JSUPERNOVA::JL0BackgroundSimulator::rt
private

Definition at line 59 of file JL0BackgroundSimulator.hh.

vector<vector<double> > JSUPERNOVA::JL0BackgroundSimulator::nc
private

Definition at line 60 of file JL0BackgroundSimulator.hh.

vector<h2d_t*> JSUPERNOVA::JL0BackgroundSimulator::rt2d
private

Definition at line 62 of file JL0BackgroundSimulator.hh.

int JSUPERNOVA::JL0BackgroundSimulator::TWindow_ms
private

Definition at line 64 of file JL0BackgroundSimulator.hh.

int JSUPERNOVA::JL0BackgroundSimulator::genRatio
private

Definition at line 65 of file JL0BackgroundSimulator.hh.

int JSUPERNOVA::JL0BackgroundSimulator::outputSize
private

Definition at line 66 of file JL0BackgroundSimulator.hh.

double JSUPERNOVA::JL0BackgroundSimulator::genScale
private

Definition at line 69 of file JL0BackgroundSimulator.hh.

TRandom* JSUPERNOVA::JL0BackgroundSimulator::rnd
private

Definition at line 71 of file JL0BackgroundSimulator.hh.


The documentation for this class was generated from the following file: