Belle II Software  release-08-01-10
SVDCoGTimeCalibrationAlgorithm Class Reference

Class implementing SVDCoGTimeCalibration calibration algorithm. More...

#include <SVDCoGTimeCalibrationAlgorithm.h>

Inheritance diagram for SVDCoGTimeCalibrationAlgorithm:
Collaboration diagram for SVDCoGTimeCalibrationAlgorithm:

Public Types

enum  EResult {
  c_OK ,
  c_Iterate ,
  c_NotEnoughData ,
  c_Failure ,
  c_Undefined
}
 The result of calibration. More...
 

Public Member Functions

 SVDCoGTimeCalibrationAlgorithm (const std::string &str)
 Constructor set the prefix to SVDTimeCalibrationCollector.
 
virtual ~SVDCoGTimeCalibrationAlgorithm ()
 Destructor.
 
void setAllowedTimeShift (float value)
 Setter for m_allowedT0Shift.
 
float getAllowedTimeShift ()
 Getter for m_allowedT0Shift.
 
void setMinEntries (int minEntries)
 Set the minimum entries required in the histograms.
 
int getMinEntries ()
 Get the minimum entries required in the histograms.
 
std::string getPrefix () const
 Get the prefix used for getting calibration data.
 
bool checkPyExpRun (PyObject *pyObj)
 Checks that a PyObject can be successfully converted to an ExpRun type. More...
 
Calibration::ExpRun convertPyExpRun (PyObject *pyObj)
 Performs the conversion of PyObject to ExpRun. More...
 
std::string getCollectorName () const
 Alias for prefix. More...
 
void setPrefix (const std::string &prefix)
 Set the prefix used to identify datastore objects.
 
void setInputFileNames (PyObject *inputFileNames)
 Set the input file names used for this algorithm from a Python list. More...
 
PyObject * getInputFileNames ()
 Get the input file names used for this algorithm and pass them out as a Python list of unicode strings.
 
std::vector< Calibration::ExpRun > getRunListFromAllData () const
 Get the complete list of runs from inspection of collected data.
 
RunRange getRunRangeFromAllData () const
 Get the complete RunRange from inspection of collected data.
 
IntervalOfValidity getIovFromAllData () const
 Get the complete IoV from inspection of collected data.
 
void fillRunToInputFilesMap ()
 Fill the mapping of ExpRun -> Files.
 
std::string getGranularity () const
 Get the granularity of collected data.
 
EResult execute (std::vector< Calibration::ExpRun > runs={}, int iteration=0, IntervalOfValidity iov=IntervalOfValidity())
 Runs calibration over vector of runs for a given iteration. More...
 
EResult execute (PyObject *runs, int iteration=0, IntervalOfValidity iov=IntervalOfValidity())
 Runs calibration over Python list of runs. Converts to C++ and then calls the other execute() function.
 
std::list< Database::DBImportQuery > & getPayloads ()
 Get constants (in TObjects) for database update from last execution.
 
std::list< Database::DBImportQuerygetPayloadValues ()
 Get constants (in TObjects) for database update from last execution but passed by VALUE.
 
bool commit ()
 Submit constants from last calibration into database.
 
bool commit (std::list< Database::DBImportQuery > payloads)
 Submit constants from a (potentially previous) set of payloads.
 
const std::string & getDescription () const
 Get the description of the algoithm (set by developers in constructor)
 
bool loadInputJson (const std::string &jsonString)
 Load the m_inputJson variable from a string (useful from Python interface). The rturn bool indicates success or failure.
 
const std::string dumpOutputJson () const
 Dump the JSON string of the output JSON object.
 
const std::vector< Calibration::ExpRun > findPayloadBoundaries (std::vector< Calibration::ExpRun > runs, int iteration=0)
 Used to discover the ExpRun boundaries that you want the Python CAF to execute on. This is optional and only used in some.
 
