1 ///**************************************************************************
2 // * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 // * Author: Alberto Pulvirenti. *
6 // * Permission to use, copy, modify and distribute this software and its *
7 // * documentation strictly for non-commercial purposes is hereby granted *
8 // * without fee, provided that the above copyright notice appears in all *
9 // * copies and that both the copyright notice and this permission notice *
10 // * appear in the supporting documentation. The authors make no claims *
11 // * about the suitability of this software for any purpose. *
12 // * It is provided "as is" without express or implied warranty. *
14 // * AN ITS STAND-ALONE "NEURAL" TRACK FINDER *
15 // * ---------------------------------------- *
16 // * This class implements the Denby-Peterson algorithm for track finding *
17 // * in the ITS stand-alone, by means of a neural network simulation. *
18 // * Many parameters have to be set for the neural network to operate *
19 // * correctly and with a good efficiency. *
20 // * The neural tracker must be feeded with a TTree filled with objects *
21 // * of the class "AliITSNeuralPoint", into a single branch called *
23 // **************************************************************************/
26 #include <Riostream.h>
34 //#include <TMarker.h>
39 //#include <TParticle.h>
40 #include <TObjArray.h>
43 #include "AliITSNeuralTracker.h"
45 class AliITSNeuralPoint;
47 //using namespace std;
48 ClassImp(AliITSNeuralTracker)
50 //--------------------------------------------------------------------------------------------
52 AliITSNeuralTracker::AliITSNeuralTracker()
56 // Initializes some data-members:
57 // - all pointers to NULL
58 // - theta cut to 180 deg. (= no theta cut)
59 // - initial choice of only 1 azimuthal sector
60 // - initial values for the neural network parameters.
62 // With these settings the neural tracker can't work
63 // because it has not any curvature cut set.
69 fSectorWidth = TMath::Pi() * 2.0;
70 fPolarInterval = 45.0;
72 fStructureOK = kFALSE;
74 fVX = fVY = fVZ = 0.0;
77 for (ilayer = 0; ilayer < 6; ilayer++) {
78 for (itheta = 0; itheta < 180; itheta++) fPoints[ilayer][itheta] = 0;
79 fThetaCut2DMin[ilayer] = 0.0;
80 fThetaCut2DMax[ilayer] = TMath::Pi();
81 fThetaCut3DMin[ilayer] = 0.0;
82 fThetaCut3DMax[ilayer] = TMath::Pi();
83 fHelixMatchCutMin[ilayer] = 1.0;
84 fHelixMatchCutMax[ilayer] = 1.0;
91 fStabThreshold = 0.001;
92 fGain2CostRatio = 1.0;
98 fChains = new TTree("TreeC", "Sextines of points");
99 fChains->Branch("l0", &fPoint[0], "l0/I");
100 fChains->Branch("l1", &fPoint[1], "l1/I");
101 fChains->Branch("l2", &fPoint[2], "l2/I");
102 fChains->Branch("l3", &fPoint[3], "l3/I");
103 fChains->Branch("l4", &fPoint[4], "l4/I");
104 fChains->Branch("l5", &fPoint[5], "l5/I");
107 //--------------------------------------------------------------------------------------------
109 AliITSNeuralTracker::~AliITSNeuralTracker()
113 // It Destroys all the dynamic arrays and
114 // clears the TCollections and the points tree
118 Int_t ilayer, itheta;
120 for (ilayer = 0; ilayer < 6; ilayer++) {
121 for (itheta = 0; itheta < 180; itheta++) {
122 fPoints[ilayer][itheta]->SetOwner();
123 delete fPoints[ilayer][itheta];
126 fNeurons->SetOwner();
132 //--------------------------------------------------------------------------------------------
134 void AliITSNeuralTracker::Display(TCanvas*& canv) const
136 // Displays the neural segments
138 Double_t x1, y1, x2, y2;
140 TObjArrayIter iter(fNeurons);
142 AliITSneuron *unit = (AliITSneuron*)iter.Next();
144 if (unit->Activation() < fActMinimum) continue;
145 x1 = unit->Inner()->X();
146 x2 = unit->Outer()->X();
147 y1 = unit->Inner()->Y();
148 y2 = unit->Outer()->Y();
149 TLine *line = new TLine(x1, y1, x2, y2);
156 //--------------------------------------------------------------------------------------------
158 void AliITSNeuralTracker::SetThetaCuts2D(Double_t *min, Double_t *max)
164 for (i = 0; i < 5; i++) {
165 if (min[i] > max[i]) {
170 fThetaCut2DMin[i] = min[i];
171 fThetaCut2DMax[i] = max[i];
173 for (i = 0; i < 5; i++) {
174 cout << "Theta 2D cut for layer " << i << " = " << fThetaCut2DMin[i] << " to " << fThetaCut2DMax[i] << endl;
178 //--------------------------------------------------------------------------------------------
180 void AliITSNeuralTracker::SetThetaCuts3D(Double_t *min, Double_t *max)
186 for (i = 0; i < 5; i++) {
187 if (min[i] > max[i]) {
192 fThetaCut3DMin[i] = min[i];
193 fThetaCut3DMax[i] = max[i];
195 for (i = 0; i < 5; i++) {
196 cout << "Theta 3D cut for layer " << i << " = " << fThetaCut3DMin[i] << " to " << fThetaCut3DMax[i] << endl;
200 //--------------------------------------------------------------------------------------------
202 void AliITSNeuralTracker::SetHelixMatchCuts(Double_t *min, Double_t *max)
208 for (i = 0; i < 5; i++) {
209 if (min[i] > max[i]) {
214 fHelixMatchCutMin[i] = min[i];
215 fHelixMatchCutMax[i] = max[i];
217 for (i = 0; i < 5; i++) {
218 cout << "Helix-match cut for layer " << i << " = " << fHelixMatchCutMin[i] << " to " << fHelixMatchCutMax[i] << endl;
222 //--------------------------------------------------------------------------------------------
224 void AliITSNeuralTracker::SetCurvatureCuts(Int_t ncuts, Double_t *curv)
226 // CURVATURE CUTS SETTER
228 // Requires an array of double values and its dimension
229 // After sorting it in increasing order, the array of curvature cuts
230 // is dinamically allocated, and filled with the sorted cuts array.
231 // A message is shown which lists all the curvature cuts.
233 Int_t i, *ind = new Int_t[ncuts];
234 TMath::Sort(ncuts, curv, ind, kFALSE);
235 fCurvCut = new Double_t[ncuts];
236 cout << "\n" << ncuts << " curvature cuts defined" << endl << "-----" << endl;
237 for (i = 0; i < ncuts; i++) {
238 fCurvCut[i] = curv[ind[i]];
239 cout << "Cut #" << i + 1 << " : " << fCurvCut[i] << endl;
241 cout << "-----" << endl;
245 //--------------------------------------------------------------------------------------------
247 void AliITSNeuralTracker::CreateArrayStructure(Int_t nsectors)
251 // Organizes the array structure to store all points in.
253 // The array is organized into a "multi-level" TCollection:
254 // - 6 fPoints[] TObjArray containing a TObjArray for each layer
255 // - each TObject contains a TObjArray for each sector.
257 // sets the number of sectors and their width.
258 fSectorNum = nsectors;
259 fSectorWidth = TMath::Pi() * 2.0 / (Double_t)fSectorNum;
261 // creates the TCollection structure
262 Int_t ilayer, isector, itheta;
263 TObjArray *sector = 0;
264 for (ilayer = 0; ilayer < 6; ilayer++) {
265 for (itheta = 0; itheta < 180; itheta++) {
266 if (fPoints[ilayer][itheta]) {
267 fPoints[ilayer][itheta]->SetOwner();
268 delete fPoints[ilayer][itheta];
270 fPoints[ilayer][itheta] = new TObjArray(nsectors);
271 for (isector = 0; isector < nsectors; isector++) {
272 sector = new TObjArray;
274 fPoints[ilayer][itheta]->AddAt(sector, isector);
279 // Sets a checking flag to TRUE.
280 // This flag is checked before filling up the arrays with the points.
281 fStructureOK = kTRUE;
284 //--------------------------------------------------------------------------------------------
286 Int_t AliITSNeuralTracker::ArrangePoints(TTree* ptstree)
288 // POINTS STORAGE INTO ARRAY
290 // Reads points from the tree and creates AliITSNode objects for each one,
291 // storing them into the array structure defined above.
292 // Returns the number of points collected (if successful) or 0 (otherwise)
294 // check: if the points tree is NULL or empty, there is nothing to do...
295 if ( !ptstree || (ptstree && !(Int_t)ptstree->GetEntries()) ) {
296 Error("ArrangePoints", "Points tree is NULL or empty: no points to arrange");
301 Error("ArrangePoints", "Structure NOT defined. Call CreateArrayStructure() first");
305 Int_t isector, itheta, ientry, ilayer, nentries, pos;
306 TObjArray *sector = 0;
307 AliITSNode *created = 0;
308 AliITSNeuralPoint *cursor = 0;
310 ptstree->SetBranchAddress("pos", &pos);
311 ptstree->SetBranchAddress("Points", &cursor);
312 nentries = (Int_t)ptstree->GetEntries();
314 for (ientry = 0; ientry < nentries; ientry++) {
315 ptstree->GetEntry(ientry);
316 // creates the object
317 created = new AliITSNode(cursor, kTRUE);
318 created->SetUser(-1);
319 created->PosInTree() = pos;
320 // finds the sector in phi
321 isector = created->GetSector(fSectorWidth);
322 itheta = created->GetThetaCell();
323 ilayer = created->GetLayer();
324 if (ilayer < 0 || ilayer > 5) {
325 Error("ArrangePoints", "Layer value %d not allowed. Aborting...", ilayer);
328 // selects the right TObjArray to store object into
329 sector = (TObjArray*)fPoints[ilayer][itheta]->At(isector);
330 sector->AddLast(created);
333 // returns the number of points processed
337 //--------------------------------------------------------------------------------------------
339 void AliITSNeuralTracker::PrintPoints()
341 // creates the TCollection structure
342 TObjArray *sector = 0;
343 Int_t ilayer, isector, itheta;
344 fstream file("test.log", ios::out);
345 for (ilayer = 0; ilayer < 6; ilayer++) {
346 for (isector = 0; isector < fSectorNum; isector++) {
347 for (itheta = 0; itheta < 180; itheta++) {
348 sector = (TObjArray*)fPoints[ilayer][itheta]->At(isector);
349 file << ilayer << " " << isector << " " << itheta;
350 file << " " << sector->GetSize() << " points" << endl;
357 //--------------------------------------------------------------------------------------------
359 Bool_t AliITSNeuralTracker::PassCurvCut
360 (AliITSNode *p1, AliITSNode *p2, Int_t curvindex, Double_t vx, Double_t vy, Double_t vz)
362 // CURVATURE CUT EVALUATOR
364 // Checks the passsed point pair w.r. to the current curvature cut
365 // Returns the result of the check.
367 if (curvindex < 0 || curvindex >= fCurvNum) {
368 Error("PassCurvCut", "Curv index %d out of range", curvindex);
372 // Find the reference layer
373 Int_t lay1 = p1->GetLayer();
374 Int_t lay2 = p2->GetLayer();
375 Int_t reflayer = (lay1 < lay2) ? lay1 : lay2;
377 Double_t x1 = p1->X() - vx;
378 Double_t x2 = p2->X() - vx;
379 Double_t y1 = p1->Y() - vy;
380 Double_t y2 = p2->Y() - vy;
381 Double_t z1 = p1->Z() - vz;
382 Double_t z2 = p2->Z() - vz;
383 Double_t r1 = sqrt(x1*x1 + y1*y1);
384 Double_t r2 = sqrt(x2*x2 + y2*y2);
386 // calculation of curvature
387 Double_t dx = p1->X() - p2->X(), dy = p1->Y() - p2->Y();
388 Double_t num = 2 * (x1*y2 - x2*y1);
389 Double_t den = r1*r2*sqrt(dx*dx + dy*dy);
393 curv = fabs(num / den);
394 if (curv > fCurvCut[curvindex]) return kFALSE;
402 curv = TMath::Abs(num / den);
403 if (curv > fCurvCut[curvindex]) return kFALSE;
407 // calculation of helix matching
408 Double_t arc1 = 2.0 * r1 * curv;
409 Double_t arc2 = 2.0 * r2 * curv;
410 Double_t helmatch = 0.0;
411 if (arc1 > -1.0 && arc1 < 1.0) arc1 = asin(arc1);
412 else arc1 = ((arc1 > 0.0) ? 0.5 : 1.5) * TMath::Pi();
413 if (arc2 > -1.0 && arc2 < 1.0) arc2 = asin(arc2);
414 else arc2 = ((arc2 > 0.0) ? 0.5 : 1.5) * TMath::Pi();
417 if (arc1 == 0.0 || arc2 == 0.0) return kFALSE;
418 helmatch = TMath::Abs(z1 / arc1 - z2 / arc2);
419 return (helmatch >= fHelixMatchCutMin[reflayer] && helmatch <= fHelixMatchCutMax[reflayer]);
423 //--------------------------------------------------------------------------------------------
425 Int_t AliITSNeuralTracker::PassAllCuts
426 (AliITSNode *p1, AliITSNode *p2, Int_t curvindex, Double_t vx, Double_t vy, Double_t vz)
428 // GLOBAL CUT EVALUATOR
430 // Checks all cuts for the passed point pair.
433 // 0 - All cuts passed
434 // 1 - theta 2D cut not passed
435 // 2 - theta 3D cut not passed
436 // 3 - curvature calculated but cut not passed
437 // 4 - curvature not calculated (division by zero)
438 // 5 - helix cut not passed
439 // 6 - curvature inxed out of range
441 if (curvindex < 0 || curvindex >= fCurvNum) return 6;
443 // Find the reference layer
444 Int_t lay1 = p1->GetLayer();
445 Int_t lay2 = p2->GetLayer();
446 Int_t reflayer = (lay1 < lay2) ? lay1 : lay2;
448 // Swap points in order that r1 < r2
449 AliITSNode *temp = 0;
450 if (p2->GetLayer() < p1->GetLayer()) {
456 // shift XY coords to the reference to the vertex position,
457 // for easier calculus of quantities.
458 Double_t x1 = p1->X() - vx;
459 Double_t x2 = p2->X() - vx;
460 Double_t y1 = p1->Y() - vy;
461 Double_t y2 = p2->Y() - vy;
462 Double_t z1 = p1->Z() - vz;
463 Double_t z2 = p2->Z() - vz;
464 Double_t r1 = sqrt(x1*x1 + y1*y1);
465 Double_t r2 = sqrt(x2*x2 + y2*y2);
467 // Check for theta cut
468 Double_t dtheta, dtheta3;
469 TVector3 v01(z1, r1, 0.0);
470 TVector3 v12(z2 - z1, r2 - r1, 0.0);
471 dtheta = v01.Angle(v12) * 180.0 / TMath::Pi();
472 TVector3 vv01(x1, y1, z1);
473 TVector3 vv12(x2 - x1, y2 - y1, z2 - z1);
474 dtheta3 = vv01.Angle(vv12) * 180.0 / TMath::Pi();
475 if (dtheta < fThetaCut2DMin[reflayer] || dtheta > fThetaCut2DMax[reflayer]) return 1;
476 if (dtheta3 < fThetaCut3DMin[reflayer] || dtheta3 > fThetaCut3DMax[reflayer]) return 2;
478 // calculation of curvature
479 Double_t dx = x1 - x2, dy = y1 - y2;
480 Double_t num = 2 * (x1*y2 - x2*y1);
481 Double_t den = r1*r2*sqrt(dx*dx + dy*dy);
484 curv = TMath::Abs(num / den);
485 if (curv > fCurvCut[curvindex]) return 3;
490 // calculation of helix matching
491 Double_t arc1 = 2.0 * r1 * curv;
492 Double_t arc2 = 2.0 * r2 * curv;
493 Double_t helmatch = 0.0;
494 if (arc1 > -1.0 && arc1 < 1.0) arc1 = asin(arc1);
495 else arc1 = ((arc1 > 0.0) ? 0.5 : 1.5) * TMath::Pi();
496 if (arc2 > -1.0 && arc2 < 1.0) arc2 = asin(arc2);
497 else arc2 = ((arc2 > 0.0) ? 0.5 : 1.5) * TMath::Pi();
500 if (arc1 == 0.0 || arc2 == 0.0) return kFALSE;
501 helmatch = TMath::Abs(z1 / arc1 - z2 / arc2);
502 if (helmatch < fHelixMatchCutMin[reflayer] || helmatch > fHelixMatchCutMax[reflayer]) return 5;
507 //--------------------------------------------------------------------------------------------
509 void AliITSNeuralTracker::StoreAbsoluteMatches()
511 // Stores in the 'fMatches' array of each node all the points in the
512 // adjacent layers which allow to create neurons accordin to the
513 // helix and theta cut, and also to the largest curvature cut
515 Int_t ilayer, isector, itheta1, itheta2, check;
516 TObjArray *list1 = 0, *list2 = 0;
517 AliITSNode *node1 = 0, *node2 = 0;
518 Double_t thetamin, thetamax;
521 for (isector = 0; isector < fSectorNum; isector++) {
522 // sector is chosen once for both lists
523 for (ilayer = 0; ilayer < 5; ilayer++) {
524 for (itheta1 = 0; itheta1 < 180; itheta1++) {
525 list1 = (TObjArray*)fPoints[ilayer][itheta1]->At(isector);
526 TObjArrayIter iter1(list1);
527 while ( (node1 = (AliITSNode*)iter1.Next()) ) {
528 if (node1->GetUser() >= 0) continue;
529 node1->Matches()->Clear();
530 thetamin = node1->ThetaDeg() - fPolarInterval;
531 thetamax = node1->ThetaDeg() + fPolarInterval;
532 imin = (Int_t)thetamin;
533 imax = (Int_t)thetamax;
534 if (imin < 0) imin = 0;
535 if (imax > 179) imax = 179;
536 for (itheta2 = imin; itheta2 <= imax; itheta2++) {
537 list2 = (TObjArray*)fPoints[ilayer + 1][itheta2]->At(isector);
538 TObjArrayIter iter2(list2);
539 while ( (node2 = (AliITSNode*)iter2.Next()) ) {
540 check = PassAllCuts(node1, node2, fCurvNum - 1, fVX, fVY, fVZ);
543 node1->Matches()->AddLast(node2);
546 //Info("StoreAbsoluteMatches", "Layer %d: THETA 2D cut not passed", ilayer);
549 //Info("StoreAbsoluteMatches", "Layer %d: THETA 3D cut not passed", ilayer);
552 //Info("StoreAbsoluteMatches", "Layer %d: CURVATURE cut not passed", ilayer);
555 //Info("StoreAbsoluteMatches", "Layer %d: Division by ZERO in curvature evaluation", ilayer);
558 //Info("StoreAbsoluteMatches", "Layer %d: HELIX-MATCH cut not passed", ilayer);
561 //Error("PassAllCuts", "Layer %d: Curv index out of range", ilayer);
564 Warning("StoreAbsoluteMatches", "Layer %d: %d: unrecognized return value", ilayer, check);
566 } // while (node2...)
567 } // for (itheta2...)
568 } // while (node1...)
571 } // for (isector...)
574 //--------------------------------------------------------------------------------------------
576 void AliITSNeuralTracker::PrintMatches(Bool_t stop)
578 // Prints the matches for each point
580 TFile *ft = new TFile("its_findables_v1.root");
581 TTree *tt = (TTree*)ft->Get("Tracks");
582 Int_t it, nP, nU, lab, nF = (Int_t)tt->GetEntries();
583 tt->SetBranchAddress("nhitsP", &nP);
584 tt->SetBranchAddress("nhitsU", &nU);
585 tt->SetBranchAddress("label", &lab);
586 TString strP("|"), strU("|");
587 for (it = 0; it < nF; it++) {
589 if (nP >= 5) strP.Append(Form("%d|", lab));
590 if (nU >= 5) strU.Append(Form("%d|", lab));
593 TObjArray *sector = 0;
594 Int_t ilayer, isector, itheta, id[3];
595 AliITSNode *node1 = 0, *node2 = 0;
597 for (ilayer = 0; ilayer < 6; ilayer++) {
598 for (isector = 0; isector < fSectorNum; isector++) {
599 for (itheta = 0; itheta < 180; itheta++) {
600 sector = (TObjArray*)fPoints[ilayer][itheta]->At(isector);
601 TObjArrayIter points(sector);
602 while ( (node1 = (AliITSNode*)points.Next()) ) {
603 for (it = 0; it < 3; it++) id[it] = node1->GetLabel(it);
604 nF = (Int_t)node1->Matches()->GetSize();
605 cout << "Node layer: " << node1->GetLayer() << ", labels: ";
606 cout << id[0] << " " << id[1] << " " << id[2] << " --> ";
608 cout << "NO MatchES!!!" << endl;
611 cout << nF << " Matches" << endl;
612 for (it = 0; it < 3; it++) {
613 if (strP.Contains(Form("|%d|", id[it])))
614 cout << "Belongs to findable (originary) track " << id[it] << endl;
615 if (strU.Contains(Form("|%d|", id[it])))
616 cout << "Belongs to findable (post-Kalman) track " << id[it] << endl;
618 TObjArrayIter matches(node1->Matches());
619 while ( (node2 = (AliITSNode*)matches.Next()) ) {
620 cout << "Match with " << node2;
621 Int_t *sh = node1->SharedID(node2);
622 for (Int_t k = 0; k < 3; k++)
623 if (sh[k] > -1) cout << " " << sh[k];
627 cout << "Press a key" << endl;
636 //--------------------------------------------------------------------------------------------
638 void AliITSNeuralTracker::ResetNodes(Int_t isector)
640 // Clears some utility arrays for each node
642 Int_t ilayer, itheta;
643 TObjArray *sector = 0;
644 AliITSNode *node = 0;
645 for (ilayer = 0; ilayer < 6; ilayer++) {
646 for (itheta = 0; itheta < 180; itheta++) {
647 sector = (TObjArray*)fPoints[ilayer][itheta]->At(isector);
648 TObjArrayIter iter(sector);
650 node = (AliITSNode*)iter.Next();
652 node->InnerOf()->Clear();
653 node->OuterOf()->Clear();
654 delete node->InnerOf();
655 delete node->OuterOf();
656 node->InnerOf() = new TObjArray;
657 node->OuterOf() = new TObjArray;
663 //--------------------------------------------------------------------------------------------
665 void AliITSNeuralTracker::NeuralTracking(const char* filesave, TCanvas*& display)
667 // This is the public method that operates the tracking.
668 // It works sector by sector, and at the end saves the found tracks.
669 // Other methods are privare because they have no meaning id used alone,
670 // and sometimes they could get segmentation faults due to uninitialized
671 // datamembert they require to work on.
672 // The argument is a file where the final results have to be stored.
674 Bool_t isStable = kFALSE;
675 Int_t i, nUnits = 0, nLinks = 0, nTracks = 0, sectTracks = 0, totTracks = 0;
677 // tracking through sectors
680 for (sector = 0; sector < fSectorNum; sector++) {
681 cout << "\rSector " << sector << ": " << endl;
683 for(curv = 0; curv < fCurvNum; curv++) {
684 cout << "- curvature step " << curv + 1;
685 cout << " (" << fCurvCut[curv] << "): ";
687 nUnits = CreateNeurons(sector, curv);
689 cout << "no neurons --> skipped" << endl;
692 cout << nUnits << " units, " << flush;
694 nLinks = LinkNeurons();
696 cout << "no connections --> skipped" << endl;
699 cout << nLinks << " connections, " << flush;
703 if (display) Display(display);
704 TObjArrayIter iter(fNeurons);
706 AliITSneuron *n = (AliITSneuron*)iter.Next();
711 cout << i << " cycles --> " << flush;
714 nTracks = Save(sector);
715 cout << nTracks << " tracks" << endl;
716 sectTracks += nTracks;
718 totTracks += sectTracks;
719 //cout << sectTracks << " tracks found (total = " << totTracks << ") " << flush;
722 cout << endl << totTracks << " tracks found!!!" << endl;
723 cout << endl << "Saving results in file " << filesave << "..." << flush;
724 TFile *f = new TFile(filesave, "recreate");
725 fChains->Write("TreeC");
729 //--------------------------------------------------------------------------------------------
731 Int_t AliITSNeuralTracker::CreateNeurons(Int_t sectoridx, Int_t curvidx)
733 // Fills the neuron arrays, following the cut criteria for the selected step
734 // (secnum = sector to analyze, curvnum = curvature cut step to use)
735 // It also sets the initial random activation.
736 // In order to avoid a large number of 'new' operations, all existing neurons
737 // are reset and initialized with the new values, and are created new unit only if
738 // it turns out to be necessary
739 // the 'flag' argument is used to decide if the lower edge in the intevral
740 // of the accepted curvatures is given by zero (kFALSE) or by the preceding used cut (kTRUE)
741 // (if this is the first step, anyway, the minimum is always zero)
743 ResetNodes(sectoridx);
745 if (fNeurons) delete fNeurons;
746 fNeurons = new TObjArray;
748 AliITSneuron *unit = 0;
749 Int_t itheta, neurons = 0;
750 TObjArray *lstsector = 0;
753 Double_t vx[6], vy[6], vz[6];
754 AliITSNode *p[6] = {0, 0, 0, 0, 0, 0};
755 for (itheta = 0; itheta < 180; itheta++) {
756 lstsector = (TObjArray*)fPoints[0][itheta]->At(sectoridx);
757 TObjArrayIter lay0(lstsector);
758 while ( (p[0] = (AliITSNode*)lay0.Next()) ) {
759 if (p[0]->GetUser() >= 0) continue;
763 TObjArrayIter lay1(p[0]->Matches());
764 while ( (p[1] = (AliITSNode*)lay1.Next()) ) {
765 if (p[1]->GetUser() >= 0) continue;
766 if (!PassCurvCut(p[0], p[1], curvidx, fVX, fVY, fVZ)) continue;
767 unit = new AliITSneuron;
768 unit->Inner() = p[0];
769 unit->Outer() = p[1];
770 unit->Activation() = gRandom->Rndm() * (fEdge1 - fEdge2) + fEdge2;
771 unit->Gain() = new TObjArray;
772 fNeurons->AddLast(unit);
773 p[0]->InnerOf()->AddLast(unit);
774 p[1]->OuterOf()->AddLast(unit);
779 TObjArrayIter lay2(p[1]->Matches());
780 while ( (p[2] = (AliITSNode*)lay2.Next()) ) {
781 if (p[2]->GetUser() >= 0) continue;
782 if (!PassCurvCut(p[1], p[2], curvidx, vx[1], vy[1], vz[1])) continue;
783 unit = new AliITSneuron;
784 unit->Inner() = p[1];
785 unit->Outer() = p[2];
786 unit->Activation() = gRandom->Rndm() * (fEdge1 - fEdge2) + fEdge2;
787 unit->Gain() = new TObjArray;
788 fNeurons->AddLast(unit);
789 p[1]->InnerOf()->AddLast(unit);
790 p[2]->OuterOf()->AddLast(unit);
795 TObjArrayIter lay3(p[2]->Matches());
796 while ( (p[3] = (AliITSNode*)lay3.Next()) ) {
797 if (p[3]->GetUser() >= 0) continue;
798 if (!PassCurvCut(p[2], p[3], curvidx, vx[2], vy[2], vz[2])) continue;
799 unit = new AliITSneuron;
800 unit->Inner() = p[2];
801 unit->Outer() = p[3];
802 unit->Activation() = gRandom->Rndm() * (fEdge1 - fEdge2) + fEdge2;
803 unit->Gain() = new TObjArray;
804 fNeurons->AddLast(unit);
805 p[2]->InnerOf()->AddLast(unit);
806 p[3]->OuterOf()->AddLast(unit);
811 TObjArrayIter lay4(p[3]->Matches());
812 while ( (p[4] = (AliITSNode*)lay4.Next()) ) {
813 if (p[4]->GetUser() >= 0) continue;
814 if (!PassCurvCut(p[3], p[4], curvidx, vx[3], vy[3], vz[3])) continue;
815 unit = new AliITSneuron;
816 unit->Inner() = p[3];
817 unit->Outer() = p[4];
818 unit->Activation() = gRandom->Rndm() * (fEdge1 - fEdge2) + fEdge2;
819 unit->Gain() = new TObjArray;
820 fNeurons->AddLast(unit);
821 p[3]->InnerOf()->AddLast(unit);
822 p[4]->OuterOf()->AddLast(unit);
827 TObjArrayIter lay5(p[4]->Matches());
828 while ( (p[5] = (AliITSNode*)lay5.Next()) ) {
829 if (p[5]->GetUser() >= 0) continue;
830 if (!PassCurvCut(p[4], p[5], curvidx, vx[4], vy[4], vz[4])) continue;
831 unit = new AliITSneuron;
832 unit->Inner() = p[4];
833 unit->Outer() = p[5];
834 unit->Activation() = gRandom->Rndm() * (fEdge1 - fEdge2) + fEdge2;
835 unit->Gain() = new TObjArray;
836 fNeurons->AddLast(unit);
837 p[4]->InnerOf()->AddLast(unit);
838 p[5]->OuterOf()->AddLast(unit);
847 // END OF NEW VERSION
850 for (ilayer = 0; ilayer < 6; ilayer++) {
851 for (itheta = 0; itheta < 180; itheta++) {
852 lstsector = (TObjArray*)fPoints[ilayer][itheta]->At(sectoridx);
853 TObjArrayIter inners(lstsector);
854 while ( (inner = (AliITSNode*)inners.Next()) ) {
855 if (inner->GetUser() >= 0) continue;
856 TObjArrayIter outers(inner->Matches());
857 while ( (outer = (AliITSNode*)outers.Next()) ) {
858 if (outer->GetUser() >= 0) continue;
859 if (!PassCurvCut(inner, outer, curvidx, fVX, fVY, fVZ)) continue;
860 unit = new AliITSneuron;
861 unit->Inner() = inner;
862 unit->Outer() = outer;
863 unit->Activation() = gRandom->Rndm() * (fEdge1 - fEdge2) + fEdge2;
864 unit->Gain() = new TObjArray;
865 fNeurons->AddLast(unit);
866 inner->InnerOf()->AddLast(unit);
867 outer->OuterOf()->AddLast(unit);
875 fNeurons->SetOwner();
881 Int_t AliITSNeuralTracker::LinkNeurons() const
883 // Creates the neural synapses among all neurons
884 // which share a point.
887 TObjArrayIter iter(fNeurons), *testiter;
888 AliITSneuron *neuron = 0, *test = 0;
890 neuron = (AliITSneuron*)iter.Next();
893 testiter = (TObjArrayIter*)neuron->Inner()->OuterOf()->MakeIterator();
895 test = (AliITSneuron*)testiter->Next();
897 neuron->Add2Gain(test, fGain2CostRatio, fAlignExponent);
901 testiter = (TObjArrayIter*)neuron->Outer()->InnerOf()->MakeIterator();
903 test = (AliITSneuron*)testiter->Next();
905 neuron->Add2Gain(test, fGain2CostRatio, fAlignExponent);
915 Bool_t AliITSNeuralTracker::Update()
917 // A complete updating cycle with the asynchronous method
918 // when the neural network is stable, kTRUE is returned.
920 Double_t actVar = 0.0, totDiff = 0.0;
921 TObjArrayIter iter(fNeurons);
924 unit = (AliITSneuron*)iter.Next();
926 actVar = Activate(unit);
927 // calculation the relative activation variation
930 totDiff /= fNeurons->GetSize();
931 return (totDiff < fStabThreshold);
936 void AliITSNeuralTracker::CleanNetwork()
938 // Removes all deactivated neurons, and resolves the competitions
939 // to the advantage of the most active unit
941 AliITSneuron *unit = 0;
942 TObjArrayIter neurons(fNeurons);
943 while ( (unit = (AliITSneuron*)neurons.Next()) ) {
944 if (unit->Activation() < fActMinimum) {
945 unit->Inner()->InnerOf()->Remove(unit);
946 unit->Outer()->OuterOf()->Remove(unit);
947 delete fNeurons->Remove(unit);
953 AliITSneuron *enemy = 0;
955 while ( (unit = (AliITSneuron*)neurons.Next()) ) {
956 nIn = (Int_t)unit->Inner()->InnerOf()->GetSize();
957 nOut = (Int_t)unit->Outer()->OuterOf()->GetSize();
958 if (nIn < 2 && nOut < 2) continue;
961 TObjArrayIter competing(unit->Inner()->InnerOf());
962 while ( (enemy = (AliITSneuron*)competing.Next()) ) {
963 if (unit->Activation() > enemy->Activation()) {
964 enemy->Inner()->InnerOf()->Remove(enemy);
965 enemy->Outer()->OuterOf()->Remove(enemy);
966 delete fNeurons->Remove(enemy);
969 unit->Inner()->InnerOf()->Remove(unit);
970 unit->Outer()->OuterOf()->Remove(unit);
971 delete fNeurons->Remove(unit);
976 if (removed) continue;
979 TObjArrayIter competing(unit->Outer()->OuterOf());
980 while ( (enemy = (AliITSneuron*)competing.Next()) ) {
981 if (unit->Activation() > enemy->Activation()) {
982 enemy->Inner()->InnerOf()->Remove(enemy);
983 enemy->Outer()->OuterOf()->Remove(enemy);
984 delete fNeurons->Remove(enemy);
987 unit->Inner()->InnerOf()->Remove(unit);
988 unit->Outer()->OuterOf()->Remove(unit);
989 delete fNeurons->Remove(unit);
1000 Bool_t AliITSNeuralTracker::CheckOccupation() const
1002 // checks if a track recognized has a point for each layer
1004 for (i = 0; i < 6; i++) if (fPoint[i] < 0) return kFALSE;
1010 Int_t AliITSNeuralTracker::Save(Int_t sectorid)
1012 // Creates chains of active neurons, in order to
1013 // find the tracks obtained as the neural network output.
1015 // every chain is started from the neurons in the first 2 layers
1016 Int_t i, check, stored = 0;
1017 Int_t a = sectorid; a = 0;
1018 Double_t testact = 0;
1019 AliITSneuron *unit = 0, *cursor = 0, *fwd = 0;
1020 AliITSNode *node = 0;
1021 TObjArrayIter iter(fNeurons), *fwditer;
1022 TObjArray *removedUnits = new TObjArray(0);
1023 TObjArray *removedPoints = new TObjArray(6);
1024 removedUnits->SetOwner(kFALSE);
1025 removedPoints->SetOwner(kFALSE);
1027 for (i = 0; i < 6; i++) fPoint[i] = -1;
1028 unit = (AliITSneuron*)iter.Next();
1030 if (unit->Inner()->GetLayer() > 0) continue;
1031 fPoint[unit->Inner()->GetLayer()] = unit->Inner()->PosInTree();
1032 fPoint[unit->Outer()->GetLayer()] = unit->Outer()->PosInTree();
1033 node = unit->Outer();
1034 removedUnits->AddLast(unit);
1035 removedPoints->AddAt(unit->Inner(), unit->Inner()->GetLayer());
1036 removedPoints->AddAt(unit->Outer(), unit->Outer()->GetLayer());
1038 testact = fActMinimum;
1039 fwditer = (TObjArrayIter*)node->InnerOf()->MakeIterator();
1042 cursor = (AliITSneuron*)fwditer->Next();
1044 if (cursor->Used()) continue;
1045 if (cursor->Activation() >= testact) {
1046 testact = cursor->Activation();
1051 removedUnits->AddLast(fwd);
1052 node = fwd->Outer();
1053 fPoint[node->GetLayer()] = node->PosInTree();
1054 removedPoints->AddAt(node, node->GetLayer());
1057 for (i = 0; i < 6; i++) if (fPoint[i] >= 0) check++;
1061 for (i = 0; i < 6; i++) {
1062 node = (AliITSNode*)removedPoints->At(i);
1065 fwditer = (TObjArrayIter*)removedUnits->MakeIterator();
1067 cursor = (AliITSneuron*)fwditer->Next();
1077 Int_t AliITSNeuralTracker::Save(Int_t sectoridx)
1078 // Creates chains of active neurons, in order to
1079 // find the tracks obtained as the neural network output.
1081 // Reads the final map of neurons and removes all
1082 // units with too small activation
1083 cout << "saving..." << flush;
1085 // every chain is started from the neurons in the first 2 layers
1086 // 00111111 00011111 00101111 00110111
1087 Int_t allowmask[] = { 0x3F, 0x1F, 0x2F, 0x37,
1089 // 00111011 00111101 00111110
1091 Double_t test_fwd = 0., testback = 0.;
1092 Int_t ilayer, itheta;
1093 AliITSNode *node = 0;
1094 AliITSneuron *fwd = 0, *back = 0;
1095 TList *listsector = 0;
1097 cout << "A -" << fActMinimum << "-" << flush;
1098 for (ilayer = 0; ilayer < 6; ilayer++) {
1099 for (itheta = 0; itheta < 180; itheta++) {
1100 listsector = (TList*)fPoints[ilayer][itheta]->At(sectoridx);
1101 TListIter iter(listsector);
1102 while ( (node = (AliITSNode*)iter.Next()) ) {
1103 TListIter fwditer(node->InnerOf());
1104 TListIter backiter(node->OuterOf());
1105 testfwd = testback = fActMinimum;
1106 while ( (fwd = (AliITSneuron*)fwditer.Next()) ) {
1107 if (fwd->Activation() > testfwd) {
1108 testfwd = fwd->Activation();
1109 node->fNext = fwd->Outer();
1112 while ( (back = (AliITSneuron*)backiter.Next()) ) {
1113 if (back->Activation() > testback) {
1114 testback = back->Activation();
1115 node->fPrev = back->Inner();
1122 cout << "B" << flush;
1123 for (ilayer = 0; ilayer < 5; ilayer++) {
1124 for (itheta = 0; itheta < 180; itheta++) {
1125 listsector = (TList*)fPoints[ilayer][itheta]->At(sectoridx);
1126 TListIter iter(listsector);
1127 while ( (node = (AliITSNode*)iter.Next()) ) {
1129 if (node->fNext->fPrev != node) node->fNext = 0;
1135 cout << "C" << flush;
1137 Int_t stored = 0, l1, l2, i, mask, im;
1138 AliITSNode *temp = 0;
1140 for (itheta = 0; itheta < 180; itheta++) {
1141 list[0] = (TList*)fPoints[0][itheta]->At(sectoridx);
1142 list[1] = (TList*)fPoints[1][itheta]->At(sectoridx);
1143 for (i = 0; i < 2; i++) {
1144 TListIter iter(list[i]);
1145 while ( (node = (AliITSNode*)iter.Next()) ) {
1146 //cout << endl << node << flush;
1147 AliITSneuralChain *chain = new AliITSneuralChain;
1148 for (temp = node; temp; temp = temp->fNext) {
1149 chain->Insert((AliITSNeuralPoint*)temp);
1152 mask = chain->OccupationMask();
1153 for (im = 0; im < 7; im++) ok = ok || (mask == allowmask[im]);
1158 //cout << " lab " << flush;
1159 chain->AssignLabel();
1160 //cout << " add " << flush;
1161 fTracks->AddLast(chain);
1163 //cout << " " << stored << flush;
1168 cout << "D" << flush;
1175 Double_t AliITSNeuralTracker::Activate(AliITSneuron* &unit)
1177 // Computes the new neural activation and
1178 // returns the variation w.r.t the previous one
1180 if (!unit) return 0.0;
1181 Double_t sumgain = 0.0, sumcost = 0.0, input, actOld, actNew;
1183 // sum gain contributions
1184 TObjArrayIter *iter = (TObjArrayIter*)unit->Gain()->MakeIterator();
1187 link = (AliITSlink*)iter->Next();
1189 sumgain += link->Linked()->Activation() * link->Weight();
1192 // sum cost contributions
1193 TObjArrayIter *testiter = (TObjArrayIter*)unit->Inner()->InnerOf()->MakeIterator();
1194 AliITSneuron *linked = 0;
1196 linked = (AliITSneuron*)testiter->Next();
1198 if (linked == unit) continue;
1199 sumcost += linked->Activation();
1202 testiter = (TObjArrayIter*)unit->Outer()->OuterOf()->MakeIterator();
1204 linked = (AliITSneuron*)testiter->Next();
1206 if (linked == unit) continue;
1207 sumcost += linked->Activation();
1210 //cout << "gain = " << sumgain << ", cost = " << sumcost << endl;
1212 input = (sumgain - sumcost) / fTemperature;
1213 actOld = unit->Activation();
1214 #ifdef NEURAL_LINEAR
1215 if (input <= -2.0 * fTemperature)
1217 else if (input >= 2.0 * fTemperature)
1220 actNew = input / (4.0 * fTemperature) + 0.5;
1222 actNew = 1.0 / (1.0 + TMath::Exp(-input));
1224 unit->Activation() = actNew;
1225 return TMath::Abs(actNew - actOld);
1230 void AliITSNeuralTracker::AliITSneuron::Add2Gain(AliITSneuron *n, Double_t multconst, Double_t exponent)
1232 // Adds a neuron to the collection of sequenced ones of another neuron
1234 AliITSlink *link = new AliITSlink;
1236 Double_t weight = Weight(n);
1237 link->Weight() = multconst * TMath::Power(weight, exponent);
1238 fGain->AddLast(link);
1243 Double_t AliITSNeuralTracker::AliITSneuron::Weight(AliITSneuron *n)
1245 // computes synaptic weight
1246 TVector3 me(Outer()->X() - Inner()->X(), Outer()->Y() - Inner()->Y(), Outer()->Z() - Inner()->Z());
1247 TVector3 it(n->Outer()->X() - n->Inner()->X(), n->Outer()->Y() - n->Inner()->Y(), n->Outer()->Z() - n->Inner()->Z());
1249 Double_t angle = me.Angle(it);
1250 Double_t weight = 1.0 - sin(angle);
1257 AliITSNeuralTracker::AliITSNeuralTracker(const AliITSNeuralTracker &t)
1258 : TObject((TObject&)t)
1260 // NO copy constructor
1261 Fatal("AliITSNeuralTracker", "No copy constructor allowed!");
1264 AliITSNeuralTracker& AliITSNeuralTracker::operator=(const AliITSNeuralTracker&)
1266 // NO assignment operator
1267 Fatal("AliITSNeuralTracker", "No assignment allowed!");
1271 AliITSNeuralTracker::AliITSNode::AliITSNode(const AliITSNode &t)
1272 : AliITSNeuralPoint((AliITSNeuralPoint&)t)
1274 // NO copy constructor
1275 Fatal("AliITSNode", "No copy constructor allowed!");
1278 AliITSNeuralTracker::AliITSNode& AliITSNeuralTracker::AliITSNode::operator=(const AliITSNode&)
1280 // NO assignment operator
1281 Fatal("AliITSNode", "No assignment allowed!");
1285 AliITSNeuralTracker::AliITSneuron::AliITSneuron(const AliITSneuron &t)
1286 : TObject((TObject&)t)
1288 // NO copy constructor
1289 Fatal("AliITSneuron", "No copy constructor allowed!");
1292 AliITSNeuralTracker::AliITSneuron& AliITSNeuralTracker::AliITSneuron::operator=(const AliITSneuron&)
1294 // NO assignment operator
1295 Fatal("AliITSneuron", "No assignment allowed!");
1299 AliITSNeuralTracker::AliITSlink::AliITSlink(const AliITSlink &t)
1300 : TObject((TObject&)t)
1302 // NO copy constructor
1303 Fatal("AliITSlink", "No copy constructor allowed!");
1306 AliITSNeuralTracker::AliITSlink& AliITSNeuralTracker::AliITSlink::operator=(const AliITSlink&)
1308 // NO assignment operator
1309 Fatal("AliITSlink", "No assignment allowed!");