source: JEM-EUSO/esaf_lal/tags/v1_r0/esaf/packages/simulation/detector/electronics/src/CellTrackingTrgEngine.cc @ 117

Last change on this file since 117 was 117, checked in by moretto, 11 years ago

ESAF version compilable on mac OS

File size: 18.4 KB
Line 
1// ESAF : Euso Simulation and Analysis Framework
2// $Id: CellTrackingTrgEngine.cc 2920 2011-06-11 06:46:41Z mabl $
3// M. Pallavicini created Oct, 24 2003
4
5#include "CellTrackingTrgEngine.hh"
6#include "MacroCellData.hh"
7#include "TROOT.h"
8#include "TMarker.h"
9#include "TBox.h"
10#include "TCanvas.h"
11#include "TLine.h"
12#include "MacroCell.hh"
13#include "MacroCellHit.hh"
14#include "FrontEndChip.hh"
15#include "Photomultiplier.hh"
16#include "PmtGeometry.hh"
17#include <string>
18
19
20ClassImp(CellTrackingTrgEngine)
21
22//______________________________________________________________________________
23CellTrackingTrgEngine::CellTrackingTrgEngine() : 
24    TriggerEngine(string("CellTrackingEngine"),kCellTrackingTrigger ) {
25    //
26    // Constructor
27    //
28}
29
30
31//______________________________________________________________________________
32CellTrackingTrgEngine::~CellTrackingTrgEngine() {
33    //
34    // Destructor
35    //
36
37
38}
39
40//______________________________________________________________________________
41void CellTrackingTrgEngine::Simulate( MacroCellData *pData ) {
42    //
43    // Do simulation
44    //
45
46
47    //get side of macrocell from config file --> alter to read from object
48    Int_t l=(Int_t)Conf()->GetNum("CellTrackingTrgEngine.MacroSide");
49    Bool_t silent=(Bool_t)Conf()->GetNum("CellTrackingTrgEngine.Silent");
50    if (gROOT->IsBatch()) silent=true;
51    Bool_t debug=(Bool_t)Conf()->GetNum("CellTrackingTrgEngine.Debug");
52
53
54    //catch a specific macrocell
55    //  if (silent) cout << "M#" <<  pData->Cell()->Id()<<" "<<flush;
56    if (!silent) 
57        cout << "MACROCELL # "<<pData->Cell()->Id() <<" num hits="<<pData->NumMacroCellHits()<<endl;
58
59    // if (pData->Cell()->Id() !=1) return;
60
61
62    //catch macrocell not empty
63    if (pData->IsEmpty())
64    {
65        //cout << "nothing to do" << endl;
66        return;
67    }
68
69
70
71    //__________________________________OBJECTS
72
73    //create object to contain Wired or
74
75    //X
76    vector<vector<Bool_t> > WiredXGTU; 
77    vector<Bool_t> WiredX;
78    //Y
79    vector<vector<Bool_t> > WiredYGTU; 
80    vector<Bool_t> WiredY;
81
82    //________________________________________
83
84    MacroCell *MCPARENT = pData->Cell();                     //Parent Macrocell Pointer
85    if (debug) cout<< "1"<<endl;
86
87    //silent variables (display)
88    Int_t minX;
89    Int_t maxX;
90    Int_t minY;
91    Int_t maxY;
92    TCanvas* MCD(0);
93    map<Int_t,TMarker*> PMpix;
94
95    if (debug) cout<< "2"<<endl;
96    if (!silent){
97        //DISPLAY macrocell
98
99        //create canvas
100        char title [50];
101        sprintf(title,"Track Find Trigger - Macrocell #%d  Display",pData->Cell()->Id());
102
103        MCD=new TCanvas("MCDisplay",title,1100,800);
104
105
106        //draw macrocell according to PMTs
107
108        const vector<Photomultiplier*>* PMTS =MCPARENT->Pmts();
109        //container for pixels : map (UID pixel) vs Pixel
110        //silent  map<Int_t,TMarker*> PMpix;
111
112
113        // get min & max for canvas: start in PMO Pix0
114
115        minX=(Int_t)MCPARENT->GetPmt(0)->Geometry()->Position(0)[0]-10;
116        maxX=(Int_t)MCPARENT->GetPmt(0)->Geometry()->Position(0)[0]+10;
117        minY=(Int_t)MCPARENT->GetPmt(0)->Geometry()->Position(0)[1]-10;
118        maxY=(Int_t)MCPARENT->GetPmt(0)->Geometry()->Position(0)[1]+10;
119
120
121        //loop in PMTS
122        for (Int_t PMTS_i=0;PMTS_i<(Int_t)PMTS->size(); PMTS_i++)
123        {
124            //get geometry
125            Photomultiplier *PMT=(*PMTS)[PMTS_i];
126            PmtGeometry* PMgeo= PMT->Geometry();
127
128            //loop in pixels
129
130            for (Int_t PMch=0;PMch<36;PMch++){
131                double pixX=PMgeo->Position(PMch)[0];
132                double pixY=PMgeo->Position(PMch)[1];
133                if (minX>pixX) minX=(Int_t)pixX-20;
134                if (minY>pixY) minY=(Int_t)pixY-20;
135                if (maxX<pixX) maxX=(Int_t)pixX+20;
136                if (maxY<pixY) maxY=(Int_t)pixY+20;
137
138                PMpix[PMgeo->GetUniqueId(PMch)]= new TMarker(pixX,pixY,8);
139
140                //       cout <<"*** "<<PMch << " x="<<  pixX << " y="<<pixY<<endl;
141                PMpix[PMgeo->GetUniqueId(PMch)]->Draw(); 
142            }
143        }
144        MCD->Range(minX,minY,maxX,maxY);
145
146        gPad->SetFillColor(38);
147
148    }//end draw macrocell
149
150    if (debug) cout<< "3"<<endl;
151    for(Int_t gtu = pData->GtuStart(); gtu < pData->GtuEnd(); gtu++) //loop nos GTU
152    {
153
154        vector<MacroCellHit*>* hitinhos =pData->Hits(gtu);
155
156        //clean wired or for this GTU
157        WiredX.assign(l,false);
158        WiredY.assign(l,false);   
159
160
161        for (unsigned int hi=0;hi < hitinhos->size();hi++) //loop in hits
162        {
163            //construct pixels of macrocell
164            MacroCellHit *hitin = (*hitinhos)[hi];
165            Int_t C= hitin->Col();
166            Int_t R= hitin->Row();
167
168            //update wired or
169            WiredX[C]=true;
170            WiredY[R]=true;
171            if (debug) cout<< "3.5"<<endl;         
172            if (!silent){ //draw HITS
173                Int_t ID = MCPARENT->GetUniqueIdRowCol(R,C);
174                PMpix[ID]->SetMarkerColor(PMpix[ID]->GetMarkerColor()+1);
175                PMpix[ID]->Draw();
176            }//end draw hists
177        };
178        WiredXGTU.push_back(WiredX);
179        WiredYGTU.push_back(WiredY);
180        if (debug) cout<< "4"<<endl;   
181
182        //to check buildup of signal
183        //cout<<gtu<<flush;
184        if (!silent) 
185            MCD->Update();
186        //getchar();
187        if (debug) cout<< "4.1"<<endl;
188    }
189
190
191    if (!silent){ //DISPLAY
192        //    MCD->Range(minX,minY,maxX,maxY);
193
194        gPad->SetFillColor(38);
195        MCD->Draw();
196        gPad->WaitPrimitive();
197
198        if (debug) cout<< "4.2"<<endl;
199        //test loop for lines and columns
200        for (Int_t line=1;line<49;line++)  {
201            for (Int_t column=1;column<49;column++)
202            {
203                //cout << "l= "<<line << " c= "<<column <<endl;
204                Int_t IDTest = MCPARENT->GetUniqueIdRowCol(line,column);
205                if (debug) cout<< "4.3"<<endl;
206                if( PMpix.count(IDTest) == 0 ) continue;
207                PMpix[IDTest]->SetMarkerStyle(28);
208                PMpix[IDTest]->Draw();
209                // MCD->Update();
210                //       getchar();
211                PMpix[IDTest]->SetMarkerStyle(24);
212                PMpix[IDTest]->Draw();
213            }
214            if (debug) cout<< "4.4"<<endl;
215            MCD->Update();
216        }
217        if (debug) cout<< "4.5"<<endl;
218    }//DISPLAY
219
220    //    return;
221
222    if (debug) cout<< "5"<<endl;
223
224
225
226
227
228    // GTU number
229    Int_t nGTU = WiredXGTU.size();
230    if (nGTU==0) {cout<< "######CELLTRACKING NGTU=0"<<endl; return;}
231    if (!silent) {//display  projections:
232        //X
233        TCanvas* Xproj=new TCanvas("XwiredOr","Track Find Trigger - X WiredOr",1100,800);
234        Xproj->Range(-5,-5,l+5,nGTU+5);
235        vector<vector<TMarker*> > pixWXGTU; //Pixels WXGTU
236        vector<TMarker*> WX(l);       //Pixels WX
237        //loop in wired or
238        for (Int_t G=0;G<(Int_t)WiredXGTU.size();G++)
239        {
240            for (Int_t CC=0;CC<(Int_t)WiredXGTU[G].size();CC++)
241            {
242                WX[CC]=new  TMarker(CC,G,24);
243
244                if (WiredXGTU[G][CC]) {
245                    WX[CC]->SetMarkerColor(2);
246                    WX[CC]->SetMarkerStyle(20);
247                }
248                WX[CC]->Draw();
249
250            }
251            pixWXGTU.push_back(WX);
252        }
253
254        if (debug) cout<< "6"<<endl;
255        //Y
256        TCanvas* Yproj=new TCanvas("YwiredOr","Track Find Trigger - Y WiredOr",1100,800);
257        Yproj->Range(-5,-5,l+5,nGTU+5);
258        vector<vector<TMarker*> > pixWYGTU; //Pixels WYGTU
259        vector<TMarker*> WY(l);       //Pixels WY
260        //loop in wired or
261        for (Int_t G=0;G<(Int_t)WiredYGTU.size();G++)
262        {
263            for (Int_t RR=0;RR<(Int_t)WiredYGTU[G].size();RR++)
264            {
265                WY[RR]= new TMarker(RR,G,24);
266                if (WiredYGTU[G][RR]) 
267                {
268                    WY[RR]->SetMarkerColor(2);
269                    WY[RR]->SetMarkerStyle(20);
270                }
271                WY[RR]->Draw();
272            }
273            pixWYGTU.push_back(WY);
274        }
275    }//display project
276
277    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
278    //+++++               Start the algorithm               ++++++
279    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
280    if (debug) cout<< "7"<<endl;
281    //variables
282    // C register
283    vector <Bool_t> CX;  // *Macrocell Side
284    vector <Bool_t> CY;
285    //next -> DX,DY
286    vector <Bool_t> DX;
287    vector <Bool_t> DY;
288
289    // L register
290    vector <Bool_t> LX;
291    vector <Bool_t> LY;
292    //next -> DX,DY
293    vector <Bool_t> MX;
294    vector <Bool_t> MY;   
295    //stair left
296    vector<vector<Bool_t> > SLX;
297    vector<vector<Bool_t> > SLY;
298
299    // R register
300    vector <Bool_t> RX;
301    vector <Bool_t> RY;
302    //next -> DX,DY
303    vector <Bool_t> SX;
304    vector <Bool_t> SY;
305    //stair Right
306    vector<vector<Bool_t> > SRX;
307    vector<vector<Bool_t> > SRY;
308
309    // Time register
310    vector <Bool_t> XT;
311    vector <Bool_t> YT;
312    vector <Bool_t> XYT;
313
314    //auxiliar:
315    // number of GTUS      --> nGTU
316    // side of macrocell:  -->l
317    // X projections X GTU -->WiredXGTU
318    // Y projections X GTU -->WiredYGTU
319
320    if (debug) cout<< "7.5"<<endl;
321    if (debug) cout<< "nGTU="<<nGTU<<endl;
322    //prepare the time registers
323    XT.assign(nGTU-1,false);
324    if (debug) cout<< "7.51"<<endl;
325    YT.assign(nGTU-1,false);
326    if (debug) cout<< "7.52"<<endl;
327    XYT.assign(nGTU-1,false);
328    if (debug) cout<< "8"<<endl;
329    // trigger loop
330    //__________________________________________________   
331    //CX register
332    CX=WiredXGTU[0];
333
334    for (Int_t cGTU=1 ; cGTU<nGTU ; cGTU++) 
335    {
336
337        DX = WiredXGTU[cGTU];
338
339        //print cx
340        //dbg   cout<<endl <<" ## CX=";
341        //dbg for (Int_t i1=0;i1<CX.size();i1++) cout << CX[i1];
342        //dbg cout <<" <-- CX"<< endl;
343
344        //print dx
345        //dbg cout<<endl <<" ## DX=";
346        //dbg for (Int_t i2=0;i2<DX.size();i2++) cout << DX[i2];
347        //dbg cout <<" <-- DX"<< endl;
348
349
350        CX =  VecBitAnd(DX,CX);
351
352        //print cxafter -->re
353        //dbg cout<<endl <<" ## CX=";
354        //dbg for (Int_t i3=0;i3<CX.size();i3++) cout << CX[i3];
355        //dbg cout <<" <-- result"<< endl;
356
357        //dbg cout << "ISEMPTY? --> "<< VecEmpty(CX)<< endl;
358        if (!VecEmpty(CX))
359            CX = DX;
360        else XT[cGTU-1]=true;
361
362
363
364
365        //dbg getchar();
366    }
367    if (debug) cout<< "9"<<endl;   
368    //__________________________________________________
369    //LX register
370
371    //      shift to the left;"create stairway register"
372    //the shift is circular then a line to the right is a center line
373    SLX=WiredXGTU;
374    for (Int_t SL1=1;SL1<(Int_t)SLX.size();SL1++)
375    {
376        for (Int_t SL2=SL1;SL2<(Int_t)SLX.size();SL2++)
377        {
378            SLX[SL2]=VecShift(SLX[SL2],-1);
379        }
380    }
381
382    //detect the center line on SLX register
383    LX=SLX[0];
384
385    for (Int_t cGTU=1 ; cGTU<nGTU ; cGTU++) 
386    {
387
388        MX = SLX[cGTU];
389
390        //print cx
391        //dbg   cout<<endl <<" ## CX=";
392        //dbg for (Int_t i1=0;i1<CX.size();i1++) cout << CX[i1];
393        //dbg cout <<" <-- CX"<< endl;
394
395        //print dx
396        //dbg cout<<endl <<" ## DX=";
397        //dbg for (Int_t i2=0;i2<DX.size();i2++) cout << DX[i2];
398        //dbg cout <<" <-- DX"<< endl;
399
400
401        LX =  VecBitAnd(MX,LX);
402
403        //print cxafter -->re
404        //dbg cout<<endl <<" ## CX=";
405        //dbg for (Int_t i3=0;i3<CX.size();i3++) cout << CX[i3];
406        //dbg cout <<" <-- result"<< endl;
407
408        //dbg cout << "ISEMPTY? --> "<< VecEmpty(CX)<< endl;
409        if (!VecEmpty(LX))
410            LX = MX;
411        else XT[cGTU-1]=true;
412
413        //dbg getchar();
414    }
415
416    if (debug) cout<< "10"<<endl;
417
418    //____________________________________________________________________
419    //RX register
420
421    //      shift to the RIGHT;"create stairway register"
422    //the shift is circular then a line to the LEFT is a center line
423    SRX=WiredXGTU;
424    for (Int_t SR1=1;SR1<(Int_t)SRX.size();SR1++)
425    {
426        for (Int_t SR2=SR1;SR2<(Int_t)SRX.size();SR2++)
427        {
428            SRX[SR2]=VecShift(SRX[SR2],1);
429        }
430    }
431
432    //detect the center line on SRX register
433    RX=SRX[0];
434
435    for (Int_t cGTU=1 ; cGTU<nGTU ; cGTU++) 
436    {
437
438        SX = SRX[cGTU];
439
440        //print cx
441        //dbg   cout<<endl <<" ## CX=";
442        //dbg for (Int_t i1=0;i1<CX.size();i1++) cout << CX[i1];
443        //dbg cout <<" <-- CX"<< endl;
444
445        //print dx
446        //dbg cout<<endl <<" ## DX=";
447        //dbg for (Int_t i2=0;i2<DX.size();i2++) cout << DX[i2];
448        //dbg cout <<" <-- DX"<< endl;
449
450
451        RX =  VecBitAnd(SX,RX);
452
453        //print cxafter -->re
454        //dbg cout<<endl <<" ## CX=";
455        //dbg for (Int_t i3=0;i3<CX.size();i3++) cout << CX[i3];
456        //dbg cout <<" <-- result"<< endl;
457
458        //dbg cout << "ISEMPTY? --> "<< VecEmpty(CX)<< endl;
459        if (!VecEmpty(RX))
460            RX = SX;
461        else XT[cGTU-1]=true;
462
463        //dbg getchar();
464    }
465
466    if (debug) cout<< "11"<<endl;
467    //________________________________________________________
468    //CY register
469
470    CY=WiredYGTU[0];
471
472    for (Int_t cGTU=1 ; cGTU<nGTU ; cGTU++) 
473    {
474        DY = WiredYGTU[cGTU];
475        CY = VecBitAnd(DY, CY);
476        if (!VecEmpty(CY))
477            CY = DY;
478        else YT[cGTU-1]=true;
479    } 
480    if (debug) cout<< "12"<<endl;
481    //__________________________________________________
482    //LY register
483
484    //      shift to the left;"create stairway register"
485    //the shift is circular then a line to the right is a center line
486    SLY=WiredYGTU;
487    for (Int_t SL1=1;SL1<(Int_t)SLY.size();SL1++)
488    {
489        for (Int_t SL2=SL1;SL2<(Int_t)SLY.size();SL2++)
490        {
491            SLY[SL2]=VecShift(SLY[SL2],-1);
492        }
493    }
494
495    //detect the center line on SLY register
496    LY=SLY[0];
497
498    for (Int_t cGTU=1 ; cGTU<nGTU ; cGTU++) 
499    {
500
501        MY = SLY[cGTU];
502
503        //print cx
504        //dbg   cout<<endl <<" ## CX=";
505        //dbg for (Int_t i1=0;i1<CX.size();i1++) cout << CX[i1];
506        //dbg cout <<" <-- CX"<< endl;
507
508        //print dx
509        //dbg cout<<endl <<" ## DX=";
510        //dbg for (Int_t i2=0;i2<DX.size();i2++) cout << DX[i2];
511        //dbg cout <<" <-- DX"<< endl;
512
513
514        LY =  VecBitAnd(MY,LY);
515
516        //print cxafter -->re
517        //dbg cout<<endl <<" ## CX=";
518        //dbg for (Int_t i3=0;i3<CX.size();i3++) cout << CX[i3];
519        //dbg cout <<" <-- result"<< endl;
520
521        //dbg cout << "ISEMPTY? --> "<< VecEmpty(CX)<< endl;
522        if (!VecEmpty(LY))
523            LY = MY;
524        else YT[cGTU-1]=true;
525
526        //dbg getchar();
527    }
528
529
530    if (debug) cout<< "13"<<endl;
531    //____________________________________________________________________
532    //RY register
533
534    //      shift to the RIGHT;"create stairway register"
535    //the shift is circular then a line to the LEFT is a center line
536    SRY=WiredYGTU;
537    for (Int_t SR1=1;SR1<(Int_t)SRY.size();SR1++)
538    {
539        for (Int_t SR2=SR1;SR2<(Int_t)SRY.size();SR2++)
540        {
541            SRY[SR2]=VecShift(SRY[SR2],1);
542        }
543    }
544
545    //detect the center line on SRY register
546    RY=SRY[0];
547
548    for (Int_t cGTU=1 ; cGTU<nGTU ; cGTU++) 
549    {
550
551        SY = SRY[cGTU];
552
553        //print cx
554        //dbg   cout<<endl <<" ## CX=";
555        //dbg for (Int_t i1=0;i1<CX.size();i1++) cout << CX[i1];
556        //dbg cout <<" <-- CX"<< endl;
557
558        //print dx
559        //dbg cout<<endl <<" ## DX=";
560        //dbg for (Int_t i2=0;i2<DX.size();i2++) cout << DX[i2];
561        //dbg cout <<" <-- DX"<< endl;
562
563
564        RY =  VecBitAnd(SY,RY);
565
566        //print cxafter -->re
567        //dbg cout<<endl <<" ## CX=";
568        //dbg for (Int_t i3=0;i3<CX.size();i3++) cout << CX[i3];
569        //dbg cout <<" <-- result"<< endl;
570
571        //dbg cout << "ISEMPTY? --> "<< VecEmpty(CX)<< endl;
572        if (!VecEmpty(RY))
573            RY = SY;
574        else YT[cGTU-1]=true;
575
576        //dbg getchar();
577    }
578    if (debug) cout<< "14"<<endl;
579    //__________________________________________________
580    //generate the trigger if "YT and XT" contain more than "n" consecutive "true". n is the persistency
581
582    // XT and YT
583    XYT=VecBitAnd(XT,YT);
584
585
586    if (!silent) 
587        for (Int_t temp1=0 ; temp1<(Int_t) YT.size() ; temp1++) 
588        {cout << "## "<< temp1<< " -XT-> "<< XT[temp1] << " -YT-> "<<YT[temp1]<< " -XYT-> "<< XYT[temp1]<<endl;}
589
590
591
592    //count the XYT
593    Int_t TrackTh=(Int_t)Conf()->GetNum("CellTrackingTrgEngine.Persistency")-1;
594    Int_t AuxCount=0;
595
596    if (!silent) cout<<endl<<"Required Track «"<<TrackTh<<"»"<<endl;
597    for (Int_t temp2=0 ; temp2<(Int_t) XYT.size() ; temp2++) 
598    {
599
600        AuxCount= (AuxCount+(Int_t) XYT[temp2])*(Int_t)XYT[temp2];
601        if (!silent) cout<< AuxCount;
602        if (AuxCount >= TrackTh) 
603        {
604            SetTrigger();//GENERATE THE TRIGGER
605            SetGtuTrigger(  pData->GtuStart()+1+temp2 );
606            cout << endl<<"=== TRIGGER! Cell:" << pData->Cell()->Id() 
607                << " Gtu:" << pData->GtuStart()+1+temp2  << " Engine:" << GetName() << endl;
608            return;
609        }
610    }
611
612    if (debug) cout<< "15"<<endl; 
613
614}
615
616//______________________________________________________________________________
617vector<Bool_t> CellTrackingTrgEngine::VecBitAnd(vector<Bool_t> A, vector<Bool_t> B) {
618    //
619    // routine to and registers
620    //
621
622  //test the registers
623  if (A.size() != B.size() ) 
624    cout << "EERRRRRRRRRRRROOOOOOOOOOOOOOOOORRRRRRRRRRRRRRR in BitAnd in CelltrackingTrigger";
625
626    Int_t s = A.size();
627    vector<Bool_t> OUT;
628    OUT.assign(s,false);
629
630    for (Int_t i =0; i<s;i++)
631      {
632        OUT[i] = A[i] && B[i];
633      }
634
635    return OUT;
636}
637
638
639Bool_t CellTrackingTrgEngine::VecEmpty (vector<Bool_t> A) {
640    //
641    // Routine to check register is empty
642    //
643    Int_t s = A.size();
644    Bool_t OUT;
645    OUT=false;
646
647    for (Int_t i =0; i<s;i++)
648    {
649        OUT = A[i] || OUT;
650    }
651
652    return OUT;
653}
654
655
656
657//______________________________________________________________________________
658vector<Bool_t> CellTrackingTrgEngine::VecShift(vector<Bool_t> vec, Int_t shift)
659{
660    // Routine to shift register
661    //  0 - do not shift
662    //  1 - shift one to the right
663    // -1 - shift one to the left
664
665
666    //-1 shift left
667    //0 nothing
668    // 1 shift right
669    Bool_t temp;
670
671    switch(shift) {
672        case -1 : 
673            temp=vec[0];
674            for (Int_t i=1; i<(Int_t) vec.size();i++)
675                vec[i-1]=vec[i];
676            vec[vec.size()-1]=temp;
677            break;
678        case 0 :
679            break;
680        case 1 :
681            temp=vec[vec.size()-1];
682            for (Int_t i=(Int_t)vec.size()-1; i>-1;i--)
683                vec[i+1]=vec[i];
684            vec[0]=temp;
685            break;
686        default :   throw runtime_error("CellTracking: ERROR");
687
688    }
689    return vec;
690}
Note: See TracBrowser for help on using the repository browser.