template<>
std::shared_ptr< TTree > getObjectPtr (const std::string &name, const std::vector< Calibration::ExpRun > &requestedRuns)
 Specialization of getObjectPtr<TTree>.
 

Protected Member Functions

virtual EResult calibrate () override
 Run algo on data. More...
 
virtual bool isBoundaryRequired (const Calibration::ExpRun &currentRun) override
 If the event T0 changes significantly return true. More...
 
virtual void boundaryFindingSetup (std::vector< Calibration::ExpRun >, int) override
 setup of the boundary finding
 
void setInputFileNames (std::vector< std::string > inputFileNames)
 Set the input file names used for this algorithm. More...
 
virtual void boundaryFindingTearDown ()
 Put your algorithm back into a state ready for normal execution if you need to.
 
const std::vector< Calibration::ExpRun > & getRunList () const
 Get the list of runs for which calibration is called.
 
int getIteration () const
 Get current iteration.
 
std::vector< std::string > getVecInputFileNames () const
 Get the input file names used for this algorithm as a STL vector.
 
template<class T >
std::shared_ptr< T > getObjectPtr (const std::string &name, const std::vector< Calibration::ExpRun > &requestedRuns)
 Get calibration data object by name and list of runs, the Merge function will be called to generate the overall object.
 
template<class T >
std::shared_ptr< T > getObjectPtr (std::string name)
 Get calibration data object (for all runs the calibration is requested for) This function will only work during or after execute() has been called once.
 
template<>
shared_ptr< TTree > getObjectPtr (const string &name, const vector< ExpRun > &requestedRuns)
 We cheekily cast the TChain to TTree for the returned pointer so that the user never knows Hopefully this doesn't cause issues if people do low level stuff to the tree...
 
std::string getGranularityFromData () const
 Get the granularity of collected data.
 
void saveCalibration (TClonesArray *data, const std::string &name)
 Store DBArray payload with given name with default IOV.
 
void saveCalibration (TClonesArray *data, const std::string &name, const IntervalOfValidity &iov)
 Store DBArray with given name and custom IOV.
 
void saveCalibration (TObject *data)
 Store DB payload with default name and default IOV.
 
void saveCalibration (TObject *data, const IntervalOfValidity &iov)
 Store DB payload with default name and custom IOV.
 
void saveCalibration (TObject *data, const std::string &name)
 Store DB payload with given name with default IOV.
 
void saveCalibration (TObject *data, const std::string &name, const IntervalOfValidity &iov)
 Store DB payload with given name and custom IOV.
 
void updateDBObjPtrs (const unsigned int event, const int run, const int experiment)
 Updates any DBObjPtrs by calling update(event) for DBStore.
 
void setDescription (const std::string &description)
 Set algorithm description (in constructor)
 
void clearCalibrationData ()
 Clear calibration data.
 
Calibration::ExpRun getAllGranularityExpRun () const
 Returns the Exp,Run pair that means 'Everything'. Currently unused.
 
void resetInputJson ()
 Clears the m_inputJson member variable.
 
void resetOutputJson ()
 Clears the m_outputJson member variable.
 
template<class T >
void setOutputJsonValue (const std::string &key, const T &value)
 Set a key:value pair for the outputJson object, expected to used interally during calibrate()
 
template<class T >
const T getOutputJsonValue (const std::string &key) const
 Get a value using a key from the JSON output object, not sure why you would want to do this.
 
template<class T >
const T getInputJsonValue (const std::string &key) const
 Get an input JSON value using a key. The normal exceptions are raised when the key doesn't exist.
 
const nlohmann::json & getInputJsonObject () const
 Get the entire top level JSON object. We explicitly say this must be of object type so that we might pick.
 
bool inputJsonKeyExists (const std::string &key) const
 Test for a key in the input JSON object.
 

Protected Attributes

std::vector< Calibration::ExpRun > m_boundaries
 When using the boundaries functionality from isBoundaryRequired, this is used to store the boundaries. It is cleared when.
 

Private Member Functions

