HARPO  5.1.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
HarpoClustering.cxx
Go to the documentation of this file.
1 //
2 // File HarpoClustering.cxx
3 //
17 #include "HarpoClustering.h"
18 #include "HarpoConfig.h"
19 #include "HarpoDetSet.h"
20 #include "HarpoDebug.h"
21 #include "HarpoDccEvent.h"
22 #include "Pmm2Event.h"
23 #include "HarpoEvent.h"
24 #include "HarpoRecoEvent.h"
25 
26 #include "TFile.h"
27 #include "TStyle.h"
28 #include "TCanvas.h"
29 #include "TRootEmbeddedCanvas.h"
30 #include "TLatex.h"
31 #include "TGraph.h"
32 #include "TF1.h"
33 #include "TMath.h"
34 #include "TSystem.h"
35 #include "TPolyLine.h"
36 
37 #include <cstdlib>
38 #include <cstring>
39 #include <cassert>
40 #include <fstream>
41 #include <iostream>
42 
43 ClassImp(HarpoClustering);
44 
46  {
47  unsigned int nd; // number of detectors
49 
50  assert(hdr != NULL);
51  hdr->print();
52 
53  for (nd = 0; nd < gkNDetectors; nd++) {
54  // if (fEvt->isdataExist(nd)) {
55  HarpoDccMap *plane = fEvt->GetDccMap(nd);
56  if (plane != NULL )plane->print();
57  }
58  }
59 
61 {
62  // Main process, calls FindCluster for each map
63 
64 
65 
66  // Bool_t drawEvent = kFALSE;
67  nEvents++;
68 
69  fNcl = 0;
71 
72  // Process RAW data
73  for(Int_t ndet=0;ndet<2;ndet++) { // Detectors 0 and 1 = X and Y planes
74  if (!gHDetSet->isExist(ndet)) continue;
75  HarpoDccMap* m = fEvt->GetDccMap(ndet);
76  if ( m == NULL ) continue;
77  fTmin = 1000;
78  fTmax = -1000;
79  fXmin = 1000;
80  fXmax = -1000;
81  fNCcl = 0;
82  fNTcl = 0;
83  fQtot = 0;
84  fXstart = -1000;
85  fTstart = -1000;
86 
87  for(Int_t i = 0; i<NALL; i++) fBadCh[i] = -1;
88  fBadCh[93] = 2; // GEM separators
89  fBadCh[94] = 2; // GEM separators
90  fBadCh[193] = 2; // GEM separators
91  fBadCh[194] = 2; // GEM separators
92 
93  if(ndet == 0){
94  fBadCh[28] = 2; // GEM sector limits
95  fBadCh[61] = 2; // GEM sector limits
96  fBadCh[127] = 2; // GEM sector limits
97  fBadCh[160] = 2; // GEM sector limits
98  fBadCh[226] = 2; // GEM sector limits
99  fBadCh[259] = 2; // GEM sector limits
100  }
101 
102  if(fHist[ndet]){
103  fHist[ndet]->GetXaxis()->SetRangeUser(0,511);
104  fHist[ndet]->GetYaxis()->SetRangeUser(0,304);
105  fCanvas[ndet]->Update();
106  }
107  // MAKE CLUSTERS
108  FindCluster(m,CCLUSTER,ndet);
109  FindCluster(m,TCLUSTER,ndet);
110  if(fHist[ndet]){
111  fHist[ndet]->GetXaxis()->SetRangeUser(0,511);
112  fHist[ndet]->GetYaxis()->SetRangeUser(0,304);
113  fCanvas[ndet]->Update();
114  }
115 
116  if(ndet==XPLANE){
125 
128  }
129  if(ndet==YPLANE){
138 
141  }
142 
143  }
144 }
145 
146 
148 
157 void HarpoClustering::FindCluster(HarpoDccMap* m, Int_t type, Int_t plane)
158 {
159 
160 
161 
162  Int_t iMax = 0, jMax = 0;
163  switch(type){
164  case TCLUSTER:
165  jMax = NCHAN;
166  iMax = NADC;
167  break;
168  case CCLUSTER:
169  jMax = NADC;
170  iMax = NCHAN;
171  break;
172  default:
173  Warning("FindCluster","Unknown cluster type %d",type);
174  return;
175  }
176 
177  Double_t xStart = 0, tStart = 0, qStart = 0;
178  Int_t nbinsStart = 0;
179 
180  for(Int_t i=0;i<iMax;i++){
181  // Initialisation
182  Double_t qTot = 0, qTotCh = 0, qMax = 0;
183  Double_t mean = 0, sigma = 0;
184  Int_t width = 0;
185  Double_t qold = -1000;
186  Int_t flag = 0;
187  Int_t up = -1;
188  Bool_t noisy = kFALSE;
189  for(Int_t j=0;j<jMax;j++){
190  Double_t q, noise, noiseThr, thr;
191  Double_t xmin, xmax, ymin, ymax, xp, yp;
192  if(type == TCLUSTER){
193  q = m->GetData(j,i);
194  if(q>-1000){
195  fQtot += q;
196  if(nbinsStart<4){
197  xStart += j*q;
198  tStart += i*q;
199  qStart += q;
200  nbinsStart++;
201  }
202  }
203  noise = m->GetSigmaNoise(j);
204  thr = noise*fThr;
205  // if(fBadCh[j]>0){
206  // if(q==-1000) q=0;
207  // noise = 500*(7*fBadCh[j] - 6) ;
208  // }
209  if(fBadCh[j]>1){
210  //noisy = kTRUE;
211  noise = 500*(7*fBadCh[j] - 6) ;
212  if(gHarpoDebug>2)
213  Info("FindCluster","Dead Channel %d (%d, %d)", j, i, up);
214  //continue;
215  }
216  noiseThr = noise*fNoiseThr;
217  xmin = i - 10;
218  xmax = i + 10;
219  ymin = j - 10;
220  ymax = j + 10;
221  xp = i;
222  yp = j;
223  }else{
224  q = m->GetData(i,j);
225  noise = m->GetSigmaNoise(i);
226  noiseThr = noise*fNoiseThr;
227  thr = noise*fThr;
228 
229  xmin = j - 10;
230  xmax = j + 10;
231  ymin = i - 10;
232  ymax = i + 10;
233  xp = j;
234  yp = i;
235  }
236  if(q>10){
237  if(xp>fTmax) fTmax = xp;
238  if(xp<fTmin) fTmin = xp;
239  if(yp>fXmax) fXmax = yp;
240  if(yp<fXmin) fXmin = yp;
241  }
242  if(fHist[plane] != 0 && q>-1000){
243  //gSystem->Sleep(1);
244  if(xmin<0) xmin = 0;
245  if(ymin<0) ymin = 0;
246  if(xmax>NADC) xmax = NADC;
247  if(ymax>NCHAN) ymax = NCHAN;
248  fCanvas[plane]->cd();
249  Float_t x[4]; // = {xp,xp,xp+1,xp+1};
250  x[0] = xp;
251  x[1] = xp;
252  x[2] = xp+1;
253  x[3] = xp+1;
254  Float_t y[4]; // = {yp,yp+1,yp+1,yp};
255  y[0] = yp;
256  y[1] = yp+1;
257  y[2] = yp+1;
258  y[3] = yp;
259  TPolyLine* triangle = new TPolyLine(4,x,y,"FL");
260  triangle->SetLineColor(kBlack);
261  triangle->SetFillColor(3+up);
262  //triangle->SetFillStyle(3002);
263  fCanvas[plane]->cd();
264  triangle->Draw("FL");
265  // if(plane == 1 && yp == 141)
266  // std::cout << noiseThr << " " << thr << " " << type << std::endl;
267 
268  // fHist[plane]->GetXaxis()->SetRangeUser(xmin,xmax);
269  // fHist[plane]->GetYaxis()->SetRangeUser(ymin,ymax);
270  //fCanvas[plane]->Update();
271  }
272 
273  if(q!=-1000.)
274  qTotCh += q;
275  // if(q != -1000)
276  // std::cout << "[" << q << "," << up << "]" << std::flush;
277  // if(up == 1 && q>thr && q < qold - noiseThr){ // Peak reached
278  if(up == 1 && q < qold - noiseThr){ // Peak reached
279  up = 2; // DOWN
280  }
281  // DOWN slope
282  if((up == 2 && q > qold + noiseThr) || (up != -1 && (q < thr || j == jMax-1))){ // End of cluster
283  if(gHarpoDebug>2){
284  Info("FindCluster","%d %d (%d, %d)", i, j, type, plane);
285  Info("FindCluster","q=%f qold=%f noise=%f fThr=%f", q, qold, noise, fThr);
286  }
287  // std::cout << std::endl;
288  // std::cout << "--------- " << qTot << " -------" << std::endl;
289  mean /= qTot;
290  sigma /= qTot;
291  sigma -= mean*mean;
292  if(q > qold + noiseThr || j == jMax-1)
293  flag = flag | 1; // Cut After;
294  if(noisy){
295  flag &= 4;
296  if(gHarpoDebug>1)
297  Info("FindCluster","Noisy Cluster %d %d %d %g",fNcl,flag,type,mean);
298  }// else
299  if(gHarpoDebug>2)
300  Info("FindCluster","%d: iFirst = %d",fNcl,j-width);
301  HarpoRecoClusters *cl = new HarpoRecoClusters(type,qTot,qMax,mean,i,TMath::Sqrt(sigma),j-width,width,-1,plane,flag);
302  //fEvt->GetRecoEvent()->SetClusters(cl,fNcl);
303  if(type == CCLUSTER){
304  cl->SetX(i);
305  cl->SetZ(mean);
306  }
307  if(type == TCLUSTER){
308  cl->SetZ(i);
309  cl->SetX(mean);
310  }
311  fEvt->GetRecoEvent()->AddClusters(cl);
312  fNcl++;
313  if(type == CCLUSTER) fNCcl++;
314  if(type == TCLUSTER) fNTcl++;
315  cl->Delete();
316  qTot = 0;
317  qMax = 0;
318  mean = 0;
319  sigma = 0;
320  width = 0;
321  if(flag&1)
322  flag = 2; // Cut before
323  else
324  flag = 0;
325  up = -1; //kTRUE; // OUT OF CLUSTER
326  noisy = kFALSE;
327  }
328  // if(plane == 1 && yp == 141 && type == 0)
329  // std::cout << q << " " << qold << " " << up << std::endl;
330  if(q<thr){
331  qold = q;
332  continue;
333  }
334  if(q > qold + noiseThr || (qold<thr && q>thr))
335  up = 1;
336 
337 
338 
339  if(type == TCLUSTER && fBadCh[j]>1)
340  noisy = kTRUE;
341  if(j-1>0)
342  if(type == TCLUSTER && fBadCh[j-1]>1)
343  noisy = kTRUE;
344  if(j+1<NCHAN)
345  if(type == TCLUSTER && fBadCh[j+1]>1)
346  noisy = kTRUE;
347 
348 
349  if(j == 0) flag = 2; // Cut before
350  qTot += q;
351  mean += q*j;
352  sigma += q*j*j;
353  width +=1;
354  if(q>qMax) qMax = q;
355  qold = q;
356 
357 
358 
359  }
360 
361  //std::cout << qTotCh << std::endl;
362  // if(type == 0 && qTotCh>6.2e4){
363  // //Info("FindCluster","Bad channel %d (%g) [%d %d %d]",i,qTotCh,fBadCh[i-1],fBadCh[i],fBadCh[i+1]);
364  // fBadCh[i-1] += 1;
365  // fBadCh[i] += 1;
366  // fBadCh[i+1] += 1;
367  // //Info("FindCluster","**Bad channel %d (%g) [%d %d %d]",i,qTotCh,fBadCh[i-1],fBadCh[i],fBadCh[i+1]);
368  // }
369  }
370 
371 
372  if(type == TCLUSTER){
373  if(qStart>0){
374  fXstart = xStart/qStart;
375  fTstart = tStart/qStart;
376  }else{
377  fXstart = -1000;
378  fTstart = -1000;
379  }
380  }
381 
382 
383  // std::cout << fNcl << std::endl;
384 
385 }
386 
387 void HarpoClustering::DisplayAnalysis(TRootEmbeddedCanvas* ecTab, TGListBox* /* infobox */)
388 {
389 
390  TCanvas* cTab = ecTab->GetCanvas();
391  HarpoRecoEvent* reco = fEvt->GetRecoEvent();
392  TClonesArray* clArray = reco->GetClustersArray();
393  Int_t nCl = clArray->GetEntries();
394 
395  TGraph* gCX = new TGraph();
396  TGraph* gCY = new TGraph();
397  TGraph* gTX = new TGraph();
398  TGraph* gTY = new TGraph();
399 
400  TGraph* gTXnoise = new TGraph();
401  TGraph* gTYnoise = new TGraph();
402  gTXnoise->SetMarkerStyle(28);
403  gTYnoise->SetMarkerStyle(28);
404  // TGraph* gCXnotrack = new TGraph();
405  // TGraph* gCYnotrack = new TGraph();
406  // TGraph* gTXnotrack = new TGraph();
407  // TGraph* gTYnotrack = new TGraph();
408 
409  for(Int_t icl = 0; icl<nCl; icl++){
410  HarpoRecoClusters* cluster = (HarpoRecoClusters*)clArray->At(icl);
411  Int_t ind = cluster->GetIndex();
412  Double_t mean = cluster->GetMean();
413  Int_t plane = cluster->GetPlane();
414  Int_t type = cluster->GetType();
415  //Int_t width = cluster->GetWidth();
416  //Double_t q = cluster->GetQ();
417  //Int_t idcluster = cluster->GetIdClusterTrack();
418  if(plane!=XPLANE && plane !=YPLANE) continue;
419  // if(type == CCLUSTER && width<10) continue;
420  // if(type == TCLUSTER && width>10) continue;
421  if(plane==XPLANE && type == CCLUSTER) gCX->SetPoint(gCX->GetN(),mean,ind+0.5);
422  if(plane==XPLANE && type == TCLUSTER) gTX->SetPoint(gTX->GetN(),ind+0.5,mean);
423  if(plane==YPLANE && type == CCLUSTER) gCY->SetPoint(gCY->GetN(),mean,ind+0.5);
424  if(plane==YPLANE && type == TCLUSTER) gTY->SetPoint(gTY->GetN(),ind+0.5,mean);
425 
426 
427 
428  if(cluster->GetQuality()<0){
429  // std::cout << "Noisy cluster: " << icl << " " << cluster->GetQuality() << std::endl;
430  if(plane==XPLANE && type == TCLUSTER) gTXnoise->SetPoint(gTXnoise->GetN(),ind+0.5,mean);
431  if(plane==YPLANE && type == TCLUSTER) gTYnoise->SetPoint(gTYnoise->GetN(),ind+0.5,mean);
432  }
433  // if(idcluster>=0) continue;
434  // if(plane==0 && type == 0) gCXnotrack->SetPoint(gCXnotrack->GetN(),mean,ind);
435  // if(plane==0 && type == 1) gTXnotrack->SetPoint(gTXnotrack->GetN(),ind,mean);
436  // if(plane==1 && type == 0) gCYnotrack->SetPoint(gCYnotrack->GetN(),mean,ind);
437  // if(plane==1 && type == 1) gTYnotrack->SetPoint(gTYnotrack->GetN(),ind,mean);
438  }
439 
440  gCX->SetMarkerStyle(7);
441  gTX->SetMarkerStyle(7);
442  gCY->SetMarkerStyle(7);
443  gTY->SetMarkerStyle(7);
444  gCX->SetMarkerSize(0.5);
445  gTX->SetMarkerSize(0.5);
446  gCY->SetMarkerSize(0.5);
447  gTY->SetMarkerSize(0.5);
448  gCX->SetMarkerColor(kGray);
449  gCY->SetMarkerColor(kGray);
450  // gCXnotrack->SetMarkerStyle(7);
451  // gTXnotrack->SetMarkerStyle(7);
452  // gCYnotrack->SetMarkerStyle(7);
453  // gTYnotrack->SetMarkerStyle(7);
454 
455  std::cout << gCX->GetN() << " " << gCY->GetN() << " " << gTX->GetN() << " " << gTY->GetN() << std::endl;
456 
457 
458  cTab->cd(1);
459  if(gCX->GetN()>0) gCX->Draw("Psame");
460  if(gTX->GetN()>0) gTX->Draw("Psame");
461  if(gTXnoise->GetN()>0) gTXnoise->Draw("Psame");
462  cTab->cd(2);
463  if(gCY->GetN()>0) gCY->Draw("Psame");
464  if(gTY->GetN()>0) gTY->Draw("Psame");
465  if(gTYnoise->GetN()>0) gTYnoise->Draw("Psame");
466 
467  cTab->Update();
468 }
469 
470 
471 
472 
473 
474 
475 
477  {
478 
479  for(Int_t i = 0; i<NALL; i++) fBadCh[i] = 0;
480 
481  fNcl = 0;
482 
483  fThr = 3;
484  Double_t thr;
485  if ( !gHConfig->Lookup("Clustering.Thr",thr) )
486  Info("Init","Use default Threshold %g",fThr);
487  else{
488  fThr = thr;
489  Info("Init","Use Threshold %g",fThr);
490  }
491 
492  fNoiseThr = 3;
493  Double_t noiseThr;
494  if ( !gHConfig->Lookup("Clustering.NoiseThr",noiseThr) )
495  Info("Init","Use default Noise Threshold %g",fNoiseThr);
496  else{
497  fNoiseThr = noiseThr;
498  Info("Init","Use Noise Threshold %g",fNoiseThr);
499  }
500 
501 
502  fHist[0] = 0;
503  fHist[1] = 0;
504  fCanvas[0] = 0;
505  fCanvas[1] = 0;
506 
507  fXstart = -1000;
508  fTstart = -1000;
509  }
510 
511 void HarpoClustering::Save(char * /* mode */)
512  {
513 
514  // TString * hstFile = gHConfig->GetHistFile();
515  // if ( hstFile == NULL ) {
516  // Info("Save","No Hist File name given, use default");
517  // hstFile = new TString(Form("outputs/anaph%lli.root",gHConfig->GetRunNo()));
518  // Info("Save","No Hist File name given, use default %s",hstFile->Data());
519  // }
520 
521  // TFile *hf = new TFile(hstFile->Data(),"RECREATE");
522  // // Save histograms here
523  // hf->Close();
524  // printf("fNewFile %s closed\n", hstFile->Data() );
525 
526  }
527 
#define NALL
Definition: HarpoDccMap.h:15
void SetX(Double_t val)
HarpoRecoEvent * GetRecoEvent()
Definition: HarpoEvent.h:46
void FindCluster(HarpoDccMap *m, Int_t type, Int_t plane)
Cluster finder.
#define XPLANE
Definition: HarpoConfig.h:24
TVirtualPad * fCanvas[2]
void SetXmax(Int_t val)
#define NCHAN
Definition: HarpoDccMap.h:16
void SetQtotXEvt(Int_t val)
Double_t GetMean()
void SetYmin(Int_t val)
Double_t GetData(Int_t i, Int_t j)
Set/Get Data Cell.
Definition: HarpoDccMap.cxx:84
void SetNclXEvt(Int_t val)
Bool_t isExist(ULong_t det)
Detecror date exist //! Number of Real Detectors.
Definition: HarpoDetSet.h:33
Object containing the reconstruction information for one event (with array of HarpoRecoClusters Harpo...
void SetNclYEvt(Int_t val)
void SetYmax(Int_t val)
void DisplayAnalysis(TRootEmbeddedCanvas *ecTab, TGListBox *infobox)
void Save(char *mode=NULL)
virtual void print()
FullEvent Header not scecific to the detectors The class is ....
void AddClusters(HarpoRecoClusters *val)
void SetZ(Double_t val)
virtual void print()
void SetNTclXEvt(Int_t val)
Cluster object, containing position, charge and quality information.
static int type
unpacked dcc data The class contains the data map for DCC or Feminos The data is stored as a 2D TMatr...
Definition: HarpoDccMap.h:29
void SetNTclYEvt(Int_t val)
#define NADC
Definition: HarpoDccMap.h:18
Int_t fBadCh[NALL]
#define TCLUSTER
Long64_t gHarpoDebug
Definition: HarpoDebug.cxx:9
#define YPLANE
Definition: HarpoConfig.h:25
void ResetClustersArray()
Bool_t Lookup(const char *path, Bool_t &val)
Lookup function for scalar values.
void SetTstart(Double_t val)
HarpoEventHeader * GetHeader()
Definition: HarpoEvent.cxx:80
void SetTmax(Int_t val)
HarpoEvent * fEvt
Definition: HarpoAnalyse.h:70
ULong_t nEvents
Definition: HarpoAnalyse.h:75
void SetTmin(Int_t val)
const ULong_t gkNDetectors
Definition: HarpoDet.h:14
#define CCLUSTER
HarpoConfig * gHConfig
void SetNCclYEvt(Int_t val)
HarpoDccMap * GetDccMap(Long_t plane=XDCC)
Definition: HarpoEvent.cxx:108
Double_t GetSigmaNoise(Int_t i)
Definition: HarpoDccMap.cxx:68
void SetXmin(Int_t val)
void SetYstart(Double_t val)
void SetQtotYEvt(Int_t val)
Clustering algorithm, runs on RAW data (HarpoMap), produces HarpoRecoClusters objects.
TClonesArray * GetClustersArray()
void SetXstart(Double_t val)
R__EXTERN HarpoDetSet * gHDetSet
Definition: HarpoDetSet.h:71
void SetNCclXEvt(Int_t val)