offsetLine.h
Go to the documentation of this file.
1 // offsetLine.h
2 //
3 // David Adams
4 // June 2018
5 //
6 // The offsetLine function is two line segments with equal slopes
7 // and equal-magnitude, opposite-sign x-offsets on either side of
8 // the origin. The function is continuous for offset >= 0 and has
9 // a discontinuity at x=0 for offset < 0.
10 //
11 // The offsetLinePed function adds a pedestal to offsetLIne.
12 //
13 // The function offsetLineFull adds NegScale, a scale factor for the
14 // slope for values with x < 0.
15 //
16 // The function offsetLineShifted adds Shift: values for x > 0
17 // (x < 0) are shifted up (down) by this amount.
18 
19 #ifndef offsetLine_H
20 #define offsetLine_H
21 
22 #include <string>
23 #include "TF1.h"
24 
25 inline
26 double offsetLineShifted(double* px, double* ppar) {
27  double xoff = ppar[0];
28  double scal = ppar[1];
29  double yped = ppar[2];
30  double sneg = ppar[3];
31  double shif = ppar[4];
32  double x = px[0];
33  double gain = scal;
34  if ( x > 0.0 ) yped += shif;
35  if ( x < 0.0 ) yped -= shif;
36  if ( x < 0.0 ) gain *= sneg;
37  if ( xoff > 0.0 ) {
38  if ( x > xoff ) return yped + gain*(x-xoff);
39  if ( x < -xoff ) return yped + gain*(x+xoff);
40  } else {
41  double y0 = -gain*xoff;
42  if ( x > 0.0 ) return yped + y0 + gain*x;
43  else if ( x < 0.0 ) return yped - y0 + gain*x;
44  }
45  return yped;
46 }
47 
48 inline
49 double offsetLineFull(double* px, double* ppar) {
50  double xoff = ppar[0];
51  double scal = ppar[1];
52  double yped = ppar[2];
53  double sneg = ppar[3];
54  double x = px[0];
55  double gain = scal;
56  if ( x < 0.0 ) gain *= sneg;
57  if ( xoff > 0.0 ) {
58  if ( x > xoff ) return yped + gain*(x-xoff);
59  if ( x < -xoff ) return yped + gain*(x+xoff);
60  } else {
61  double y0 = -gain*xoff;
62  if ( x > 0.0 ) return yped + y0 + gain*x;
63  else if ( x < 0.0 ) return yped - y0 + gain*x;
64  }
65  return yped;
66 }
67 
68 inline
69 double offsetLine(double* px, double* ppar) {
70  double xoff = ppar[0];
71  double gain = ppar[1];
72  double x = px[0];
73  if ( xoff > 0.0 ) {
74  if ( x > xoff ) return gain*(x-xoff);
75  if ( x < -xoff ) return gain*(x+xoff);
76  } else {
77  double y0 = -gain*xoff;
78  if ( x > 0.0 ) return y0 + gain*x;
79  else if ( x < 0.0 ) return -y0 + gain*x;
80  }
81  return 0.0;
82 }
83 
84 inline
85 double offsetLinePed(double* px, double* ppar) {
86  double yped = ppar[2];
87  return yped + offsetLine(px, ppar);
88 }
89 
90 inline
91 TF1* offsetLineTF1(double off =0.0, double slope =1.0,
92  double xmin =-10.0, double xmax =10.0,
93  std::string fname ="offsetLine") {
94  TF1* pf = new TF1(fname.c_str(), offsetLine, xmin, xmax, 2);
95  pf->SetParName(0, "Offset");
96  pf->SetParName(1, "Slope");
97  pf->SetParameter(0, off);
98  pf->SetParameter(1, slope);
99  return pf;
100 }
101 
102 inline
103 TF1* offsetLinePedTF1(double off =0.0, double slope =1.0, double ped =0.0,
104  double xmin =-10.0, double xmax =10.0,
105  std::string fname ="offsetLinePed") {
106  TF1* pf = new TF1(fname.c_str(), offsetLinePed, xmin, xmax, 3);
107  pf->SetParName(0, "Offset");
108  pf->SetParName(1, "Slope");
109  pf->SetParName(2, "Pedestal");
110  pf->SetParameter(0, off);
111  pf->SetParameter(1, slope);
112  pf->SetParameter(2, ped);
113  return pf;
114 }
115 
116 inline
117 TF1* offsetLineFullTF1(double off =0.0, double slope =1.0,
118  double ped =0.0, double sneg =1.0,
119  double xmin =-10.0, double xmax =10.0,
120  std::string fname ="offsetLinePed") {
121  TF1* pf = new TF1(fname.c_str(), offsetLineFull, xmin, xmax, 4);
122  pf->SetParName(0, "Offset");
123  pf->SetParName(1, "Slope");
124  pf->SetParName(2, "Pedestal");
125  pf->SetParName(3, "NegScale");
126  pf->SetParameter(0, off);
127  pf->SetParameter(1, slope);
128  pf->SetParameter(2, ped);
129  pf->SetParameter(3, sneg);
130  return pf;
131 }
132 
133 inline
134 TF1* offsetLineShiftedTF1(double off =0.0, double slope =1.0,
135  double ped =0.0, double sneg =1.0, double shif=0.0,
136  double xmin =-10.0, double xmax =10.0,
137  std::string fname ="offsetLineShifted") {
138  TF1* pf = new TF1(fname.c_str(), offsetLineShifted, xmin, xmax, 5);
139  pf->SetParName(0, "Offset");
140  pf->SetParName(1, "Slope");
141  pf->SetParName(2, "Pedestal");
142  pf->SetParName(3, "NegScale");
143  pf->SetParName(4, "Shift");
144  pf->SetParameter(0, off);
145  pf->SetParameter(1, slope);
146  pf->SetParameter(2, ped);
147  pf->SetParameter(3, sneg);
148  pf->SetParameter(4, shif);
149  return pf;
150 }
151 
152 #endif
TF1 * offsetLineTF1(double off=0.0, double slope=1.0, double xmin=-10.0, double xmax=10.0, std::string fname="offsetLine")
Definition: offsetLine.h:91
std::string string
Definition: nybbler.cc:12
TF1 * offsetLineShiftedTF1(double off=0.0, double slope=1.0, double ped=0.0, double sneg=1.0, double shif=0.0, double xmin=-10.0, double xmax=10.0, std::string fname="offsetLineShifted")
Definition: offsetLine.h:134
double offsetLineShifted(double *px, double *ppar)
Definition: offsetLine.h:26
double offsetLine(double *px, double *ppar)
Definition: offsetLine.h:69
TF1 * offsetLineFullTF1(double off=0.0, double slope=1.0, double ped=0.0, double sneg=1.0, double xmin=-10.0, double xmax=10.0, std::string fname="offsetLinePed")
Definition: offsetLine.h:117
list x
Definition: train.py:276
TF1 * offsetLinePedTF1(double off=0.0, double slope=1.0, double ped=0.0, double xmin=-10.0, double xmax=10.0, std::string fname="offsetLinePed")
Definition: offsetLine.h:103
double offsetLineFull(double *px, double *ppar)
Definition: offsetLine.h:49
double offsetLinePed(double *px, double *ppar)
Definition: offsetLine.h:85