std::string getExpRunString (Calibration::ExpRun &expRun) const
 Gets the "exp.run" string repr. of (exp,run)
 
std::string getFullObjectPath (const std::string &name, Calibration::ExpRun expRun) const
 constructs the full TDirectory + Key name of an object in a TFile based on its name and exprun
 

Private Attributes

std::string m_id = ""
 Parameter given to set the UniqueID of the payload.
 
std::optional< float > m_previousRawTimeMeanL3V
 CoG time mean of the previous run for V side of layer 3.
 
float m_allowedTimeShift = 2.
 Allowed EventT0 shift.
 
float m_minEntries = 10000
 Set the minimun number of entries required in the histograms of layer 3.
 
std::vector< std::string > m_inputFileNames
 List of input files to the Algorithm, will initially be user defined but then gets the wildcards expanded during execute()
 
std::map< Calibration::ExpRun, std::vector< std::string > > m_runsToInputFiles
 Map of Runs to input files. Gets filled when you call getRunRangeFromAllData, gets cleared when setting input files again.
 
std::string m_granularityOfData
 Granularity of input data. This only changes when the input files change so it isn't specific to an execution.
 
ExecutionData m_data
 Data specific to a SINGLE execution of the algorithm. Gets reset at the beginning of execution.
 
std::string m_description {""}
 Description of the algorithm.
 
std::string m_prefix {""}
 The name of the TDirectory the collector objects are contained within.
 
nlohmann::json m_jsonExecutionInput = nlohmann::json::object()
 Optional input JSON object used to make decisions about how to execute the algorithm code.
 
nlohmann::json m_jsonExecutionOutput = nlohmann::json::object()
 Optional output JSON object that can be set during the execution by the underlying algorithm code.
 

Static Private Attributes

static const Calibration::ExpRun m_allExpRun = make_pair(-1, -1)
 allExpRun
 

Detailed Description

Class implementing SVDCoGTimeCalibration calibration algorithm.

Definition at line 23 of file SVDCoGTimeCalibrationAlgorithm.h.

Member Enumeration Documentation

◆ EResult

enum EResult
inherited

The result of calibration.

Enumerator
c_OK 

Finished successfuly =0 in Python.

c_Iterate 

Needs iteration =1 in Python.

c_NotEnoughData 

Needs more data =2 in Python.

c_Failure 

Failed =3 in Python.

c_Undefined 

Not yet known (before execution) =4 in Python.

Definition at line 40 of file CalibrationAlgorithm.h.

Member Function Documentation

◆ calibrate()

CalibrationAlgorithm::EResult calibrate ( )
overrideprotectedvirtual

Run algo on data.

Fit with pol1

Implements CalibrationAlgorithm.

Definition at line 33 of file SVDCoGTimeCalibrationAlgorithm.cc.

