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 //using namespace std;
46 ClassImp(AliITSNeuralTracker)
48 //--------------------------------------------------------------------------------------------
50 AliITSNeuralTracker::AliITSNeuralTracker()
54 // Initializes some data-members:
55 // - all pointers to NULL
56 // - theta cut to 180 deg. (= no theta cut)
57 // - initial choice of only 1 azimuthal sector
58 // - initial values for the neural network parameters.
60 // With these settings the neural tracker can't work
61 // because it has not any curvature cut set.
67 fSectorWidth = TMath::Pi() * 2.0;
68 fPolarInterval = 45.0;
70 fStructureOK = kFALSE;
72 fVX = fVY = fVZ = 0.0;
75 for (ilayer = 0; ilayer < 6; ilayer++) {
76 for (itheta = 0; itheta < 180; itheta++) fPoints[ilayer][itheta] = 0;
77 fThetaCut2DMin[ilayer] = 0.0;
78 fThetaCut2DMax[ilayer] = TMath::Pi();
79 fThetaCut3DMin[ilayer] = 0.0;
80 fThetaCut3DMax[ilayer] = TMath::Pi();
81 fHelixMatchCutMin[ilayer] = 1.0;
82 fHelixMatchCutMax[ilayer] = 1.0;
89 fStabThreshold = 0.001;
90 fGain2CostRatio = 1.0;
96 fChains = new TTree("TreeC", "Sextines of points");
97 fChains->Branch("l0", &fPoint[0], "l0/I");
98 fChains->Branch("l1", &fPoint[1], "l1/I");
99 fChains->Branch("l2", &fPoint[2], "l2/I");
100 fChains->Branch("l3", &fPoint[3], "l3/I");
101 fChains->Branch("l4", &fPoint[4], "l4/I");
102 fChains->Branch("l5", &fPoint[5], "l5/I");
105 //--------------------------------------------------------------------------------------------
107 AliITSNeuralTracker::~AliITSNeuralTracker()
111 // It Destroys all the dynamic arrays and
112 // clears the TCollections and the points tree
116 Int_t ilayer, itheta;
118 for (ilayer = 0; ilayer < 6; ilayer++) {
119 for (itheta = 0; itheta < 180; itheta++) {
120 fPoints[ilayer][itheta]->SetOwner();
121 delete fPoints[ilayer][itheta];
124 fNeurons->SetOwner();
130 //--------------------------------------------------------------------------------------------
132 void AliITSNeuralTracker::Display(TCanvas*& canv) const
134 // Displays the neural segments
136 Double_t x1, y1, x2, y2;
138 TObjArrayIter iter(fNeurons);
140 AliITSneuron *unit = (AliITSneuron*)iter.Next();
142 if (unit->Activation() < fActMinimum) continue;
143 x1 = unit->Inner()->X();
144 x2 = unit->Outer()->X();
145 y1 = unit->Inner()->Y();
146 y2 = unit->Outer()->Y();
147 TLine *line = new TLine(x1, y1, x2, y2);
154 //--------------------------------------------------------------------------------------------
156 void AliITSNeuralTracker::SetThetaCuts2D(Double_t *min, Double_t *max)
162 for (i = 0; i < 5; i++) {
163 if (min[i] > max[i]) {
168 fThetaCut2DMin[i] = min[i];
169 fThetaCut2DMax[i] = max[i];
171 for (i = 0; i < 5; i++) {
172 cout << "Theta 2D cut for layer " << i << " = " << fThetaCut2DMin[i] << " to " << fThetaCut2DMax[i] << endl;
176 //--------------------------------------------------------------------------------------------
178 void AliITSNeuralTracker::SetThetaCuts3D(Double_t *min, Double_t *max)
184 for (i = 0; i < 5; i++) {
185 if (min[i] > max[i]) {
190 fThetaCut3DMin[i] = min[i];
191 fThetaCut3DMax[i] = max[i];
193 for (i = 0; i < 5; i++) {
194 cout << "Theta 3D cut for layer " << i << " = " << fThetaCut3DMin[i] << " to " << fThetaCut3DMax[i] << endl;
198 //--------------------------------------------------------------------------------------------
200 void AliITSNeuralTracker::SetHelixMatchCuts(Double_t *min, Double_t *max)
206 for (i = 0; i < 5; i++) {
207 if (min[i] > max[i]) {
212 fHelixMatchCutMin[i] = min[i];
213 fHelixMatchCutMax[i] = max[i];
215 for (i = 0; i < 5; i++) {
216 cout << "Helix-match cut for layer " << i << " = " << fHelixMatchCutMin[i] << " to " << fHelixMatchCutMax[i] << endl;
220 //--------------------------------------------------------------------------------------------
222 void AliITSNeuralTracker::SetCurvatureCuts(Int_t ncuts, Double_t *curv)
224 // CURVATURE CUTS SETTER
226 // Requires an array of double values and its dimension
227 // After sorting it in increasing order, the array of curvature cuts
228 // is dinamically allocated, and filled with the sorted cuts array.
229 // A message is shown which lists all the curvature cuts.
231 Int_t i, *ind = new Int_t[ncuts];
232 TMath::Sort(ncuts, curv, ind, kFALSE);
233 fCurvCut = new Double_t[ncuts];
234 cout << "\n" << ncuts << " curvature cuts defined" << endl << "-----" << endl;
235 for (i = 0; i < ncuts; i++) {
236 fCurvCut[i] = curv[ind[i]];
237 cout << "Cut #" << i + 1 << " : " << fCurvCut[i] << endl;
239 cout << "-----" << endl;
243 //--------------------------------------------------------------------------------------------
245 void AliITSNeuralTracker::CreateArrayStructure(Int_t nsectors)
249 // Organizes the array structure to store all points in.
251 // The array is organized into a "multi-level" TCollection:
252 // - 6 fPoints[] TObjArray containing a TObjArray for each layer
253 // - each TObject contains a TObjArray for each sector.
255 // sets the number of sectors and their width.
256 fSectorNum = nsectors;
257 fSectorWidth = TMath::Pi() * 2.0 / (Double_t)fSectorNum;
259 // creates the TCollection structure
260 Int_t ilayer, isector, itheta;
261 TObjArray *sector = 0;
262 for (ilayer = 0; ilayer < 6; ilayer++) {
263 for (itheta = 0; itheta < 180; itheta++) {
264 if (fPoints[ilayer][itheta]) {
265 fPoints[ilayer][itheta]->SetOwner();
266 delete fPoints[ilayer][itheta];
268 fPoints[ilayer][itheta] = new TObjArray(nsectors);
269 for (isector = 0; isector < nsectors; isector++) {
270 sector = new TObjArray;
272 fPoints[ilayer][itheta]->AddAt(sector, isector);
277 // Sets a checking flag to TRUE.
278 // This flag is checked before filling up the arrays with the points.
279 fStructureOK = kTRUE;
282 //--------------------------------------------------------------------------------------------
284 Int_t AliITSNeuralTracker::ArrangePoints(TTree* ptstree)
286 // POINTS STORAGE INTO ARRAY
288 // Reads points from the tree and creates AliITSNode objects for each one,
289 // storing them into the array structure defined above.
290 // Returns the number of points collected (if successful) or 0 (otherwise)
292 // check: if the points tree is NULL or empty, there is nothing to do...
293 if ( !ptstree || (ptstree && !(Int_t)ptstree->GetEntries()) ) {
294 Error("ArrangePoints", "Points tree is NULL or empty: no points to arrange");
299 Error("ArrangePoints", "Structure NOT defined. Call CreateArrayStructure() first");
303 Int_t isector, itheta, ientry, ilayer, nentries, pos;
304 TObjArray *sector = 0;
305 AliITSNode *created = 0;
306 AliITSNeuralPoint *cursor = 0;
308 ptstree->SetBranchAddress("pos", &pos);
309 ptstree->SetBranchAddress("Points", &cursor);
310 nentries = (Int_t)ptstree->GetEntries();
312 for (ientry = 0; ientry < nentries; ientry++) {
313 ptstree->GetEntry(ientry);
314 // creates the object
315 created = new AliITSNode(cursor, kTRUE);
316 created->SetUser(-1);
317 created->PosInTree() = pos;
318 // finds the sector in phi
319 isector = created->GetSector(fSectorWidth);
320 itheta = created->GetThetaCell();
321 ilayer = created->GetLayer();
322 if (ilayer < 0 || ilayer > 5) {
323 Error("ArrangePoints", "Layer value %d not allowed. Aborting...", ilayer);
326 // selects the right TObjArray to store object into
327 sector = (TObjArray*)fPoints[ilayer][itheta]->At(isector);
328 sector->AddLast(created);
331 // returns the number of points processed
335 //--------------------------------------------------------------------------------------------
337 void AliITSNeuralTracker::PrintPoints()
339 // creates the TCollection structure
340 TObjArray *sector = 0;
341 Int_t ilayer, isector, itheta;
342 fstream file("test.log", ios::out);
343 for (ilayer = 0; ilayer < 6; ilayer++) {
344 for (isector = 0; isector < fSectorNum; isector++) {
345 for (itheta = 0; itheta < 180; itheta++) {
346 sector = (TObjArray*)fPoints[ilayer][itheta]->At(isector);
347 file << ilayer << " " << isector << " " << itheta;
348 file << " " << sector->GetSize() << " points" << endl;
355 //--------------------------------------------------------------------------------------------
357 Bool_t AliITSNeuralTracker::PassCurvCut
358 (AliITSNode *p1, AliITSNode *p2, Int_t curvindex, Double_t vx, Double_t vy, Double_t vz)
360 // CURVATURE CUT EVALUATOR
362 // Checks the passsed point pair w.r. to the current curvature cut
363 // Returns the result of the check.
365 if (curvindex < 0 || curvindex >= fCurvNum) {
366 Error("PassCurvCut", "Curv index %d out of range", curvindex);
370 // Find the reference layer
371 Int_t lay1 = p1->GetLayer();
372 Int_t lay2 = p2->GetLayer();
373 Int_t reflayer = (lay1 < lay2) ? lay1 : lay2;
375 Double_t x1 = p1->X() - vx;
376 Double_t x2 = p2->X() - vx;
377 Double_t y1 = p1->Y() - vy;
378 Double_t y2 = p2->Y() - vy;
379 Double_t z1 = p1->Z() - vz;
380 Double_t z2 = p2->Z() - vz;
381 Double_t r1 = sqrt(x1*x1 + y1*y1);
382 Double_t r2 = sqrt(x2*x2 + y2*y2);
384 // calculation of curvature
385 Double_t dx = p1->X() - p2->X(), dy = p1->Y() - p2->Y();
386 Double_t num = 2 * (x1*y2 - x2*y1);
387 Double_t den = r1*r2*sqrt(dx*dx + dy*dy);
391 curv = fabs(num / den);
392 if (curv > fCurvCut[curvindex]) return kFALSE;
400 curv = TMath::Abs(num / den);
401 if (curv > fCurvCut[curvindex]) return kFALSE;
405 // calculation of helix matching
406 Double_t arc1 = 2.0 * r1 * curv;
407 Double_t arc2 = 2.0 * r2 * curv;
408 Double_t helmatch = 0.0;
409 if (arc1 > -1.0 && arc1 < 1.0) arc1 = asin(arc1);
410 else arc1 = ((arc1 > 0.0) ? 0.5 : 1.5) * TMath::Pi();
411 if (arc2 > -1.0 && arc2 < 1.0) arc2 = asin(arc2);
412 else arc2 = ((arc2 > 0.0) ? 0.5 : 1.5) * TMath::Pi();
415 if (arc1 == 0.0 || arc2 == 0.0) return kFALSE;
416 helmatch = TMath::Abs(z1 / arc1 - z2 / arc2);
417 return (helmatch >= fHelixMatchCutMin[reflayer] && helmatch <= fHelixMatchCutMax[reflayer]);
421 //--------------------------------------------------------------------------------------------
423 Int_t AliITSNeuralTracker::PassAllCuts
424 (AliITSNode *p1, AliITSNode *p2, Int_t curvindex, Double_t vx, Double_t vy, Double_t vz)
426 // GLOBAL CUT EVALUATOR
428 // Checks all cuts for the passed point pair.
431 // 0 - All cuts passed
432 // 1 - theta 2D cut not passed
433 // 2 - theta 3D cut not passed
434 // 3 - curvature calculated but cut not passed
435 // 4 - curvature not calculated (division by zero)
436 // 5 - helix cut not passed
437 // 6 - curvature inxed out of range
439 if (curvindex < 0 || curvindex >= fCurvNum) return 6;
441 // Find the reference layer
442 Int_t lay1 = p1->GetLayer();
443 Int_t lay2 = p2->GetLayer();
444 Int_t reflayer = (lay1 < lay2) ? lay1 : lay2;
446 // Swap points in order that r1 < r2
447 AliITSNode *temp = 0;
448 if (p2->GetLayer() < p1->GetLayer()) {
454 // shift XY coords to the reference to the vertex position,
455 // for easier calculus of quantities.
456 Double_t x1 = p1->X() - vx;
457 Double_t x2 = p2->X() - vx;
458 Double_t y1 = p1->Y() - vy;
459 Double_t y2 = p2->Y() - vy;
460 Double_t z1 = p1->Z() - vz;
461 Double_t z2 = p2->Z() - vz;
462 Double_t r1 = sqrt(x1*x1 + y1*y1);
463 Double_t r2 = sqrt(x2*x2 + y2*y2);
465 // Check for theta cut
466 Double_t dtheta, dtheta3;
467 TVector3 v01(z1, r1, 0.0);
468 TVector3 v12(z2 - z1, r2 - r1, 0.0);
469 dtheta = v01.Angle(v12) * 180.0 / TMath::Pi();
470 TVector3 vv01(x1, y1, z1);
471 TVector3 vv12(x2 - x1, y2 - y1, z2 - z1);
472 dtheta3 = vv01.Angle(vv12) * 180.0 / TMath::Pi();
473 if (dtheta < fThetaCut2DMin[reflayer] || dtheta > fThetaCut2DMax[reflayer]) return 1;
474 if (dtheta3 < fThetaCut3DMin[reflayer] || dtheta3 > fThetaCut3DMax[reflayer]) return 2;
476 // calculation of curvature
477 Double_t dx = x1 - x2, dy = y1 - y2;
478 Double_t num = 2 * (x1*y2 - x2*y1);
479 Double_t den = r1*r2*sqrt(dx*dx + dy*dy);
482 curv = TMath::Abs(num / den);
483 if (curv > fCurvCut[curvindex]) return 3;
488 // calculation of helix matching
489 Double_t arc1 = 2.0 * r1 * curv;
490 Double_t arc2 = 2.0 * r2 * curv;
491 Double_t helmatch = 0.0;
492 if (arc1 > -1.0 && arc1 < 1.0) arc1 = asin(arc1);
493 else arc1 = ((arc1 > 0.0) ? 0.5 : 1.5) * TMath::Pi();
494 if (arc2 > -1.0 && arc2 < 1.0) arc2 = asin(arc2);
495 else arc2 = ((arc2 > 0.0) ? 0.5 : 1.5) * TMath::Pi();
498 if (arc1 == 0.0 || arc2 == 0.0) return kFALSE;
499 helmatch = TMath::Abs(z1 / arc1 - z2 / arc2);
500 if (helmatch < fHelixMatchCutMin[reflayer] || helmatch > fHelixMatchCutMax[reflayer]) return 5;
505 //--------------------------------------------------------------------------------------------
507 void AliITSNeuralTracker::StoreAbsoluteMatches()
509 // Stores in the 'fMatches' array of each node all the points in the
510 // adjacent layers which allow to create neurons accordin to the
511 // helix and theta cut, and also to the largest curvature cut
513 Int_t ilayer, isector, itheta1, itheta2, check;
514 TObjArray *list1 = 0, *list2 = 0;
515 AliITSNode *node1 = 0, *node2 = 0;
516 Double_t thetamin, thetamax;
519 for (isector = 0; isector < fSectorNum; isector++) {
520 // sector is chosen once for both lists
521 for (ilayer = 0; ilayer < 5; ilayer++) {
522 for (itheta1 = 0; itheta1 < 180; itheta1++) {
523 list1 = (TObjArray*)fPoints[ilayer][itheta1]->At(isector);
524 TObjArrayIter iter1(list1);
525 while ( (node1 = (AliITSNode*)iter1.Next()) ) {
526 if (node1->GetUser() >= 0) continue;
527 node1->Matches()->Clear();
528 thetamin = node1->ThetaDeg() - fPolarInterval;
529 thetamax = node1->ThetaDeg() + fPolarInterval;
530 imin = (Int_t)thetamin;
531 imax = (Int_t)thetamax;
532 if (imin < 0) imin = 0;
533 if (imax > 179) imax = 179;
534 for (itheta2 = imin; itheta2 <= imax; itheta2++) {
535 list2 = (TObjArray*)fPoints[ilayer + 1][itheta2]->At(isector);
536 TObjArrayIter iter2(list2);
537 while ( (node2 = (AliITSNode*)iter2.Next()) ) {
538 check = PassAllCuts(node1, node2, fCurvNum - 1, fVX, fVY, fVZ);
541 node1->Matches()->AddLast(node2);
544 //Info("StoreAbsoluteMatches", "Layer %d: THETA 2D cut not passed", ilayer);
547 //Info("StoreAbsoluteMatches", "Layer %d: THETA 3D cut not passed", ilayer);
550 //Info("StoreAbsoluteMatches", "Layer %d: CURVATURE cut not passed", ilayer);
553 //Info("StoreAbsoluteMatches", "Layer %d: Division by ZERO in curvature evaluation", ilayer);
556 //Info("StoreAbsoluteMatches", "Layer %d: HELIX-MATCH cut not passed", ilayer);
559 //Error("PassAllCuts", "Layer %d: Curv index out of range", ilayer);
562 Warning("StoreAbsoluteMatches", "Layer %d: %d: unrecognized return value", ilayer, check);
564 } // while (node2...)
565 } // for (itheta2...)
566 } // while (node1...)
569 } // for (isector...)
572 //--------------------------------------------------------------------------------------------
574 void AliITSNeuralTracker::PrintMatches(Bool_t stop)
576 // Prints the matches for each point
578 TFile *ft = new TFile("its_findables_v1.root");
579 TTree *tt = (TTree*)ft->Get("Tracks");
580 Int_t it, nP, nU, lab, nF = (Int_t)tt->GetEntries();
581 tt->SetBranchAddress("nhitsP", &nP);
582 tt->SetBranchAddress("nhitsU", &nU);
583 tt->SetBranchAddress("label", &lab);
584 TString strP("|"), strU("|");
585 for (it = 0; it < nF; it++) {
587 if (nP >= 5) strP.Append(Form("%d|", lab));
588 if (nU >= 5) strU.Append(Form("%d|", lab));
591 TObjArray *sector = 0;
592 Int_t ilayer, isector, itheta, id[3];
593 AliITSNode *node1 = 0, *node2 = 0;
595 for (ilayer = 0; ilayer < 6; ilayer++) {
596 for (isector = 0; isector < fSectorNum; isector++) {
597 for (itheta = 0; itheta < 180; itheta++) {
598 sector = (TObjArray*)fPoints[ilayer][itheta]->At(isector);
599 TObjArrayIter points(sector);
600 while ( (node1 = (AliITSNode*)points.Next()) ) {
601 for (it = 0; it < 3; it++) id[it] = node1->GetLabel(it);
602 nF = (Int_t)node1->Matches()->GetSize();
603 cout << "Node layer: " << node1->GetLayer() << ", labels: ";
604 cout << id[0] << " " << id[1] << " " << id[2] << " --> ";
606 cout << "NO MatchES!!!" << endl;
609 cout << nF << " Matches" << endl;
610 for (it = 0; it < 3; it++) {
611 if (strP.Contains(Form("|%d|", id[it])))
612 cout << "Belongs to findable (originary) track " << id[it] << endl;
613 if (strU.Contains(Form("|%d|", id[it])))
614 cout << "Belongs to findable (post-Kalman) track " << id[it] << endl;
616 TObjArrayIter matches(node1->Matches());
617 while ( (node2 = (AliITSNode*)matches.Next()) ) {
618 cout << "Match with " << node2;
619 Int_t *sh = node1->SharedID(node2);
620 for (Int_t k = 0; k < 3; k++)
621 if (sh[k] > -1) cout << " " << sh[k];
625 cout << "Press a key" << endl;
634 //--------------------------------------------------------------------------------------------
636 void AliITSNeuralTracker::ResetNodes(Int_t isector)
638 // Clears some utility arrays for each node
640 Int_t ilayer, itheta;
641 TObjArray *sector = 0;
642 AliITSNode *node = 0;
643 for (ilayer = 0; ilayer < 6; ilayer++) {
644 for (itheta = 0; itheta < 180; itheta++) {
645 sector = (TObjArray*)fPoints[ilayer][itheta]->At(isector);
646 TObjArrayIter iter(sector);
648 node = (AliITSNode*)iter.Next();
650 node->InnerOf()->Clear();
651 node->OuterOf()->Clear();
652 delete node->InnerOf();
653 delete node->OuterOf();
654 node->InnerOf() = new TObjArray;
655 node->OuterOf() = new TObjArray;
661 //--------------------------------------------------------------------------------------------
663 void AliITSNeuralTracker::NeuralTracking(const char* filesave, TCanvas*& display)
665 // This is the public method that operates the tracking.
666 // It works sector by sector, and at the end saves the found tracks.
667 // Other methods are privare because they have no meaning id used alone,
668 // and sometimes they could get segmentation faults due to uninitialized
669 // datamembert they require to work on.
670 // The argument is a file where the final results have to be stored.
672 Bool_t isStable = kFALSE;
673 Int_t i, nUnits = 0, nLinks = 0, nTracks = 0, sectTracks = 0, totTracks = 0;
675 // tracking through sectors
678 for (sector = 0; sector < fSectorNum; sector++) {
679 cout << "\rSector " << sector << ": " << endl;
681 for(curv = 0; curv < fCurvNum; curv++) {
682 cout << "- curvature step " << curv + 1;
683 cout << " (" << fCurvCut[curv] << "): ";
685 nUnits = CreateNeurons(sector, curv);
687 cout << "no neurons --> skipped" << endl;
690 cout << nUnits << " units, " << flush;
692 nLinks = LinkNeurons();
694 cout << "no connections --> skipped" << endl;
697 cout << nLinks << " connections, " << flush;
701 if (display) Display(display);
702 TObjArrayIter iter(fNeurons);
704 AliITSneuron *n = (AliITSneuron*)iter.Next();
709 cout << i << " cycles --> " << flush;
712 nTracks = Save(sector);
713 cout << nTracks << " tracks" << endl;
714 sectTracks += nTracks;
716 totTracks += sectTracks;
717 //cout << sectTracks << " tracks found (total = " << totTracks << ") " << flush;
720 cout << endl << totTracks << " tracks found!!!" << endl;
721 cout << endl << "Saving results in file " << filesave << "..." << flush;
722 TFile *f = new TFile(filesave, "recreate");
723 fChains->Write("TreeC");
727 //--------------------------------------------------------------------------------------------
729 Int_t AliITSNeuralTracker::CreateNeurons(Int_t sectoridx, Int_t curvidx)
731 // Fills the neuron arrays, following the cut criteria for the selected step
732 // (secnum = sector to analyze, curvnum = curvature cut step to use)
733 // It also sets the initial random activation.
734 // In order to avoid a large number of 'new' operations, all existing neurons
735 // are reset and initialized with the new values, and are created new unit only if
736 // it turns out to be necessary
737 // the 'flag' argument is used to decide if the lower edge in the intevral
738 // of the accepted curvatures is given by zero (kFALSE) or by the preceding used cut (kTRUE)
739 // (if this is the first step, anyway, the minimum is always zero)
741 ResetNodes(sectoridx);
743 if (fNeurons) delete fNeurons;
744 fNeurons = new TObjArray;
746 AliITSneuron *unit = 0;
747 Int_t itheta, neurons = 0;
748 TObjArray *lstsector = 0;
751 Double_t vx[6], vy[6], vz[6];
752 AliITSNode *p[6] = {0, 0, 0, 0, 0, 0};
753 for (itheta = 0; itheta < 180; itheta++) {
754 lstsector = (TObjArray*)fPoints[0][itheta]->At(sectoridx);
755 TObjArrayIter lay0(lstsector);
756 while ( (p[0] = (AliITSNode*)lay0.Next()) ) {
757 if (p[0]->GetUser() >= 0) continue;
761 TObjArrayIter lay1(p[0]->Matches());
762 while ( (p[1] = (AliITSNode*)lay1.Next()) ) {
763 if (p[1]->GetUser() >= 0) continue;
764 if (!PassCurvCut(p[0], p[1], curvidx, fVX, fVY, fVZ)) continue;
765 unit = new AliITSneuron;
766 unit->Inner() = p[0];
767 unit->Outer() = p[1];
768 unit->Activation() = gRandom->Rndm() * (fEdge1 - fEdge2) + fEdge2;
769 unit->Gain() = new TObjArray;
770 fNeurons->AddLast(unit);
771 p[0]->InnerOf()->AddLast(unit);
772 p[1]->OuterOf()->AddLast(unit);
777 TObjArrayIter lay2(p[1]->Matches());
778 while ( (p[2] = (AliITSNode*)lay2.Next()) ) {
779 if (p[2]->GetUser() >= 0) continue;
780 if (!PassCurvCut(p[1], p[2], curvidx, vx[1], vy[1], vz[1])) continue;
781 unit = new AliITSneuron;
782 unit->Inner() = p[1];
783 unit->Outer() = p[2];
784 unit->Activation() = gRandom->Rndm() * (fEdge1 - fEdge2) + fEdge2;
785 unit->Gain() = new TObjArray;
786 fNeurons->AddLast(unit);
787 p[1]->InnerOf()->AddLast(unit);
788 p[2]->OuterOf()->AddLast(unit);
793 TObjArrayIter lay3(p[2]->Matches());
794 while ( (p[3] = (AliITSNode*)lay3.Next()) ) {
795 if (p[3]->GetUser() >= 0) continue;
796 if (!PassCurvCut(p[2], p[3], curvidx, vx[2], vy[2], vz[2])) continue;
797 unit = new AliITSneuron;
798 unit->Inner() = p[2];
799 unit->Outer() = p[3];
800 unit->Activation() = gRandom->Rndm() * (fEdge1 - fEdge2) + fEdge2;
801 unit->Gain() = new TObjArray;
802 fNeurons->AddLast(unit);
803 p[2]->InnerOf()->AddLast(unit);
804 p[3]->OuterOf()->AddLast(unit);
809 TObjArrayIter lay4(p[3]->Matches());
810 while ( (p[4] = (AliITSNode*)lay4.Next()) ) {
811 if (p[4]->GetUser() >= 0) continue;
812 if (!PassCurvCut(p[3], p[4], curvidx, vx[3], vy[3], vz[3])) continue;
813 unit = new AliITSneuron;
814 unit->Inner() = p[3];
815 unit->Outer() = p[4];
816 unit->Activation() = gRandom->Rndm() * (fEdge1 - fEdge2) + fEdge2;
817 unit->Gain() = new TObjArray;
818 fNeurons->AddLast(unit);
819 p[3]->InnerOf()->AddLast(unit);
820 p[4]->OuterOf()->AddLast(unit);
825 TObjArrayIter lay5(p[4]->Matches());
826 while ( (p[5] = (AliITSNode*)lay5.Next()) ) {
827 if (p[5]->GetUser() >= 0) continue;
828 if (!PassCurvCut(p[4], p[5], curvidx, vx[4], vy[4], vz[4])) continue;
829 unit = new AliITSneuron;
830 unit->Inner() = p[4];
831 unit->Outer() = p[5];
832 unit->Activation() = gRandom->Rndm() * (fEdge1 - fEdge2) + fEdge2;
833 unit->Gain() = new TObjArray;
834 fNeurons->AddLast(unit);
835 p[4]->InnerOf()->AddLast(unit);
836 p[5]->OuterOf()->AddLast(unit);
845 // END OF NEW VERSION
848 for (ilayer = 0; ilayer < 6; ilayer++) {
849 for (itheta = 0; itheta < 180; itheta++) {
850 lstsector = (TObjArray*)fPoints[ilayer][itheta]->At(sectoridx);
851 TObjArrayIter inners(lstsector);
852 while ( (inner = (AliITSNode*)inners.Next()) ) {
853 if (inner->GetUser() >= 0) continue;
854 TObjArrayIter outers(inner->Matches());
855 while ( (outer = (AliITSNode*)outers.Next()) ) {
856 if (outer->GetUser() >= 0) continue;
857 if (!PassCurvCut(inner, outer, curvidx, fVX, fVY, fVZ)) continue;
858 unit = new AliITSneuron;
859 unit->Inner() = inner;
860 unit->Outer() = outer;
861 unit->Activation() = gRandom->Rndm() * (fEdge1 - fEdge2) + fEdge2;
862 unit->Gain() = new TObjArray;
863 fNeurons->AddLast(unit);
864 inner->InnerOf()->AddLast(unit);
865 outer->OuterOf()->AddLast(unit);
873 fNeurons->SetOwner();
879 Int_t AliITSNeuralTracker::LinkNeurons() const
881 // Creates the neural synapses among all neurons
882 // which share a point.
885 TObjArrayIter iter(fNeurons), *testiter;
886 AliITSneuron *neuron = 0, *test = 0;
888 neuron = (AliITSneuron*)iter.Next();
891 testiter = (TObjArrayIter*)neuron->Inner()->OuterOf()->MakeIterator();
893 test = (AliITSneuron*)testiter->Next();
895 neuron->Add2Gain(test, fGain2CostRatio, fAlignExponent);
899 testiter = (TObjArrayIter*)neuron->Outer()->InnerOf()->MakeIterator();
901 test = (AliITSneuron*)testiter->Next();
903 neuron->Add2Gain(test, fGain2CostRatio, fAlignExponent);
913 Bool_t AliITSNeuralTracker::Update()
915 // A complete updating cycle with the asynchronous method
916 // when the neural network is stable, kTRUE is returned.
918 Double_t actVar = 0.0, totDiff = 0.0;
919 TObjArrayIter iter(fNeurons);
922 unit = (AliITSneuron*)iter.Next();
924 actVar = Activate(unit);
925 // calculation the relative activation variation
928 totDiff /= fNeurons->GetSize();
929 return (totDiff < fStabThreshold);
934 void AliITSNeuralTracker::CleanNetwork()
936 // Removes all deactivated neurons, and resolves the competitions
937 // to the advantage of the most active unit
939 AliITSneuron *unit = 0;
940 TObjArrayIter neurons(fNeurons);
941 while ( (unit = (AliITSneuron*)neurons.Next()) ) {
942 if (unit->Activation() < fActMinimum) {
943 unit->Inner()->InnerOf()->Remove(unit);
944 unit->Outer()->OuterOf()->Remove(unit);
945 delete fNeurons->Remove(unit);
951 AliITSneuron *enemy = 0;
953 while ( (unit = (AliITSneuron*)neurons.Next()) ) {
954 nIn = (Int_t)unit->Inner()->InnerOf()->GetSize();
955 nOut = (Int_t)unit->Outer()->OuterOf()->GetSize();
956 if (nIn < 2 && nOut < 2) continue;
959 TObjArrayIter competing(unit->Inner()->InnerOf());
960 while ( (enemy = (AliITSneuron*)competing.Next()) ) {
961 if (unit->Activation() > enemy->Activation()) {
962 enemy->Inner()->InnerOf()->Remove(enemy);
963 enemy->Outer()->OuterOf()->Remove(enemy);
964 delete fNeurons->Remove(enemy);
967 unit->Inner()->InnerOf()->Remove(unit);
968 unit->Outer()->OuterOf()->Remove(unit);
969 delete fNeurons->Remove(unit);
974 if (removed) continue;
977 TObjArrayIter competing(unit->Outer()->OuterOf());
978 while ( (enemy = (AliITSneuron*)competing.Next()) ) {
979 if (unit->Activation() > enemy->Activation()) {
980 enemy->Inner()->InnerOf()->Remove(enemy);
981 enemy->Outer()->OuterOf()->Remove(enemy);
982 delete fNeurons->Remove(enemy);
985 unit->Inner()->InnerOf()->Remove(unit);
986 unit->Outer()->OuterOf()->Remove(unit);
987 delete fNeurons->Remove(unit);
998 Bool_t AliITSNeuralTracker::CheckOccupation() const
1000 // checks if a track recognized has a point for each layer
1002 for (i = 0; i < 6; i++) if (fPoint[i] < 0) return kFALSE;
1008 Int_t AliITSNeuralTracker::Save(Int_t sectorid)
1010 // Creates chains of active neurons, in order to
1011 // find the tracks obtained as the neural network output.
1013 // every chain is started from the neurons in the first 2 layers
1014 Int_t i, check, stored = 0;
1015 Int_t a = sectorid; a = 0;
1016 Double_t testact = 0;
1017 AliITSneuron *unit = 0, *cursor = 0, *fwd = 0;
1018 AliITSNode *node = 0;
1019 TObjArrayIter iter(fNeurons), *fwditer;
1020 TObjArray *removedUnits = new TObjArray(0);
1021 TObjArray *removedPoints = new TObjArray(6);
1022 removedUnits->SetOwner(kFALSE);
1023 removedPoints->SetOwner(kFALSE);
1025 for (i = 0; i < 6; i++) fPoint[i] = -1;
1026 unit = (AliITSneuron*)iter.Next();
1028 if (unit->Inner()->GetLayer() > 0) continue;
1029 fPoint[unit->Inner()->GetLayer()] = unit->Inner()->PosInTree();
1030 fPoint[unit->Outer()->GetLayer()] = unit->Outer()->PosInTree();
1031 node = unit->Outer();
1032 removedUnits->AddLast(unit);
1033 removedPoints->AddAt(unit->Inner(), unit->Inner()->GetLayer());
1034 removedPoints->AddAt(unit->Outer(), unit->Outer()->GetLayer());
1036 testact = fActMinimum;
1037 fwditer = (TObjArrayIter*)node->InnerOf()->MakeIterator();
1040 cursor = (AliITSneuron*)fwditer->Next();
1042 if (cursor->Used()) continue;
1043 if (cursor->Activation() >= testact) {
1044 testact = cursor->Activation();
1049 removedUnits->AddLast(fwd);
1050 node = fwd->Outer();
1051 fPoint[node->GetLayer()] = node->PosInTree();
1052 removedPoints->AddAt(node, node->GetLayer());
1055 for (i = 0; i < 6; i++) if (fPoint[i] >= 0) check++;
1059 for (i = 0; i < 6; i++) {
1060 node = (AliITSNode*)removedPoints->At(i);
1063 fwditer = (TObjArrayIter*)removedUnits->MakeIterator();
1065 cursor = (AliITSneuron*)fwditer->Next();
1075 Int_t AliITSNeuralTracker::Save(Int_t sectoridx)
1076 // Creates chains of active neurons, in order to
1077 // find the tracks obtained as the neural network output.
1079 // Reads the final map of neurons and removes all
1080 // units with too small activation
1081 cout << "saving..." << flush;
1083 // every chain is started from the neurons in the first 2 layers
1084 // 00111111 00011111 00101111 00110111
1085 Int_t allowmask[] = { 0x3F, 0x1F, 0x2F, 0x37,
1087 // 00111011 00111101 00111110
1089 Double_t test_fwd = 0., testback = 0.;
1090 Int_t ilayer, itheta;
1091 AliITSNode *node = 0;
1092 AliITSneuron *fwd = 0, *back = 0;
1093 TList *listsector = 0;
1095 cout << "A -" << fActMinimum << "-" << flush;
1096 for (ilayer = 0; ilayer < 6; ilayer++) {
1097 for (itheta = 0; itheta < 180; itheta++) {
1098 listsector = (TList*)fPoints[ilayer][itheta]->At(sectoridx);
1099 TListIter iter(listsector);
1100 while ( (node = (AliITSNode*)iter.Next()) ) {
1101 TListIter fwditer(node->InnerOf());
1102 TListIter backiter(node->OuterOf());
1103 testfwd = testback = fActMinimum;
1104 while ( (fwd = (AliITSneuron*)fwditer.Next()) ) {
1105 if (fwd->Activation() > testfwd) {
1106 testfwd = fwd->Activation();
1107 node->fNext = fwd->Outer();
1110 while ( (back = (AliITSneuron*)backiter.Next()) ) {
1111 if (back->Activation() > testback) {
1112 testback = back->Activation();
1113 node->fPrev = back->Inner();
1120 cout << "B" << flush;
1121 for (ilayer = 0; ilayer < 5; ilayer++) {
1122 for (itheta = 0; itheta < 180; itheta++) {
1123 listsector = (TList*)fPoints[ilayer][itheta]->At(sectoridx);
1124 TListIter iter(listsector);
1125 while ( (node = (AliITSNode*)iter.Next()) ) {
1127 if (node->fNext->fPrev != node) node->fNext = 0;
1133 cout << "C" << flush;
1135 Int_t stored = 0, l1, l2, i, mask, im;
1136 AliITSNode *temp = 0;
1138 for (itheta = 0; itheta < 180; itheta++) {
1139 list[0] = (TList*)fPoints[0][itheta]->At(sectoridx);
1140 list[1] = (TList*)fPoints[1][itheta]->At(sectoridx);
1141 for (i = 0; i < 2; i++) {
1142 TListIter iter(list[i]);
1143 while ( (node = (AliITSNode*)iter.Next()) ) {
1144 //cout << endl << node << flush;
1145 AliITSneuralChain *chain = new AliITSneuralChain;
1146 for (temp = node; temp; temp = temp->fNext) {
1147 chain->Insert((AliITSNeuralPoint*)temp);
1150 mask = chain->OccupationMask();
1151 for (im = 0; im < 7; im++) ok = ok || (mask == allowmask[im]);
1156 //cout << " lab " << flush;
1157 chain->AssignLabel();
1158 //cout << " add " << flush;
1159 fTracks->AddLast(chain);
1161 //cout << " " << stored << flush;
1166 cout << "D" << flush;
1173 Double_t AliITSNeuralTracker::Activate(AliITSneuron* &unit)
1175 // Computes the new neural activation and
1176 // returns the variation w.r.t the previous one
1178 if (!unit) return 0.0;
1179 Double_t sumgain = 0.0, sumcost = 0.0, input, actOld, actNew;
1181 // sum gain contributions
1182 TObjArrayIter *iter = (TObjArrayIter*)unit->Gain()->MakeIterator();
1185 link = (AliITSlink*)iter->Next();
1187 sumgain += link->Linked()->Activation() * link->Weight();
1190 // sum cost contributions
1191 TObjArrayIter *testiter = (TObjArrayIter*)unit->Inner()->InnerOf()->MakeIterator();
1192 AliITSneuron *linked = 0;
1194 linked = (AliITSneuron*)testiter->Next();
1196 if (linked == unit) continue;
1197 sumcost += linked->Activation();
1200 testiter = (TObjArrayIter*)unit->Outer()->OuterOf()->MakeIterator();
1202 linked = (AliITSneuron*)testiter->Next();
1204 if (linked == unit) continue;
1205 sumcost += linked->Activation();
1208 //cout << "gain = " << sumgain << ", cost = " << sumcost << endl;
1210 input = (sumgain - sumcost) / fTemperature;
1211 actOld = unit->Activation();
1212 #ifdef NEURAL_LINEAR
1213 if (input <= -2.0 * fTemperature)
1215 else if (input >= 2.0 * fTemperature)
1218 actNew = input / (4.0 * fTemperature) + 0.5;
1220 actNew = 1.0 / (1.0 + TMath::Exp(-input));
1222 unit->Activation() = actNew;
1223 return TMath::Abs(actNew - actOld);
1228 void AliITSNeuralTracker::AliITSneuron::Add2Gain(AliITSneuron *n, Double_t multconst, Double_t exponent)
1230 // Adds a neuron to the collection of sequenced ones of another neuron
1232 AliITSlink *link = new AliITSlink;
1234 Double_t weight = Weight(n);
1235 link->Weight() = multconst * TMath::Power(weight, exponent);
1236 fGain->AddLast(link);
1241 Double_t AliITSNeuralTracker::AliITSneuron::Weight(AliITSneuron *n)
1243 // computes synaptic weight
1244 TVector3 me(Outer()->X() - Inner()->X(), Outer()->Y() - Inner()->Y(), Outer()->Z() - Inner()->Z());
1245 TVector3 it(n->Outer()->X() - n->Inner()->X(), n->Outer()->Y() - n->Inner()->Y(), n->Outer()->Z() - n->Inner()->Z());
1247 Double_t angle = me.Angle(it);
1248 Double_t weight = 1.0 - sin(angle);
1255 AliITSNeuralTracker::AliITSNeuralTracker(const AliITSNeuralTracker &t)
1256 : TObject((TObject&)t)
1258 // NO copy constructor
1259 Fatal("AliITSNeuralTracker", "No copy constructor allowed!");
1262 AliITSNeuralTracker& AliITSNeuralTracker::operator=(const AliITSNeuralTracker&)
1264 // NO assignment operator
1265 Fatal("AliITSNeuralTracker", "No assignment allowed!");
1269 AliITSNeuralTracker::AliITSNode::AliITSNode(const AliITSNode &t)
1270 : AliITSNeuralPoint((AliITSNeuralPoint&)t)
1272 // NO copy constructor
1273 Fatal("AliITSNode", "No copy constructor allowed!");
1276 AliITSNeuralTracker::AliITSNode& AliITSNeuralTracker::AliITSNode::operator=(const AliITSNode&)
1278 // NO assignment operator
1279 Fatal("AliITSNode", "No assignment allowed!");
1283 AliITSNeuralTracker::AliITSneuron::AliITSneuron(const AliITSneuron &t)
1284 : TObject((TObject&)t)
1286 // NO copy constructor
1287 Fatal("AliITSneuron", "No copy constructor allowed!");
1290 AliITSNeuralTracker::AliITSneuron& AliITSNeuralTracker::AliITSneuron::operator=(const AliITSneuron&)
1292 // NO assignment operator
1293 Fatal("AliITSneuron", "No assignment allowed!");
1297 AliITSNeuralTracker::AliITSlink::AliITSlink(const AliITSlink &t)
1298 : TObject((TObject&)t)
1300 // NO copy constructor
1301 Fatal("AliITSlink", "No copy constructor allowed!");
1304 AliITSNeuralTracker::AliITSlink& AliITSNeuralTracker::AliITSlink::operator=(const AliITSlink&)
1306 // NO assignment operator
1307 Fatal("AliITSlink", "No assignment allowed!");