Skip to content

Commit

Permalink
Merge branch 'master' into upgrade_recovery
Browse files Browse the repository at this point in the history
  • Loading branch information
Anakintana authored Mar 8, 2023
2 parents 75dc330 + 6845aa7 commit a93f2e6
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 114 deletions.
6 changes: 2 additions & 4 deletions .github/pr-badge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,5 @@
message: "Ok: $additions"
color: "green"
when: "$additions < 100"
- imageUrl: "https://gitlab.cern.ch/rest-for-physics/detectorlib/badges/$branchName/pipeline.svg"
url: "https://gitlab.cern.ch/rest-for-physics/detectorlib/-/commits/$branchName"
- imageUrl: "https://gitlab.cern.ch/rest-for-physics/framework/badges/$branchName/pipeline.svg"
url: "https://gitlab.cern.ch/rest-for-physics/framework/-/commits/$branchName"
- imageUrl: "https://github.com/rest-for-physics/detectorlib/actions/workflows/validation.yml/badge.svg?branch=$branchName"
url: "https://github.com/rest-for-physics/detectorlib/commits/$branchName"
Binary file added images/smear.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
71 changes: 42 additions & 29 deletions inc/TRestDetectorHitsSmearingProcess.h
Original file line number Diff line number Diff line change
@@ -1,53 +1,63 @@
///______________________________________________________________________________
///______________________________________________________________________________
///______________________________________________________________________________
///
///
/// RESTSoft : Software for Rare Event Searches with TPCs
///
/// TRestDetectorHitsSmearingProcess.h
///
/// feb 2016: First concept
/// Created as part of the conceptualization of existing REST
/// software.
/// Javier G. Garza
///_______________________________________________________________________________
/*************************************************************************
* This file is part of the REST software framework. *
* *
* Copyright (C) 2016 GIFNA/TREX (University of Zaragoza) *
* For more information see https://gifna.unizar.es/trex *
* *
* REST is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* REST is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have a copy of the GNU General Public License along with *
* REST in $REST_PATH/LICENSE. *
* If not, see https://www.gnu.org/licenses/. *
* For the list of contributors see $REST_PATH/CREDITS. *
*************************************************************************/

#ifndef RestCore_TRestDetectorHitsSmearingProcess
#define RestCore_TRestDetectorHitsSmearingProcess

#include <TRandom3.h>
#include <TRestEventProcess.h>

#include "TRestDetectorGas.h"
#include "TRestDetectorHitsEvent.h"