34 {
35 
36  gROOT->SetBatch(true);
37 
38  auto timeCal = new Belle2::SVDCoGCalibrationFunction();
39  auto payload = new Belle2::SVDCoGTimeCalibrations::t_payload(*timeCal, m_id);
40 
41  std::unique_ptr<TF1> pol1(new TF1("pol1", "[0] + [1]*x", -10, 80));
42  pol1->SetParameters(-40, 0.9);
43  std::unique_ptr<TF1> pol3(new TF1("pol3", "[0] + [1]*x + [2]*x*x + [3]*x*x*x", -10, 80));
44  pol3->SetParLimits(0, -200, 0);
45  pol3->SetParLimits(1, 0, 10);
46  pol3->SetParLimits(2, -1, 0);
47  pol3->SetParLimits(3, 0, 1);
48  std::unique_ptr<TF1> pol5(new TF1("pol5", "[0] + [1]*x + [2]*x*x + [3]*x*x*x + [4]*x*x*x*x + [5]*x*x*x*x*x", -100, 100));
49  pol5->SetParameters(-50, 1.5, 0.01, 0.0001, 0.00001, 0.000001);
50 
51  FileStat_t info;
52  int cal_rev = 1;
53  while (gSystem->GetPathInfo(Form("algorithm_6SampleCoG_output_rev_%d.root", cal_rev), info) == 0)
54  cal_rev++;
55  std::unique_ptr<TFile> f(new TFile(Form("algorithm_6SampleCoG_output_rev_%d.root", cal_rev), "RECREATE"));
56 
57  auto m_tree = new TTree(Form("rev_%d", cal_rev), "RECREATE");
58  int layer_num, ladder_num, sensor_num, view, ndf;
59  float a, b, c, d, a_err, b_err, c_err, d_err, chi2, p;
60  m_tree->Branch("layer", &layer_num, "layer/I");
61  m_tree->Branch("ladder", &ladder_num, "ladder/I");
62  m_tree->Branch("sensor", &sensor_num, "sensor/I");
63  m_tree->Branch("isU", &view, "isU/I");
64  m_tree->Branch("a", &a, "a/F");
65  m_tree->Branch("b", &b, "b/F");
66  m_tree->Branch("c", &c, "c/F");
67  m_tree->Branch("d", &d, "d/F");
68  m_tree->Branch("a_err", &a_err, "a_err/F");
69  m_tree->Branch("b_err", &b_err, "b_err/F");
70  m_tree->Branch("c_err", &c_err, "c_err/F");
71  m_tree->Branch("d_err", &d_err, "d_err/F");
72  m_tree->Branch("chi2", &chi2, "chi2/F");
73  m_tree->Branch("ndf", &ndf, "ndf/I");
74  m_tree->Branch("p", &p, "p/F");
75 
76  auto __hEventT0vsCoG__ = getObjectPtr<TH3F>("__hEventT0vsCoG__");
77  auto __hEventT0__ = getObjectPtr<TH2F>("__hEventT0__");
78  auto __hEventT0NoSync__ = getObjectPtr<TH2F>("__hEventT0NoSync__");
79  auto __hBinToSensorMap__ = getObjectPtr<TH1F>("__hBinToSensorMap__");
80 
81  for (int ij = 0; ij < (__hBinToSensorMap__->GetNbinsX()); ij++) {
82  {
83  {
84  {
85 
86  auto binLabel = __hBinToSensorMap__->GetXaxis()->GetBinLabel(ij + 1);
87  char side;
88  std::sscanf(binLabel, "L%dL%dS%d%c", &layer_num, &ladder_num, &sensor_num, &side);
89  view = 0;
90  if (side == 'U')
91  view = 1;
92 
93  B2INFO("Projecting for Sensor: " << binLabel << " with Bin Number: " << ij + 1);
94 
95  __hEventT0vsCoG__->GetZaxis()->SetRange(ij + 1, ij + 1);
96  auto hEventT0vsCoG = (TH2D*)__hEventT0vsCoG__->Project3D("yxe");
97  auto hEventT0 = (TH1D*)__hEventT0__->ProjectionX("hEventT0_tmp", ij + 1, ij + 1);
98  auto hEventT0nosync = (TH1D*)__hEventT0NoSync__->ProjectionX("hEventT0NoSync_tmp", ij + 1, ij + 1);
99 
100  hEventT0vsCoG->SetName(Form("eventT0vsCoG__L%dL%dS%d%c", layer_num, ladder_num, sensor_num, side));
101  hEventT0->SetName(Form("eventT0__L%dL%dS%d%c", layer_num, ladder_num, sensor_num, side));
102  hEventT0nosync->SetName(Form("eventT0nosync__L%dL%dS%d%c", layer_num, ladder_num, sensor_num, side));
103 
104  char sidePN = (side == 'U' ? 'P' : 'N');
105  hEventT0vsCoG->SetTitle(Form("EventT0Sync vs rawTime in %d.%d.%d %c/%c", layer_num, ladder_num, sensor_num, side, sidePN));
106  hEventT0->SetTitle(Form("EventT0Sync in %d.%d.%d %c/%c", layer_num, ladder_num, sensor_num, side, sidePN));
107  hEventT0nosync->SetTitle(Form("EventT0NoSync in %d.%d.%d %c/%c", layer_num, ladder_num, sensor_num, side, sidePN));
108 
109  hEventT0vsCoG->SetDirectory(0);
110  hEventT0->SetDirectory(0);
111  hEventT0nosync->SetDirectory(0);
112 
113  B2INFO("Histogram: " << hEventT0vsCoG->GetName() <<
114  " Entries (n. clusters): " << hEventT0vsCoG->GetEntries());
115  if (layer_num == 3 && hEventT0vsCoG->GetEntries() < m_minEntries) {
116  B2INFO("Histogram: " << hEventT0vsCoG->GetName() <<
117  " Entries (n. clusters): " << hEventT0vsCoG->GetEntries() <<
118  " Entries required: " << m_minEntries);
119  B2WARNING("Not enough data, adding one run to the collector");
120  f->Close();
121  gSystem->Unlink(Form("algorithm_6SampleCoG_output_rev_%d.root", cal_rev));
122  return c_NotEnoughData;
123  }
124  if (layer_num != 3 && hEventT0vsCoG->GetEntries() < m_minEntries / 10) {
125  B2INFO("Histogram: " << hEventT0vsCoG->GetName() <<
126  " Entries (n. clusters): " << hEventT0vsCoG->GetEntries() <<
127  " Entries required: " << m_minEntries / 10);
128  B2WARNING("Not enough data, adding one run to the collector");
129  f->Close();
130  gSystem->Unlink(Form("algorithm_6SampleCoG_output_rev_%d.root", cal_rev));
131  return c_NotEnoughData;
132  }
133  for (int i = 1; i <= hEventT0vsCoG->GetNbinsX(); i++) {
134  for (int j = 1; j <= hEventT0vsCoG->GetNbinsY(); j++) {
135  if (hEventT0vsCoG->GetBinContent(i, j) < max(2, int(hEventT0vsCoG->GetEntries() * 0.001))) {
136  hEventT0vsCoG->SetBinContent(i, j, 0);
137  }
138  }
139  }
140  TProfile* pfx = hEventT0vsCoG->ProfileX();
141  std::string name = "pfx_" + std::string(hEventT0vsCoG->GetName());
142  pfx->SetName(name.c_str());
143  TFitResultPtr tfr = pfx->Fit("pol3", "RQSM");
144  double par[4];
145  pol3->GetParameters(par);
147  /*
148  pfx->Fit("pol1", "RQ");
149  double par[4];
150  pol1->GetParameters(par);
151  par[2] = 0;
152  par[3] = 0;
153  */
154  // double meanT0 = hEventT0->GetMean();
155  // double meanT0NoSync = hEventT0nosync->GetMean();
156  timeCal->set_current(1);
157  // timeCal->set_current(2);
158  timeCal->set_pol3parameters(par[0], par[1], par[2], par[3]);
159  payload->set(layer_num, ladder_num, sensor_num, bool(view), 1, *timeCal);
160  f->cd();
161  hEventT0->Write();
162  hEventT0vsCoG->Write();
163  hEventT0nosync->Write();
164  pfx->Write();
165 
166  delete pfx;
167  delete hEventT0vsCoG;
168  delete hEventT0;
169  delete hEventT0nosync;
170 
171  if (tfr.Get() == nullptr || (tfr->Status() != 0 && tfr->Status() != 4 && tfr->Status() != 4000)) {
172  f->Close();
173  B2FATAL("Fit to the histogram failed in SVDCoGTimeCalibrationAlgorithm. "
174  << "Check the 2-D histogram to clarify the reason.");
175  } else {
176  a = par[0]; b = par[1]; c = par[2]; d = par[3];
177  a_err = tfr->ParError(0); b_err = tfr->ParError(1); c_err = tfr->ParError(2); d_err = tfr->ParError(3);
178  chi2 = tfr->Chi2();
179  ndf = tfr->Ndf();
180  p = tfr->Prob();
181  m_tree->Fill();
182  }
183  }
184  }
185  }
186  }
187  m_tree->Write();
188  f->Close();
189  saveCalibration(payload, "SVDCoGTimeCalibrations");
190 
191  //delete f;
192 
193  // probably not needed - would trigger re-doing the collection
194  // if ( ... too large corrections ... ) return c_Iterate;
195  return c_OK;
196 }
void saveCalibration(TClonesArray *data, const std::string &name)
Store DBArray payload with given name with default IOV.
@ c_OK
Finished successfuly =0 in Python.
@ c_NotEnoughData
Needs more data =2 in Python.
class to contain the CoG Time calibrations
std::string m_id
Parameter given to set the UniqueID of the payload.
float m_minEntries
Set the minimun number of entries required in the histograms of layer 3.
SVDCalibrationsBase< SVDCalibrationsScalar< SVDCoGCalibrationFunction > > t_payload
typedef for the SVDCoGCalibrationFunction payload of all SVD sensors

