10 #include "TTreeIndex.h" 11 #include "TVirtualIndex.h" 21 const size_t MEM_SIZE = (size_t)1024*(
size_t)1024*(size_t)1024*(
size_t)8;
26 TList *midvshigh =
new TList;
29 TH2D *midvshigh1 =
new TH2D(
"midvshigh1",
"midvshigh1", 5000, 0, 5000,5000,0,5000); midvshigh->Add(midvshigh1);
30 TH2D *midvshigh2 =
new TH2D(
"midvshigh2",
"midvshigh2", 5000, 0, 5000,5000,0,5000); midvshigh->Add(midvshigh2);
31 TH2D *midvshigh3 =
new TH2D(
"midvshigh3",
"midvshigh3", 5000, 0, 5000,5000,0,5000); midvshigh->Add(midvshigh3);
32 TH2D *midvshigh4 =
new TH2D(
"midvshigh4",
"midvshigh4", 5000, 0, 5000,5000,0,5000); midvshigh->Add(midvshigh4);
35 TBranch *branch = tree->GetBranch(
"TFragment");
39 time_t lowmidtime = tree->GetMinimum(
"MidasTimeStamp");
42 printf(
DYELLOW "Tree Index not found, building index on %s/%s..." RESET_COLOR,
"MidasTimeStamp",
"TimeStampHigh"); fflush(stdout);
43 tree->BuildIndex(
"MidasTimeStamp",
"TimeStampHigh");
46 TTreeIndex *index = (TTreeIndex*)tree->GetTreeIndex();
47 Long64_t *indexvalues = index->GetIndex();
48 int fEntries = index->GetN();
52 tree->GetEntry(indexvalues[0]);
54 int low_hightime[4] = {0,0,0,0};
58 for(
long x=1;x<fEntries;x++) {
59 if(tree->GetEntry(indexvalues[x]) == -1 ) {
60 printf(
"FIRE!!!" "\n");
65 long hightime = currentFrag->TimeStampHigh;
66 time_t midtime = currentFrag->MidasTimeStamp - lowmidtime;
68 digitizer = (int)((currentFrag->ChannelNumber-1)/16.0);
69 if(currentFrag->DetectorType == 1)
70 ((TH2D*)(midvshigh->At(digitizer)))->Fill(midtime, hightime);
71 if(hightime < low_hightime[digitizer])
72 low_hightime[digitizer] = hightime;
77 for(
int i =0; i<4; i++){
78 if(low_hightime[i] < low_hightime[lowest_dig])
83 printf(
"The lowest digitizer is %d\n",lowest_dig);
84 printf(
"***** High time shifts *******\n");
85 for(
int i =0; i<4;i++){
86 printf(
"%d:\t %d\n",i,low_hightime[i]);
88 correction[i] = ((low_hightime[i] - low_hightime[lowest_dig]) & 0x00003fff)<<28;
90 printf(
"********************\n");
100 printf(
DYELLOW "Tree Index not found, building index on %s/%s..." RESET_COLOR,
"MidasTimeStamp",
"TimeStampLow"); fflush(stdout);
101 tree->BuildIndex(
"MidasTimeStamp",
"TimeStampLow");
104 TList *bestvsrough =
new TList;
105 TH1C *bestvs1rough =
new TH1C(
"bestvs1rough",
"bestvs1rough",50E6,-25E6,25E6); bestvsrough->Add(bestvs1rough);
106 TH1C *bestvs2rough =
new TH1C(
"bestvs2rough",
"bestvs2rough",50E6,-25E6,25E6); bestvsrough->Add(bestvs2rough);
107 TH1C *bestvs3rough =
new TH1C(
"bestvs3rough",
"bestvs3rough",50E6,-25E6,25E6); bestvsrough->Add(bestvs3rough);
108 TH1C *bestvs4rough =
new TH1C(
"bestvs4rough",
"bestvs4rough",50E6,-25E6,25E6); bestvsrough->Add(bestvs4rough);
111 TBranch *branch = tree->GetBranch(
"TFragment");
115 int best_digitizer = 0;
117 for(
int i =0;i<4;i++){
118 printf(
"Correction %d: %ld\n",i,correction[i]);
119 if(correction[best_digitizer] > correction[i])
122 printf(
"Assuming the best digitizer is: Digitizer %d\n",best_digitizer);
124 TTreeIndex *index = (TTreeIndex*)tree->GetTreeIndex();
125 Long64_t *indexvalues = index->GetIndex();
126 int fEntries = index->GetN();
127 time_t lowmidtime = tree->GetMinimum(
"MidasTimeStamp");
131 bool keepfilling[4] = {1,1,1,1};
133 tree->GetEntry(indexvalues[0]);
135 double low_hightime[4] = {0.0,0.0,0.0,0.0};
139 for(
long x=1;x<fEntries;x++) {
140 if(tree->GetEntry(indexvalues[x]) == -1 ) {
141 printf(
"FIRE!!!" "\n");
148 if(best_digitizer !=(
int)((currentFrag->ChannelNumber-1)/16.0))
continue;
149 int64_t besttime = currentFrag->
GetTimeStamp() - correction[best_digitizer];
150 time_t midtime = myFrag.MidasTimeStamp - lowmidtime;
151 if(midtime > 3)
break;
153 for(
long y=x+1;y<fEntries;y++) {
158 if(tree->GetEntry(indexvalues[y]) == -1 ) {
159 printf(
"FIRE!!!" "\n");
163 printf(
"\tOn fragment %i/%i MidasTime:%d\r",x,fEntries,midtime);
165 if(currentFrag->MidasTimeStamp - lowmidtime - midtime > 0)
break;
167 if(myFrag.DetectorType == 1 && currentFrag->DetectorType == 1) {
168 int mydigitizer = (int)((currentFrag->ChannelNumber-1)/16.0);
169 if(keepfilling[mydigitizer]){
170 fillhist = (TH1C*)(bestvsrough->At(mydigitizer));
171 Int_t bin = currentFrag->
GetTimeStamp()-correction[mydigitizer]-besttime;
172 if(fillhist->FindBin(bin) > 0 && fillhist->FindBin(bin) < fillhist->GetNbinsX()){
173 if(fillhist->GetBinContent(fillhist->Fill(bin))>126){
174 keepfilling[mydigitizer] =
false;
184 for(
int i=0;i<4;i++){
185 fillhist = (TH1C*)(bestvsrough->At(i));
186 correction[i] += (int64_t)fillhist->GetBinCenter(fillhist->GetMaximumBin()) ;
187 printf(
"Maximum bin in channel %d = %ld\n",i,correction[i]);
189 bestvsrough->Print();
190 bestvsrough->Write();
191 bestvsrough->Delete();
196 const int range = 1000;
199 printf(
DYELLOW "Tree Index not found, building index on %s/%s..." RESET_COLOR,
"Primary Filter Id",
"TimeStampLow"); fflush(stdout);
200 tree->BuildIndex(
"TriggerId",
"TimeStampLow");
202 TList* bestvs =
new TList;
204 TH1D *bestvs1 =
new TH1D(
"bestvs1",
"bestvs1",1000,-500,500); bestvs->AddLast(bestvs1);
205 TH1D *bestvs2 =
new TH1D(
"bestvs2",
"bestvs2",1000,-500,500); bestvs->AddLast(bestvs2);
206 TH1D *bestvs3 =
new TH1D(
"bestvs3",
"bestvs3",1000,-500,500); bestvs->AddLast(bestvs3);
207 TH1D *bestvs4 =
new TH1D(
"bestvs4",
"bestvs4",1000,-500,500); bestvs->AddLast(bestvs4);
210 TBranch *branch = tree->GetBranch(
"TFragment");
214 int best_digitizer = 0;
216 for(
int i =0;i<4;i++){
217 printf(
"Rough Low-time-stamp shift %d: %ld\n",i,correction[i]);
218 if(correction[best_digitizer] > correction[i])
221 printf(
"Assuming the best digitizer is: Digitizer %d\n",best_digitizer);
223 TTreeIndex *index = (TTreeIndex*)tree->GetTreeIndex();
224 Long64_t *indexvalues = index->GetIndex();
225 int fEntries = index->GetN();
229 bool keepfilling[4] = {1,1,1,1};
232 tree->GetEntry(indexvalues[0]);
234 double low_hightime[4] = {0.0,0.0,0.0,0.0};
238 for(
long x=1;x<fEntries;x++) {
239 if(tree->GetEntry(indexvalues[x]) == -1 ) {
240 printf(
"FIRE!!!" "\n");
247 if(best_digitizer !=(
int)((currentFrag->ChannelNumber-1)/16.0))
continue;
248 int64_t besttime = currentFrag->
GetTimeStamp() - correction[best_digitizer];
249 int myId = myFrag.TriggerId;
250 if(histsfull > 3)
break;
251 for(
long y=x-1000;y<fEntries;y++) {
257 if(tree->GetEntry(indexvalues[y]) == -1 ) {
258 printf(
"FIRE!!!" "\n");
262 printf(
"\tOn fragment %i/%i\r",x,fEntries);
264 int currentId = currentFrag->TriggerId;
265 if(TMath::Abs(currentId - myId > 1000))
break;
267 if(myFrag.DetectorType == 1 && currentFrag->DetectorType == 1) {
268 int mydigitizer = (int)((currentFrag->ChannelNumber-1)/16.0);
269 if(keepfilling[mydigitizer]){
270 fillhist = (TH1C*)(bestvs->At(mydigitizer));
271 Int_t bin = currentFrag->
GetTimeStamp()-correction[mydigitizer]-besttime;
272 if(fillhist->FindBin(bin) > 0 && fillhist->FindBin(bin) < fillhist->GetNbinsX()){
273 if(fillhist->GetBinContent(fillhist->Fill(bin))>150){
274 keepfilling[mydigitizer] =
false;
289 for(
int i=0;i<4;i++){
290 fillhist = (TH1C*)(bestvs->At(i));
291 TF1* gausfit =
new TF1(Form(
"Fit%d",i+1),
"[0]*TMath::Exp(-(x-[1])*(x-[1])/2./([2]*[2])) + [3]",-100,100);
292 gausfit->SetParNames(
"Constant",
"Mean",
"Sigma",
"Flat");
293 gausfit->SetParameters(500, 0.0, 10.0, 10);
294 gausfit->SetParLimits(1,-50,50);
295 gausfit->SetParLimits(2,0.0,30.0);
296 gausfit->SetParLimits(3,0,10000000);
297 gausfit->SetNpx(1000);
298 fillhist->Fit(gausfit,
"MLR+");
299 if(i!=best_digitizer){
300 correction[i] += (int64_t)(gausfit->GetParameter(1));
303 correction[i] = (int64_t)(gausfit->GetParameter(1));
304 printf(
"Channel %d is the best\n",i);
307 int64_t best_time_diff = correction[best_digitizer];
308 for(
int i=0;i<4;i++){
309 correction[i] -= best_time_diff;
310 printf(
"Correction for channel %d = %ld *10ns\n",i,correction[i]);
321 const Int_t ENTRIES = 100000;
324 printf(
"Usage: ./offsetfind <input.mid>\n");
329 GFile *f =
new GFile(
argv[1],
"READ");
330 TTree *tree = (TTree*)f->Get(
"FragmentTree");
332 GFile *outfile =
new GFile(
"outfile.root",
"RECREATE");
337 int64_t correction[4] = {0,0,0,0};
338 int64_t dig_number[4] = {0,1,2,3};
int main(int argc, char **argv)
void SetAddress(const UInt_t &temp_address)
!
static Int_t ReadCalFromTree(TTree *, Option_t *opt="overwrite")
void GetTimeDiff(TTree *tree, int64_t *correction)
void Print(Option_t *opt="") const override
void GetRoughTimeDiff(TTree *tree, int64_t *correction)
void CheckHighTimeStamp(TTree *tree, int64_t *correction)
virtual Long64_t GetTimeStamp(Option_t *="") const