GRSISort
Created by P.C. Bender
Developement Team: P.C. Bender, R. Dunlop, V. Bildstein
An extension of the ROOT analysis Framework
write_cal.cxx
Go to the documentation of this file.
1 //Experiment: S1607
2 
3 //Root version: 6.08/02
4 //GRSISort:3.1.3.2
5 
6 //Compilation: If placed in the GRSISort/scripts directory, GRSISort will automatically compile it with a make
7 
8 //Usage is: TAC_offset /path/to/analysistree.root
9 
10 //The analog TAC signals are delayed by a (rather) fixed time. This can create problems when building the AnalysisTree coincidences. To avoid that, a constant offset can be applied to the TAC timestamp. This value is given in the calfile. This script helps determining which value should be set. It produces a set of histograms with the timestamp difference between each TAC and its starting LaBr. One set of histograms is done with the TAC offset timestamp set to 0 and the other with the current one from the calfile. You should adjust the offset so the main peak is roughly at 0.
11 
12 //NOTE: take into account that the histograms are in ns, but the calfile parameters (offset or time build window, for example) are in 10 ns.
13 
14 #include "TFile.h"
15 #include "TChain.h"
16 #include "TTree.h"
17 #include "TH1F.h"
18 #include "TF1.h"
19 #include "TMath.h"
20 
21 #include "TGriffin.h"
22 #include "TSceptar.h"
23 #include "TLaBr.h"
24 #include "TZeroDegree.h"
25 #include "TTAC.h"
26 
27 #include <vector>
28 #include <string>
29 
30 #define FIRST_CHANNEL 84
31 #define LAST_CHANNEL 91
32 int main(int argc, char** argv) {
33  if(argc == 1) {
34  std::cout<<"Usage: "<<argv[0]<<" <analysis tree file(s)>"<<std::endl;
35  return 1;
36  }
37 
38  std::cout<<std::endl
39  <<"WARNING: This script assumes that the TACs are in channels FIRST_CHANNEL-LAST_CHANNEL (which are the default). Should have they been assigned to other channel numbers, the script should be edited acordingly"<<std::endl
40  <<std::endl;
41 
42  TFile* file = new TFile(argv[1]);
43 
44  TTree* AnalysisTree = static_cast<TTree*>(file->Get("AnalysisTree"));
45 
46  TChannel::ReadCalFromTree(AnalysisTree);
47 
48  TChannel* channel = nullptr;
49 
50  double offset[8];
51 
52  for(int n = FIRST_CHANNEL; n <= LAST_CHANNEL; n++) {
53  channel = TChannel::GetChannelByNumber(n);
54  offset[n-FIRST_CHANNEL] = channel->GetTimeOffset();
55  std::cout<<"Current TAC offset in the calfile: "<<offset[n-FIRST_CHANNEL]<<" for channel #"<<n<<std::endl;
56  }
57 
58 
59  TLaBr* labr = nullptr;
60  TTAC* tac = nullptr;
61  TGriffin* grif = nullptr;
62 
63  if(AnalysisTree->SetBranchAddress("TLaBr", &labr) == TTree::kMissingBranch) {
64  labr = new TLaBr;
65  }
66  if(AnalysisTree->SetBranchAddress("TTAC", &tac) == TTree::kMissingBranch) {
67  tac = new TTAC;
68  }
69  if(AnalysisTree->SetBranchAddress("TGriffin", &grif) == TTree::kMissingBranch) {
70  grif = new TGriffin;
71  }
72 
73  Long64_t nEntries = AnalysisTree->GetEntries();
74 
75  std::string outname = argv[1];
76  outname.replace(outname.begin(), outname.end()-14, "TAC_offset");
77  std::cout<<"writing to '"<<outname<<"'"<<std::endl;
78 
79  TFile outfile(outname.c_str(), "recreate");
80 
81  TList list;
82 
83  UInt_t labr1,labr2,tac1; //counters
84 
85  Double_t progress;
86  Int_t multi_labr, multi_tac, multi_grif;
87 
88  //TAC offset histograms
89  TH1D* TAC_offset[8];
90  for(int i = 0; i < 8; ++i) {
91  TAC_offset[i] = new TH1D(Form("TAC_offset_%d",i), Form("TAC_offset_%d; time (ns); counts/ns",i), 10000,-5000.,5000.); list.Add(TAC_offset[i]);
92  }
93 
94  TH1D* TAC_offset_corrected[8];
95  for(int i = 0; i < 8; ++i) {
96  TAC_offset_corrected[i] = new TH1D(Form("TAC_offset_corrected_%d",i), Form("TAC_offset_corrected_%d; time (ns); counts/ns",i), 10000,-5000.,5000.); list.Add(TAC_offset_corrected[i]);
97  }
98 
99  TH1D* time_diff[8];
100  for(int i = 0; i < 8; ++i) {
101  time_diff[i] = new TH1D(Form("time_diff%d",i), Form("Time difference for LaBr_%d - LaBr with TAC coincidence; time (ns); counts/ns",i), 10000,-5000.,5000.); list.Add(time_diff[i]);
102  }
103  TH1D* time_diff_noTAC[8];
104  for(int i = 0; i < 8; ++i) {
105  time_diff_noTAC[i] = new TH1D(Form("time_diff_noTAC%d",i), Form("Time difference for LaBr_%d - LaBr with no TAC coincidence; time (ns); counts/ns",i), 10000,-5000.,5000.); list.Add(time_diff_noTAC[i]);
106  }
107 
108  TH1D* timestamp_diff_noTACcoinc[8];
109  for(int i = 0; i < 8; ++i) {
110  timestamp_diff_noTACcoinc[i] = new TH1D(Form("timestamp_diff_noTACcoinc%d",i), Form("Timestamp difference for LaBr_%d - LaBr, no TAC coincidence; time (ns); counts/ns",i), 10000,-5000.,5000.); list.Add(timestamp_diff_noTACcoinc[i]);
111  }
112  TH1D* timestamp_diff_TACcoinc[8];
113  for(int i = 0; i < 8; ++i) {
114  timestamp_diff_TACcoinc[i] = new TH1D(Form("timestamp_diff_TACcoinc%d",i), Form("Timestamp difference for LaBr_%d - LaBr, with TAC coincidence; time (ns); counts/ns",i), 10000,-5000.,5000.); list.Add(timestamp_diff_TACcoinc[i]);
115  }
116 
117  TH1D* timestamp_diff_hpge = new TH1D("timestamp_diff_hpge", "Timestamp difference for HPGe - LaBr, with TAC coincidence; time (ns); counts/ns", 10000,-5000.,5000.); list.Add(timestamp_diff_hpge);
118  TH1D* time_diff_hpge = new TH1D("time_diff_hpge", "Time difference for HPGe - LaBr, with TAC coincidence; time (ns); counts/ns", 10000,-5000.,5000.); list.Add(time_diff_hpge);
119 
120  for(Long64_t n = 0; n < nEntries; ++n) { //This is the main loop, that will cycle through the entire AnalysisTree
121  AnalysisTree->GetEntry(n);
122 
123  //Builds the number of detector hits in each event (multiplicity of the coincidence)
124  multi_labr = labr->GetMultiplicity();
125  multi_tac = tac->GetMultiplicity();
126  multi_grif = grif->GetMultiplicity();
127 
128  if(multi_labr==2) {
129  labr1=labr->GetLaBrHit(0)->GetDetector()-1;//GetDetector goes from 1-8, while the counter goes from 0-7, hence the -1
130  labr2=labr->GetLaBrHit(1)->GetDetector()-1;//GetDetector goes from 1-8, while the counter goes from 0-7, hence the -1
131  timestamp_diff_noTACcoinc[labr2]->Fill(labr->GetLaBrHit(1)->GetTimeStamp()-labr->GetLaBrHit(0)->GetTimeStamp());
132  time_diff_noTAC[labr2]->Fill(labr->GetLaBrHit(1)->GetTime()-labr->GetLaBrHit(0)->GetTime());
133  }
134 
135  if(multi_labr==2 && multi_tac==1) { //It only looks for LaBrx2+TAC coincidences
136  labr1=labr->GetLaBrHit(0)->GetDetector()-1;//GetDetector goes from 1-8, while the counter goes from 0-7, hence the -1
137  labr2=labr->GetLaBrHit(1)->GetDetector()-1;//GetDetector goes from 1-8, while the counter goes from 0-7, hence the -1
138  tac1=tac->GetTACHit(0)->GetDetector()-1;//GetDetector goes from 1-8, while the counter goes from 0-7, hence the -1
139  if(labr1<labr2) {
140  if(tac1==labr1) {
141  TAC_offset[tac1]->Fill(labr->GetLaBrHit(0)->GetTime()-tac->GetTACHit(0)->GetTime()+offset[tac1]*10);
142  TAC_offset_corrected[tac1]->Fill(labr->GetLaBrHit(0)->GetTime()-tac->GetTACHit(0)->GetTime());
143  time_diff[labr2]->Fill(labr->GetLaBrHit(1)->GetTime()-labr->GetLaBrHit(0)->GetTime());
144  timestamp_diff_TACcoinc[labr2]->Fill(labr->GetLaBrHit(1)->GetTimeStamp()-labr->GetLaBrHit(0)->GetTimeStamp());
145  if(multi_grif>0) {
146  for(int i=0; i<multi_grif;i++) {
147  time_diff_hpge->Fill(grif->GetGriffinHit(i)->GetTime()-labr->GetLaBrHit(0)->GetTime());
148  timestamp_diff_hpge->Fill(grif->GetGriffinHit(i)->GetTimeStamp()-labr->GetLaBrHit(0)->GetTimeStamp());
149  }
150  }
151  }
152  } else if(labr1>labr2) {
153  if(tac1==labr2) {
154  TAC_offset[tac1]->Fill(labr->GetLaBrHit(1)->GetTime()-tac->GetTACHit(0)->GetTime()+offset[tac1]*10);
155  TAC_offset_corrected[tac1]->Fill(labr->GetLaBrHit(1)->GetTime()-tac->GetTACHit(0)->GetTime());
156  time_diff[labr2]->Fill(labr->GetLaBrHit(1)->GetTime()-labr->GetLaBrHit(0)->GetTime());
157  timestamp_diff_TACcoinc[labr2]->Fill(labr->GetLaBrHit(1)->GetTimeStamp()-labr->GetLaBrHit(0)->GetTimeStamp());
158  if(multi_grif>0) {
159  for(int i=0; i<multi_grif;i++) {
160  time_diff_hpge->Fill(grif->GetGriffinHit(i)->GetTime()-labr->GetLaBrHit(0)->GetTime());
161  timestamp_diff_hpge->Fill(grif->GetGriffinHit(i)->GetTimeStamp()-labr->GetLaBrHit(0)->GetTimeStamp());
162  }
163  }
164  }
165  }
166  }
167 
168  if(n%10000 == 0) {
169  progress = ((double_t) n)/((double_t) nEntries);
170  std::cout<<std::setw(4)<<100*progress<<"% done\r"<<std::flush;
171  }
172 
173  }//Analysistree loop
174  std::cout<<"100% done"<<std::endl;
175 
176  list.Sort();
177  list.Write();
178 
179  return 0;
180 }
TTACHit * GetTACHit(const int &i) const
Definition: TTAC.h:34
static TChannel * GetChannelByNumber(int temp_num)
Definition: TChannel.cxx:363
int main(int argc, char **argv)
Definition: write_cal.cxx:32
static Int_t ReadCalFromTree(TTree *, Option_t *opt="overwrite")
Definition: TChannel.cxx:890
Definition: TLaBr.h:28
virtual Double_t GetTime(const ETimeFlag &correct_flag=ETimeFlag::kAll, Option_t *opt="") const
Returns a time value to the nearest nanosecond!
Short_t GetMultiplicity() const override
Definition: TGriffin.h:49
Definition: TTAC.h:27
#define FIRST_CHANNEL
Definition: write_cal.cxx:30
TLaBrHit * GetLaBrHit(const int &i) const
Definition: TLaBr.h:46
Long64_t GetTimeOffset() const
Definition: TChannel.h:173
virtual Int_t GetDetector() const
!
#define LAST_CHANNEL
Definition: write_cal.cxx:31
virtual Long64_t GetTimeStamp(Option_t *="") const
Definition: TDetectorHit.h:142
virtual Short_t GetMultiplicity() const
Definition: TDetector.h:62
TGriffinHit * GetGriffinHit(const int &i)
!
Definition: TGriffin.h:44