◆ checkPyExpRun()

bool checkPyExpRun ( PyObject *  pyObj)
inherited

Checks that a PyObject can be successfully converted to an ExpRun type.

Checks if the PyObject can be converted to ExpRun.

Definition at line 28 of file CalibrationAlgorithm.cc.

◆ convertPyExpRun()

ExpRun convertPyExpRun ( PyObject *  pyObj)
inherited

Performs the conversion of PyObject to ExpRun.

Converts the PyObject to an ExpRun. We've preoviously checked the object so this assumes a lot about the PyObject.

Definition at line 70 of file CalibrationAlgorithm.cc.

◆ execute()

CalibrationAlgorithm::EResult execute ( std::vector< Calibration::ExpRun >  runs = {},
int  iteration = 0,
IntervalOfValidity  iov = IntervalOfValidity() 
)
inherited

Runs calibration over vector of runs for a given iteration.

You can also specify the IoV to save the database payload as. By default the Algorithm will create an IoV from your requested ExpRuns, or from the overall ExpRuns of the input data if you haven't specified ExpRuns in this function.

No checks are performed to make sure that a IoV you specify matches the data you ran over, it simply labels the IoV to commit to the database later.

Definition at line 114 of file CalibrationAlgorithm.cc.

◆ getCollectorName()

