Skip to content

Commit

Permalink
restoring ntupler to ops analisis and making it unversal
Browse files Browse the repository at this point in the history
  • Loading branch information
kkacprzak committed Feb 28, 2024
1 parent adb46d7 commit 89e61ad
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 188 deletions.
2 changes: 1 addition & 1 deletion ModularDetectorAnalysis/EventCategorizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ void EventCategorizer::initialiseCalibrationHistograms(bool includeTrento)
"Scin ID", "Time diffrence [ps]");

// Time walk histograms
double revToTLimit = 0.000000018;
double revToTLimit = 0.000000030;

getStatistics().createHistogramWithAxes(
new TH2D("time_walk_ab_tdiff", "AB TDiff vs. reversed ToT", 200, -fMaxTimeDiff / 2.0, fMaxTimeDiff / 2.0, 200, -revToTLimit, revToTLimit),
Expand Down
189 changes: 39 additions & 150 deletions opsAnalysis/NTupler.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @copyright Copyright 2016 The J-PET Framework Authors. All rights reserved.
* @copyright Copyright 2024 The J-PET Framework Authors. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may find a copy of the License in the LICENCE file.
Expand All @@ -13,123 +13,85 @@
* @file Ntupler.cpp
*/

#include <iostream>
#include "NTupler.h"
//#include "OPSAnalysisTools.h"
#include <JPetOptionsTools/JPetOptionsTools.h>
#include "../ModularDetectorAnalysis/HitFinderTools.h"
#include <JPetOptionsTools/JPetOptionsTools.h>
#include <iostream>

using namespace jpet_options_tools;
//using namespace ops_analysis_tools;
using namespace std;

NTupler::NTupler(const char* name): JPetUserTask(name) {}
NTupler::NTupler(const char* name) : JPetUserTask(name) {}

bool NTupler::init()
{
INFO("Started reduction of data to ntuples.");

fOutputEvents = new JPetTimeWindow("JPetEvent");

//if (isOptionSet(fParams.getOptions(), fThresholdValuesKey)){
//fThresholdValues = getOptionAsVectorOfInts(fParams.getOptions(), fThresholdValuesKey);

//INFO(Form("Loaded the following threshold values: (%d, %d, %d, %d)",
// fThresholdValues[0],
// fThresholdValues[1],
// fThresholdValues[2],
// fThresholdValues[3]
// ));

//}else{
// WARNING("TOT values were not provided by the user!");
// }

if (isOptionSet(fParams.getOptions(), fEventTimeParamKey)){
kEventTimeWindow = getOptionAsFloat(fParams.getOptions(), fEventTimeParamKey);
}else{
ERROR("Fine time window width not provided by the user!");
return false;
}

if(isOptionSet(fParams.getOptions(), "inputFile_std::string")){
fOutFileName = getOptionAsString(fParams.getOptions(), "inputFile_std::string");
if (isOptionSet(fParams.getOptions(), "inputFile_std::string"))
{
fOutFileName = getOptionAsString(fParams.getOptions(), "inputFile_std::string");
}

if(isOptionSet(fParams.getOptions(), "outputPath_std::string")){
fOutFilePath = getOptionAsString(fParams.getOptions(), "outputPath_std::string");
if (isOptionSet(fParams.getOptions(), "outputPath_std::string"))
{
fOutFilePath = getOptionAsString(fParams.getOptions(), "outputPath_std::string");
}

// initialize output file and tree
if(fOutFileName.find("unk.evt.root") != std::string::npos){
fOutFileName.replace(fOutFileName.find("unk.evt.root"), std::string::npos, "ntu.root");
if (fOutFileName.find("unk.evt.root") != std::string::npos)
{
fOutFileName.replace(fOutFileName.find("unk.evt.root"), std::string::npos, "ntu.root");
}

// handle special case if direct processing is used
//if(fOutFileName.find("hld.root") != std::string::npos){
//fOutFileName.replace(fOutFileName.find("hld.root"), std::string::npos, "ntu.root");
//}

if (!fOutFilePath.empty()) {
if (!fOutFilePath.empty())
{
size_t filename_pos = fOutFileName.find("dabc");
fOutFileName.replace(0, filename_pos - 1, fOutFilePath);
}

fOutFile = new TFile(fOutFileName.c_str(), "RECREATE");
fOutTree = new TTree("T", "o-Ps event candidates");
fOutTree = new TTree("T", "JPET Events");

fOutTree->Branch("nhits", &fNumberOfHits, "nhits/b");
fOutTree->Branch("times", &fHitTimes);
fOutTree->Branch("pos", &fHitPos);
fOutTree->Branch("tots_flat", &fHitTOTsFlat);
fOutTree->Branch("tots", &fHitTOTs);
fOutTree->Branch("tots", &fHitTOTs);
fOutTree->Branch("scins", &fHitScinIDs);


return true;
}

bool NTupler::exec()
{
if (auto timeWindow = dynamic_cast<const JPetTimeWindow* const>(fEvent)) {
{
if (auto timeWindow = dynamic_cast<const JPetTimeWindow* const>(fEvent))
{

int n_events = timeWindow->getNumberOfEvents();

for (int entry = 0; entry < n_events; ++entry) {
const JPetEvent &event =
dynamic_cast<const JPetEvent &>(timeWindow->operator[](entry));

const auto &hits = event.getHits();
fNumberOfHits = event.getHits().size();

for (auto &hit : hits) {

//double tot_proportional = calculateTOTproportional(hit);
//double tot = HitFinderTools::calculateTOT(hit, HitFinderTools::TOTCalculationType::kThresholdTrapeze)/1000;
double tot_flat = hit->getEnergy();
for (int entry = 0; entry < n_events; ++entry)
{
const JPetEvent& event = dynamic_cast<const JPetEvent&>(timeWindow->operator[](entry));

// reject hits with badly reconstructed TOT
if (
tot_flat <= 0.) {
continue;
}
const auto& hits = event.getHits();
fNumberOfHits = event.getHits().size();

fHitPos.push_back(hit->getPos());
for (auto& hit : hits)
{
// Writing time in nanoseconds
fHitTimes.push_back(hit->getTime() / 1000.);
fHitPos.push_back(hit->getPos());
fHitTOTs.push_back(hit->getEnergy());
fHitScinIDs.push_back(hit->getScin().getID());
fHitTOTsFlat.push_back(tot_flat);
//fHitTOTs.push_back(tot);
}

// only save the event if there was a cluster of 3+ hits in the fine time window
if (isThreeHitCluster(hits)) {
fOutTree->Fill();
}
fOutTree->Fill();
resetRow();
}

} else {
}
else
{
return false;
}
return true;
Expand All @@ -140,88 +102,15 @@ bool NTupler::terminate()
fOutTree->Write();
fOutFile->Close();

// delete fOutTree;
// delete fOutFile;

INFO("Finished reduction of data to ntuples.");
INFO("Finished reduction of data to ntuples.");
return true;
}

/*
double Ntupler::calculateTOTproportional(const JPetHit& hit) const {
auto getThresholdWeight = [&](int i) -> double {
if (i <= 1) {
return 1.;
}
return 1.*(fThresholdValues[i-1] - fThresholdValues[i-2]) / fThresholdValues[0];
};
double tot = 0.;
for(auto& phys_sig: {hit.getSignalA(), hit.getSignalB()}){
const JPetRawSignal& raw_sig = phys_sig.getRecoSignal().getRawSignal();
auto leading_points = raw_sig.getTimesVsThresholdNumber(JPetSigCh::Leading);
auto trailing_points = raw_sig.getTimesVsThresholdNumber(JPetSigCh::Trailing);
for (int i = 1; i <= 4; i++) {
auto lead_search = leading_points.find(i);
auto trail_search = trailing_points.find(i);
if (lead_search != leading_points.end()
&& trail_search != trailing_points.end()){
tot += getThresholdWeight(i) *
(trail_search->second - lead_search->second) / 1000.; // in ns
}
}
}
return tot;
}
*/

bool NTupler::isThreeHitCluster( std::vector<const JPetBaseHit*> hits)
void NTupler::resetRow()
{
vector<JPetEvent> newEventVec;

int s = 0;
int n_hits = hits.size();
//std::cout<<n_hits<<std::endl;
int cluster_size = 0;

while ( s < n_hits ) {

const JPetBaseHit* startHit = hits[s];
cluster_size = 1;

int k = 1;
while ( s + k < n_hits ) {
const JPetBaseHit* currentHit = hits[s + k];
if (fabs(currentHit->getTime() - startHit->getTime()) < kEventTimeWindow) {
cluster_size++;
k++;
} else {
break;
}
}
s += k;
}

if (cluster_size >= 3) {
return true;
}
return false;
}

void NTupler::resetRow() {

fNumberOfHits = 0;
fHitScinIDs.clear();
fHitPos.clear();
fHitTimes.clear();
fHitTOTsFlat.clear();
fHitPos.clear();
fHitTOTs.clear();

fHitScinIDs.clear();
}
52 changes: 17 additions & 35 deletions opsAnalysis/NTupler.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @copyright Copyright 2019 The J-PET Framework Authors. All rights reserved.
* @copyright Copyright 2024 The J-PET Framework Authors. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may find a copy of the License in the LICENCE file.
Expand All @@ -13,65 +13,47 @@
* @file Ntupler.h
*/

#ifndef NTUPLER_H
#define NTUPLER_H
#ifndef NTUPLER_H
#define NTUPLER_H

#include <vector>
#include <map>
#include <string>
#include <Hits/JPetBaseHit/JPetBaseHit.h>
#include <JPetCommonTools/JPetCommonTools.h>
#include <JPetUserTask/JPetUserTask.h>
#include <JPetEvent/JPetEvent.h>
//#include <JPetHit/JPetHit.h>
#include <JPetEvent/JPetEvent.h>
#include <Hits/JPetBaseHit/JPetBaseHit.h>
#include <Hits/JPetMCRecoHit/JPetMCRecoHit.h>
#include <JPetUserTask/JPetUserTask.h>

#include <map>
#include <string>
#include <vector>

#include <TFile.h>
#include <TTree.h>
#include <TVector3.h>

#ifdef __CINT__
# define override
#endif

class NTupler : public JPetUserTask{
class NTupler : public JPetUserTask
{

public:
NTupler(const char * name);
virtual ~NTupler(){}
NTupler(const char* name);
virtual ~NTupler() {}
virtual bool init() override;
virtual bool exec() override;
virtual bool terminate() override;

protected:
//double calculateTOTproportional(const JPetHit& hit) const;
bool isThreeHitCluster(std::vector<const JPetBaseHit*> hits);
protected:
void resetRow();

bool fIsMC = false;
const double kSpeedOfLight = 29.9792458; // cm / ns

//std::string fThresholdValuesKey = "Ntupler_ThresholdValues_std::vector<int>";
//std::vector<int> fThresholdValues;

double kEventTimeWindow = 2500; //5000.0; //ps
const std::string fEventTimeParamKey = "EventFinder_EventTime_float";
//const std::string fEventTimeParamKey = "Ntupler_FineEventTime_float";

TFile* fOutFile;
TTree* fOutTree;
std::string fOutFileName;
std::string fOutFilePath;

// ntuple components
std::vector<double> fHitTimes;
std::vector<TVector3> fHitPos;
std::vector<UChar_t> fHitScinIDs;
std::vector<double> fHitTOTsFlat;
std::vector<double> fHitTOTs;
std::vector<UChar_t> fHitScinIDs;

UChar_t fNumberOfHits;

};

#endif /* !NTUPLER_H */
4 changes: 2 additions & 2 deletions opsAnalysis/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ int main(int argc, const char* argv[])
manager.registerTask<SignalTransformer>("SignalTransformer");
manager.registerTask<HitFinder>("HitFinder");
manager.registerTask<EventFinder>("EventFinder");
// manager.registerTask<NTupler>("NTupler");
manager.registerTask<EventCategorizer>("EventCategorizer");
manager.registerTask<NTupler>("NTupler");

manager.useTask("TimeWindowCreator", "hld", "tslot.calib");
manager.useTask("SignalFinder", "tslot.calib", "raw.sig");
manager.useTask("SignalTransformer", "raw.sig", "phys.sig");
manager.useTask("HitFinder", "phys.sig", "hits");
manager.useTask("EventFinder", "hits", "unk.evt");
// manager.useTask("NTupler", "unk.evt", "tupler");
manager.useTask("EventCategorizer", "unk.evt", "cat.evt");
manager.useTask("NTupler", "cat.evt", "ntu");

manager.run(argc, argv);
}
Expand Down

0 comments on commit 89e61ad

Please sign in to comment.