/// A process to include detector energy resolution in a TRestDetectorHitsEvent
class TRestDetectorHitsSmearingProcess : public TRestEventProcess {
private:
TRestDetectorHitsEvent* fHitsInputEvent; //!
TRestDetectorHitsEvent* fHitsOutputEvent; //!
/// A pointer to the process input event
TRestDetectorHitsEvent* fInputEvent = nullptr; //!

TRandom3* fRandom; //!
/// A pointer to the process output event
TRestDetectorHitsEvent* fOutputEvent = nullptr; //!

TRestDetectorGas* fGas; //!
/// A pointer to the random generator initializes with fSeed
TRandom3* fRandom = nullptr; //!

void InitFromConfigFile() override;
void Initialize() override;
void LoadDefaultConfig();
void InitProcess() override;

protected:
// add here the members of your event process
/// Reference energy for the FWHM
Double_t fEnergyRef = 5.9; //<

/// FWHM at Energy of reference
Double_t fResolutionAtERef = 15; //<

Double_t fEnergyRef; ///< reference energy for the FWHM
Double_t fResolutionAtERef; ///< FWHM at Energy of reference
/// The seed to be used for the random generator
ULong_t fSeed = 0; //<

public:
any GetInputEvent() const override { return fHitsInputEvent; }
any GetOutputEvent() const override { return fHitsOutputEvent; }
any GetInputEvent() const override { return fInputEvent; }
any GetOutputEvent() const override { return fOutputEvent; }

void InitProcess() override;
TRestEvent* ProcessEvent(TRestEvent* inputEvent) override;
void EndProcess() override;

void LoadConfig(const std::string& configFilename, const std::string& name = "");

Expand All @@ -64,14 +74,17 @@ class TRestDetectorHitsSmearingProcess : public TRestEventProcess {

const char* GetProcessName() const override { return "smearingProcess"; }

/// Returns the reference energy where the FWHM is defined
inline Double_t GetEnergyReference() const { return fEnergyRef; }

/// Returns the energy resolution in %FWHM for the reference energy
inline Double_t GetResolutionReference() const { return fResolutionAtERef; }

TRestDetectorHitsSmearingProcess();
TRestDetectorHitsSmearingProcess(const char* configFilename);

~TRestDetectorHitsSmearingProcess();

ClassDefOverride(TRestDetectorHitsSmearingProcess, 2);
ClassDefOverride(TRestDetectorHitsSmearingProcess, 3);
};
#endif
2 changes: 1 addition & 1 deletion src/TRestDetectorHitsEvent.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ TPad* TRestDetectorHitsEvent::DrawEvent(const TString& option) {

/// The default histogram using a pitch of 0 mm,
// which means that it should be extracted from the hit array
if (optList.size() == 0) optList.push_back("hist(Cont1,col)");
if (optList.size() == 0) optList.push_back("hist(Cont1,col)[3]");

if (fPad != nullptr) {
delete fPad;
Expand Down
168 changes: 88 additions & 80 deletions src/TRestDetectorHitsSmearingProcess.cxx
Original file line number Diff line number Diff line change
@@ -1,113 +1,121 @@
///______________________________________________________________________________
///______________________________________________________________________________
///______________________________________________________________________________
/*************************************************************************
* This file is part of the REST software framework. *
* *
* Copyright (C) 2016 GIFNA/TREX (University of Zaragoza) *
* For more information see http://gifna.unizar.es/trex *
* *
* REST is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* REST is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have a copy of the GNU General Public License along with *
* REST in $REST_PATH/LICENSE. *
* If not, see http://www.gnu.org/licenses/. *
* For the list of contributors see $REST_PATH/CREDITS. *
*************************************************************************/

//////////////////////////////////////////////////////////////////////////
/// TRestDetectorHitsSmearingProcess will introduce a stochastic smearing on the
/// energy of the hits for each event. The total energy of each event will be
/// re-distributed following a gaussian shaped function.
///
/// The energy resolution will follow a root square law with the energy
/// using the `energyReference` and the `resolutionReference` as a pivoting
/// point.
///
/// RESTSoft : Software for Rare Event Searches with TPCs
/// Therefore, the process defines only two parameters, one being the energy
/// at wich the energy resolution is "known", and the FWHM in percentage at
/// that given energy, `resolutionReference`.
///
/// TRestDetectorHitsSmearingProcess.cxx
/// \code
/// <addProcess type="TRestDetectorHitsSmearingProcess" name="smear"
/// title="Energy resolution smearing">
/// <parameter name="energyReference" value="250keV" />
/// <parameter name="resolutionReference" value="15" />
/// </addProcess>
/// \endcode
///
/// Template to use to design "event process" classes inherited from
/// TRestDetectorHitsSmearingProcess
/// How to use: replace TRestDetectorHitsSmearingProcess by your name,
/// fill the required functions following instructions and add all
/// needed additional members and funcionality
/// The following image is the result of smearing the energy of an electron
/// gun of 250keV. It was produced using the exercise 3.2 inside the
/// [day3/session2](https://github.com/rest-for-physics/rest-school/tree/master/day3/session2)
/// at the rest-school repository.
///
/// \htmlonly <style>div.image img[src="smear.png"]{width:500px;}</style> \endhtmlonly
/// ![The result of applying a 15% energy resolution at 250keV](smear.png)
///
///--------------------------------------------------------------------------
///
/// RESTsoft - Software for Rare Event Searches with TPCs
///
/// History of developments:
///
/// 2016-February: First implementation of TRestDetectorHitsSmearingProcess
/// \author Javier G. Garza
///
/// 2023-January: This class has been documented
/// \author Javier Galan - javier.galan@unizar.es
///
/// \class TRestDetectorHitsSmearingProcess
///
/// <hr>
///
/// feb 2016: First concept
/// Created as part of the conceptualization of existing REST
/// software.
/// Javier G. Garza
///_______________________________________________________________________________

#include "TRestDetectorHitsSmearingProcess.h"

using namespace std;

ClassImp(TRestDetectorHitsSmearingProcess);

///////////////////////////////////////////////
/// \brief Default constructor
///
TRestDetectorHitsSmearingProcess::TRestDetectorHitsSmearingProcess() { Initialize(); }

TRestDetectorHitsSmearingProcess::TRestDetectorHitsSmearingProcess(const char* configFilename) {
Initialize();

if (LoadConfigFromFile(configFilename)) {
LoadDefaultConfig();
}

PrintMetadata();
}

///////////////////////////////////////////////
/// \brief Default destructor
///
TRestDetectorHitsSmearingProcess::~TRestDetectorHitsSmearingProcess() {
delete fHitsOutputEvent;
delete fOutputEvent;
// TRestDetectorHitsSmearingProcess destructor
}

void TRestDetectorHitsSmearingProcess::LoadDefaultConfig() {
SetTitle("Default config");

fEnergyRef = 5.9;
fResolutionAtERef = 15.0;
}

///////////////////////////////////////////////
/// \brief Function to initialize input/output event members and define the section name
///
void TRestDetectorHitsSmearingProcess::Initialize() {
SetSectionName(this->ClassName());
SetLibraryVersion(LIBRARY_VERSION);

fEnergyRef = 5.9;
fResolutionAtERef = 15.0;

fHitsInputEvent = nullptr;
fHitsOutputEvent = new TRestDetectorHitsEvent();

fRandom = nullptr;
}

void TRestDetectorHitsSmearingProcess::LoadConfig(const string& configFilename, const string& name) {
if (LoadConfigFromFile(configFilename, name)) {
LoadDefaultConfig();
}

PrintMetadata();

fGas = GetMetadata<TRestDetectorGas>();
fInputEvent = nullptr;
fOutputEvent = new TRestDetectorHitsEvent();
}

void TRestDetectorHitsSmearingProcess::InitProcess() {
// Function to be executed once at the beginning of process
// (before starting the process of the events)

// Start by calling the InitProcess function of the abstract class.
// Comment this if you don't want it.
// TRestEventProcess::InitProcess();
if (fRandom != nullptr) delete fRandom;
fRandom = new TRandom3(fSeed);
fSeed = fRandom->TRandom::GetSeed();
}

///////////////////////////////////////////////
/// \brief The main processing event function
///
TRestEvent* TRestDetectorHitsSmearingProcess::ProcessEvent(TRestEvent* inputEvent) {
fHitsInputEvent = (TRestDetectorHitsEvent*)inputEvent;
fHitsOutputEvent->SetEventInfo(fHitsInputEvent);
fInputEvent = (TRestDetectorHitsEvent*)inputEvent;
fOutputEvent->SetEventInfo(fInputEvent);

Double_t eDep = fHitsInputEvent->GetTotalEnergy();
Double_t eDep = fInputEvent->GetTotalEnergy();
Double_t eRes = fResolutionAtERef * TMath::Sqrt(fEnergyRef / eDep) / 2.35 / 100.0;

Double_t gain = fRandom->Gaus(1.0, eRes);
for (unsigned int hit = 0; hit < fHitsInputEvent->GetNumberOfHits(); hit++)
fHitsOutputEvent->AddHit(fHitsInputEvent->GetX(hit), fHitsInputEvent->GetY(hit),
fHitsInputEvent->GetZ(hit), fHitsInputEvent->GetEnergy(hit) * gain,
fHitsInputEvent->GetTime(hit), fHitsInputEvent->GetType(hit));

return fHitsOutputEvent;
}

void TRestDetectorHitsSmearingProcess::EndProcess() {
// Function to be executed once at the end of the process
// (after all events have been processed)

// Start by calling the EndProcess function of the abstract class.
// Comment this if you don't want it.
// TRestEventProcess::EndProcess();
}
for (unsigned int hit = 0; hit < fInputEvent->GetNumberOfHits(); hit++)
fOutputEvent->AddHit(fInputEvent->GetX(hit), fInputEvent->GetY(hit), fInputEvent->GetZ(hit),
fInputEvent->GetEnergy(hit) * gain, fInputEvent->GetTime(hit),
fInputEvent->GetType(hit));

void TRestDetectorHitsSmearingProcess::InitFromConfigFile() {
fEnergyRef = GetDblParameterWithUnits("energyReference");
fResolutionAtERef = StringToDouble(GetParameter("resolutionReference"));
fRandom = new TRandom3(StringToDouble(GetParameter("seed", "0")));
return fOutputEvent;
}

0 comments on commit a93f2e6

Please sign in to comment.