std::string getCollectorName ( ) const
inlineinherited

Alias for prefix.

For convenience and less writing, we say developers to set this to default collector module name in constructor of base class. One can however use the dublets of collector+algorithm multiple times with different settings. To bind these together correctly, the prefix has to be set the same for algo and collector. So we call the setter setPrefix rather than setModuleName or whatever. This getter will work out of the box for default cases -> return the name of module you have to add to your path to collect data for this algorihtm.

Definition at line 164 of file CalibrationAlgorithm.h.

◆ isBoundaryRequired()

bool isBoundaryRequired ( const Calibration::ExpRun &  currentRun)
overrideprotectedvirtual

If the event T0 changes significantly return true.

This is run inside the findPayloadBoundaries member function in the base class.

Reimplemented from CalibrationAlgorithm.

Definition at line 198 of file SVDCoGTimeCalibrationAlgorithm.cc.

◆ setInputFileNames() [1/2]

void setInputFileNames ( PyObject *  inputFileNames)
inherited

Set the input file names used for this algorithm from a Python list.

Set the input file names used for this algorithm and resolve the wildcards.

Definition at line 166 of file CalibrationAlgorithm.cc.

◆ setInputFileNames() [2/2]

void setInputFileNames ( std::vector< std::string >  inputFileNames)
protectedinherited

Set the input file names used for this algorithm.

Set the input file names used for this algorithm and resolve the wildcards.

Definition at line 194 of file CalibrationAlgorithm.cc.


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