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():
64 fStabThreshold(0.001),
71 // Initializes some data-members:
72 // - all pointers to NULL
73 // - theta cut to 180 deg. (= no theta cut)
74 // - initial choice of only 1 azimuthal sector
75 // - initial values for the neural network parameters.
77 // With these settings the neural tracker can't work
78 // because it has not any curvature cut set.
81 fSectorWidth = TMath::Pi() * 2.0;
84 for (ilayer = 0; ilayer < 6; ilayer++) {
85 for (itheta = 0; itheta < 180; itheta++) fPoints[ilayer][itheta] = 0;
86 fThetaCut2DMin[ilayer] = 0.0;
87 fThetaCut2DMax[ilayer] = TMath::Pi();
88 fThetaCut3DMin[ilayer] = 0.0;
89 fThetaCut3DMax[ilayer] = TMath::Pi();
90 fHelixMatchCutMin[ilayer] = 1.0;
91 fHelixMatchCutMax[ilayer] = 1.0;
97 fChains = new TTree("TreeC", "Sextines of points");
98 fChains->Branch("l0", &fPoint[0], "l0/I");
99 fChains->Branch("l1", &fPoint[1], "l1/I");
100 fChains->Branch("l2", &fPoint[2], "l2/I");
101 fChains->Branch("l3", &fPoint[3], "l3/I");
102 fChains->Branch("l4", &fPoint[4], "l4/I");
103 fChains->Branch("l5", &fPoint[5], "l5/I");
106 AliITSNeuralTracker::AliITSNeuralTracker(const AliITSNeuralTracker &n):TObject(n),
107 fSectorNum(n.fSectorNum),
108 fSectorWidth(n.fSectorWidth),
109 fPolarInterval(n.fPolarInterval),
110 fCurvNum(n.fCurvNum),
111 fCurvCut(n.fCurvCut),
112 fStructureOK(n.fStructureOK),
116 fActMinimum(n.fActMinimum),
119 fTemperature(n.fTemperature),
120 fStabThreshold(n.fStabThreshold),
121 fGain2CostRatio(n.fGain2CostRatio),
122 fAlignExponent(n.fAlignExponent),
124 fNeurons(n.fNeurons){
128 AliITSNeuralTracker& AliITSNeuralTracker::operator=(const AliITSNeuralTracker& t){
129 //assignment operator
130 this->~AliITSNeuralTracker();
131 new(this) AliITSNeuralTracker(t);
135 //--------------------------------------------------------------------------------------------
137 AliITSNeuralTracker::~AliITSNeuralTracker()
141 // It Destroys all the dynamic arrays and
142 // clears the TCollections and the points tree
146 Int_t ilayer, itheta;
148 for (ilayer = 0; ilayer < 6; ilayer++) {
149 for (itheta = 0; itheta < 180; itheta++) {
150 fPoints[ilayer][itheta]->SetOwner();
151 delete fPoints[ilayer][itheta];
154 fNeurons->SetOwner();
160 //--------------------------------------------------------------------------------------------
162 void AliITSNeuralTracker::Display(TCanvas*& canv) const
164 // Displays the neural segments
166 Double_t x1, y1, x2, y2;
168 TObjArrayIter iter(fNeurons);
170 AliITSneuron *unit = (AliITSneuron*)iter.Next();
172 if (unit->Activation() < fActMinimum) continue;
173 x1 = unit->Inner()->X();
174 x2 = unit->Outer()->X();
175 y1 = unit->Inner()->Y();
176 y2 = unit->Outer()->Y();
177 TLine *line = new TLine(x1, y1, x2, y2);
184 //--------------------------------------------------------------------------------------------
186 void AliITSNeuralTracker::SetThetaCuts2D(Double_t *min, Double_t *max)
192 for (i = 0; i < 5; i++) {
193 if (min[i] > max[i]) {
198 fThetaCut2DMin[i] = min[i];
199 fThetaCut2DMax[i] = max[i];
201 for (i = 0; i < 5; i++) {
202 cout << "Theta 2D cut for layer " << i << " = " << fThetaCut2DMin[i] << " to " << fThetaCut2DMax[i] << endl;
206 //--------------------------------------------------------------------------------------------
208 void AliITSNeuralTracker::SetThetaCuts3D(Double_t *min, Double_t *max)
214 for (i = 0; i < 5; i++) {
215 if (min[i] > max[i]) {
220 fThetaCut3DMin[i] = min[i];
221 fThetaCut3DMax[i] = max[i];
223 for (i = 0; i < 5; i++) {
224 cout << "Theta 3D cut for layer " << i << " = " << fThetaCut3DMin[i] << " to " << fThetaCut3DMax[i] << endl;
228 //--------------------------------------------------------------------------------------------
230 void AliITSNeuralTracker::SetHelixMatchCuts(Double_t *min, Double_t *max)
236 for (i = 0; i < 5; i++) {
237 if (min[i] > max[i]) {
242 fHelixMatchCutMin[i] = min[i];
243 fHelixMatchCutMax[i] = max[i];
245 for (i = 0; i < 5; i++) {
246 cout << "Helix-match cut for layer " << i << " = " << fHelixMatchCutMin[i] << " to " << fHelixMatchCutMax[i] << endl;
250 //--------------------------------------------------------------------------------------------
252 void AliITSNeuralTracker::SetCurvatureCuts(Int_t ncuts, Double_t *curv)
254 // CURVATURE CUTS SETTER
256 // Requires an array of double values and its dimension
257 // After sorting it in increasing order, the array of curvature cuts
258 // is dinamically allocated, and filled with the sorted cuts array.
259 // A message is shown which lists all the curvature cuts.
261 Int_t i, *ind = new Int_t[ncuts];
262 TMath::Sort(ncuts, curv, ind, kFALSE);
263 fCurvCut = new Double_t[ncuts];
264 cout << "\n" << ncuts << " curvature cuts defined" << endl << "-----" << endl;
265 for (i = 0; i < ncuts; i++) {
266 fCurvCut[i] = curv[ind[i]];
267 cout << "Cut #" << i + 1 << " : " << fCurvCut[i] << endl;
269 cout << "-----" << endl;
273 //--------------------------------------------------------------------------------------------
275 void AliITSNeuralTracker::CreateArrayStructure(Int_t nsectors)
279 // Organizes the array structure to store all points in.
281 // The array is organized into a "multi-level" TCollection:
282 // - 6 fPoints[] TObjArray containing a TObjArray for each layer
283 // - each TObject contains a TObjArray for each sector.
285 // sets the number of sectors and their width.
286 fSectorNum = nsectors;
287 fSectorWidth = TMath::Pi() * 2.0 / (Double_t)fSectorNum;
289 // creates the TCollection structure
290 Int_t ilayer, isector, itheta;
291 TObjArray *sector = 0;
292 for (ilayer = 0; ilayer < 6; ilayer++) {
293 for (itheta = 0; itheta < 180; itheta++) {
294 if (fPoints[ilayer][itheta]) {
295 fPoints[ilayer][itheta]->SetOwner();
296 delete fPoints[ilayer][itheta];
298 fPoints[ilayer][itheta] = new TObjArray(nsectors);
299 for (isector = 0; isector < nsectors; isector++) {
300 sector = new TObjArray;
302 fPoints[ilayer][itheta]->AddAt(sector, isector);
307 // Sets a checking flag to TRUE.
308 // This flag is checked before filling up the arrays with the points.
309 fStructureOK = kTRUE;
312 //--------------------------------------------------------------------------------------------
314 Int_t AliITSNeuralTracker::ArrangePoints(TTree* ptstree)
316 // POINTS STORAGE INTO ARRAY
318 // Reads points from the tree and creates AliITSNode objects for each one,
319 // storing them into the array structure defined above.
320 // Returns the number of points collected (if successful) or 0 (otherwise)
322 // check: if the points tree is NULL or empty, there is nothing to do...
323 if ( !ptstree || (ptstree && !(Int_t)ptstree->GetEntries()) ) {
324 Error("ArrangePoints", "Points tree is NULL or empty: no points to arrange");
329 Error("ArrangePoints", "Structure NOT defined. Call CreateArrayStructure() first");
333 Int_t isector, itheta, ientry, ilayer, nentries, pos;
334 TObjArray *sector = 0;
335 AliITSNode *created = 0;
336 AliITSNeuralPoint *cursor = 0;
338 ptstree->SetBranchAddress("pos", &pos);
339 ptstree->SetBranchAddress("Points", &cursor);
340 nentries = (Int_t)ptstree->GetEntries();
342 for (ientry = 0; ientry < nentries; ientry++) {
343 ptstree->GetEntry(ientry);
344 // creates the object
345 created = new AliITSNode(cursor, kTRUE);
346 created->SetUser(-1);
347 created->PosInTree() = pos;
348 // finds the sector in phi
349 isector = created->GetSector(fSectorWidth);
350 itheta = created->GetThetaCell();
351 ilayer = created->GetLayer();
352 if (ilayer < 0 || ilayer > 5) {
353 Error("ArrangePoints", "Layer value %d not allowed. Aborting...", ilayer);
356 // selects the right TObjArray to store object into
357 sector = (TObjArray*)fPoints[ilayer][itheta]->At(isector);
358 sector->AddLast(created);
361 // returns the number of points processed
365 //--------------------------------------------------------------------------------------------
367 void AliITSNeuralTracker::PrintPoints()
369 // creates the TCollection structure
370 TObjArray *sector = 0;
371 Int_t ilayer, isector, itheta;
372 fstream file("test.log", ios::out);
373 for (ilayer = 0; ilayer < 6; ilayer++) {
374 for (isector = 0; isector < fSectorNum; isector++) {
375 for (itheta = 0; itheta < 180; itheta++) {
376 sector = (TObjArray*)fPoints[ilayer][itheta]->At(isector);
377 file << ilayer << " " << isector << " " << itheta;
378 file << " " << sector->GetSize() << " points" << endl;
385 //--------------------------------------------------------------------------------------------
387 Bool_t AliITSNeuralTracker::PassCurvCut
388 (AliITSNode *p1, AliITSNode *p2, Int_t curvindex, Double_t vx, Double_t vy, Double_t vz)
390 // CURVATURE CUT EVALUATOR
392 // Checks the passsed point pair w.r. to the current curvature cut
393 // Returns the result of the check.
395 if (curvindex < 0 || curvindex >= fCurvNum) {
396 Error("PassCurvCut", "Curv index %d out of range", curvindex);
400 // Find the reference layer
401 Int_t lay1 = p1->GetLayer();
402 Int_t lay2 = p2->GetLayer();
403 Int_t reflayer = (lay1 < lay2) ? lay1 : lay2;
405 Double_t x1 = p1->X() - vx;
406 Double_t x2 = p2->X() - vx;
407 Double_t y1 = p1->Y() - vy;
408 Double_t y2 = p2->Y() - vy;
409 Double_t z1 = p1->Z() - vz;
410 Double_t z2 = p2->Z() - vz;
411 Double_t r1 = sqrt(x1*x1 + y1*y1);
412 Double_t r2 = sqrt(x2*x2 + y2*y2);
414 // calculation of curvature
415 Double_t dx = p1->X() - p2->X(), dy = p1->Y() - p2->Y();
416 Double_t num = 2 * (x1*y2 - x2*y1);
417 Double_t den = r1*r2*sqrt(dx*dx + dy*dy);
421 curv = fabs(num / den);
422 if (curv > fCurvCut[curvindex]) return kFALSE;
430 curv = TMath::Abs(num / den);
431 if (curv > fCurvCut[curvindex]) return kFALSE;
435 // calculation of helix matching
436 Double_t arc1 = 2.0 * r1 * curv;
437 Double_t arc2 = 2.0 * r2 * curv;
438 Double_t helmatch = 0.0;
439 if (arc1 > -1.0 && arc1 < 1.0) arc1 = asin(arc1);
440 else arc1 = ((arc1 > 0.0) ? 0.5 : 1.5) * TMath::Pi();
441 if (arc2 > -1.0 && arc2 < 1.0) arc2 = asin(arc2);
442 else arc2 = ((arc2 > 0.0) ? 0.5 : 1.5) * TMath::Pi();
445 if (arc1 == 0.0 || arc2 == 0.0) return kFALSE;
446 helmatch = TMath::Abs(z1 / arc1 - z2 / arc2);
447 return (helmatch >= fHelixMatchCutMin[reflayer] && helmatch <= fHelixMatchCutMax[reflayer]);
451 //--------------------------------------------------------------------------------------------
453 Int_t AliITSNeuralTracker::PassAllCuts
454 (AliITSNode *p1, AliITSNode *p2, Int_t curvindex, Double_t vx, Double_t vy, Double_t vz)
456 // GLOBAL CUT EVALUATOR
458 // Checks all cuts for the passed point pair.
461 // 0 - All cuts passed
462 // 1 - theta 2D cut not passed
463 // 2 - theta 3D cut not passed
464 // 3 - curvature calculated but cut not passed
465 // 4 - curvature not calculated (division by zero)
466 // 5 - helix cut not passed
467 // 6 - curvature inxed out of range
469 if (curvindex < 0 || curvindex >= fCurvNum) return 6;
471 // Find the reference layer
472 Int_t lay1 = p1->GetLayer();
473 Int_t lay2 = p2->GetLayer();
474 Int_t reflayer = (lay1 < lay2) ? lay1 : lay2;
476 // Swap points in order that r1 < r2
477 AliITSNode *temp = 0;
478 if (p2->GetLayer() < p1->GetLayer()) {
484 // shift XY coords to the reference to the vertex position,
485 // for easier calculus of quantities.
486 Double_t x1 = p1->X() - vx;
487 Double_t x2 = p2->X() - vx;
488 Double_t y1 = p1->Y() - vy;
489 Double_t y2 = p2->Y() - vy;
490 Double_t z1 = p1->Z() - vz;
491 Double_t z2 = p2->Z() - vz;
492 Double_t r1 = sqrt(x1*x1 + y1*y1);
493 Double_t r2 = sqrt(x2*x2 + y2*y2);
495 // Check for theta cut
496 Double_t dtheta, dtheta3;
497 TVector3 v01(z1, r1, 0.0);
498 TVector3 v12(z2 - z1, r2 - r1, 0.0);
499 dtheta = v01.Angle(v12) * 180.0 / TMath::Pi();
500 TVector3 vv01(x1, y1, z1);
501 TVector3 vv12(x2 - x1, y2 - y1, z2 - z1);
502 dtheta3 = vv01.Angle(vv12) * 180.0 / TMath::Pi();
503 if (dtheta < fThetaCut2DMin[reflayer] || dtheta > fThetaCut2DMax[reflayer]) return 1;
504 if (dtheta3 < fThetaCut3DMin[reflayer] || dtheta3 > fThetaCut3DMax[reflayer]) return 2;
506 // calculation of curvature
507 Double_t dx = x1 - x2, dy = y1 - y2;
508 Double_t num = 2 * (x1*y2 - x2*y1);
509 Double_t den = r1*r2*sqrt(dx*dx + dy*dy);
512 curv = TMath::Abs(num / den);
513 if (curv > fCurvCut[curvindex]) return 3;
518 // calculation of helix matching
519 Double_t arc1 = 2.0 * r1 * curv;
520 Double_t arc2 = 2.0 * r2 * curv;
521 Double_t helmatch = 0.0;
522 if (arc1 > -1.0 && arc1 < 1.0) arc1 = asin(arc1);
523 else arc1 = ((arc1 > 0.0) ? 0.5 : 1.5) * TMath::Pi();
524 if (arc2 > -1.0 && arc2 < 1.0) arc2 = asin(arc2);
525 else arc2 = ((arc2 > 0.0) ? 0.5 : 1.5) * TMath::Pi();
528 if (arc1 == 0.0 || arc2 == 0.0) return kFALSE;
529 helmatch = TMath::Abs(z1 / arc1 - z2 / arc2);
530 if (helmatch < fHelixMatchCutMin[reflayer] || helmatch > fHelixMatchCutMax[reflayer]) return 5;
535 //--------------------------------------------------------------------------------------------
537 void AliITSNeuralTracker::StoreAbsoluteMatches()
539 // Stores in the 'fMatches' array of each node all the points in the
540 // adjacent layers which allow to create neurons accordin to the
541 // helix and theta cut, and also to the largest curvature cut
543 Int_t ilayer, isector, itheta1, itheta2, check;
544 TObjArray *list1 = 0, *list2 = 0;
545 AliITSNode *node1 = 0, *node2 = 0;
546 Double_t thetamin, thetamax;
549 for (isector = 0; isector < fSectorNum; isector++) {
550 // sector is chosen once for both lists
551 for (ilayer = 0; ilayer < 5; ilayer++) {
552 for (itheta1 = 0; itheta1 < 180; itheta1++) {
553 list1 = (TObjArray*)fPoints[ilayer][itheta1]->At(isector);
554 TObjArrayIter iter1(list1);
555 while ( (node1 = (AliITSNode*)iter1.Next()) ) {
556 if (node1->GetUser() >= 0) continue;
557 node1->Matches()->Clear();
558 thetamin = node1->ThetaDeg() - fPolarInterval;
559 thetamax = node1->ThetaDeg() + fPolarInterval;
560 imin = (Int_t)thetamin;
561 imax = (Int_t)thetamax;
562 if (imin < 0) imin = 0;
563 if (imax > 179) imax = 179;
564 for (itheta2 = imin; itheta2 <= imax; itheta2++) {
565 list2 = (TObjArray*)fPoints[ilayer + 1][itheta2]->At(isector);
566 TObjArrayIter iter2(list2);
567 while ( (node2 = (AliITSNode*)iter2.Next()) ) {
568 check = PassAllCuts(node1, node2, fCurvNum - 1, fVX, fVY, fVZ);
571 node1->Matches()->AddLast(node2);
574 //Info("StoreAbsoluteMatches", "Layer %d: THETA 2D cut not passed", ilayer);
577 //Info("StoreAbsoluteMatches", "Layer %d: THETA 3D cut not passed", ilayer);
580 //Info("StoreAbsoluteMatches", "Layer %d: CURVATURE cut not passed", ilayer);
583 //Info("StoreAbsoluteMatches", "Layer %d: Division by ZERO in curvature evaluation", ilayer);
586 //Info("StoreAbsoluteMatches", "Layer %d: HELIX-MATCH cut not passed", ilayer);
589 //Error("PassAllCuts", "Layer %d: Curv index out of range", ilayer);
592 Warning("StoreAbsoluteMatches", "Layer %d: %d: unrecognized return value", ilayer, check);
594 } // while (node2...)
595 } // for (itheta2...)
596 } // while (node1...)
599 } // for (isector...)
602 //--------------------------------------------------------------------------------------------
604 void AliITSNeuralTracker::PrintMatches(Bool_t stop)
606 // Prints the matches for each point
608 TFile *ft = new TFile("its_findables_v1.root");
609 TTree *tt = (TTree*)ft->Get("Tracks");
610 Int_t it, nP, nU, lab, nF = (Int_t)tt->GetEntries();
611 tt->SetBranchAddress("nhitsP", &nP);
612 tt->SetBranchAddress("nhitsU", &nU);
613 tt->SetBranchAddress("label", &lab);
614 TString strP("|"), strU("|");
615 for (it = 0; it < nF; it++) {
617 if (nP >= 5) strP.Append(Form("%d|", lab));
618 if (nU >= 5) strU.Append(Form("%d|", lab));
621 TObjArray *sector = 0;
622 Int_t ilayer, isector, itheta, id[3];
623 AliITSNode *node1 = 0, *node2 = 0;
625 for (ilayer = 0; ilayer < 6; ilayer++) {
626 for (isector = 0; isector < fSectorNum; isector++) {
627 for (itheta = 0; itheta < 180; itheta++) {
628 sector = (TObjArray*)fPoints[ilayer][itheta]->At(isector);
629 TObjArrayIter points(sector);
630 while ( (node1 = (AliITSNode*)points.Next()) ) {
631 for (it = 0; it < 3; it++) id[it] = node1->GetLabel(it);
632 nF = (Int_t)node1->Matches()->GetSize();
633 cout << "Node layer: " << node1->GetLayer() << ", labels: ";
634 cout << id[0] << " " << id[1] << " " << id[2] << " --> ";
636 cout << "NO MatchES!!!" << endl;
639 cout << nF << " Matches" << endl;
640 for (it = 0; it < 3; it++) {
641 if (strP.Contains(Form("|%d|", id[it])))
642 cout << "Belongs to findable (originary) track " << id[it] << endl;
643 if (strU.Contains(Form("|%d|", id[it])))
644 cout << "Belongs to findable (post-Kalman) track " << id[it] << endl;
646 TObjArrayIter matches(node1->Matches());
647 while ( (node2 = (AliITSNode*)matches.Next()) ) {
648 cout << "Match with " << node2;
649 Int_t *sh = node1->SharedID(node2);
650 for (Int_t k = 0; k < 3; k++)
651 if (sh[k] > -1) cout << " " << sh[k];
655 cout << "Press a key" << endl;
664 //--------------------------------------------------------------------------------------------
666 void AliITSNeuralTracker::ResetNodes(Int_t isector)
668 // Clears some utility arrays for each node
670 Int_t ilayer, itheta;
671 TObjArray *sector = 0;
672 AliITSNode *node = 0;
673 for (ilayer = 0; ilayer < 6; ilayer++) {
674 for (itheta = 0; itheta < 180; itheta++) {
675 sector = (TObjArray*)fPoints[ilayer][itheta]->At(isector);
676 TObjArrayIter iter(sector);
678 node = (AliITSNode*)iter.Next();
680 node->InnerOf()->Clear();
681 node->OuterOf()->Clear();
682 delete node->InnerOf();
683 delete node->OuterOf();
684 node->InnerOf() = new TObjArray;
685 node->OuterOf() = new TObjArray;
691 //--------------------------------------------------------------------------------------------
693 void AliITSNeuralTracker::NeuralTracking(const char* filesave, TCanvas*& display)
695 // This is the public method that operates the tracking.
696 // It works sector by sector, and at the end saves the found tracks.
697 // Other methods are privare because they have no meaning id used alone,
698 // and sometimes they could get segmentation faults due to uninitialized
699 // datamembert they require to work on.
700 // The argument is a file where the final results have to be stored.
702 Bool_t isStable = kFALSE;
703 Int_t i, nUnits = 0, nLinks = 0, nTracks = 0, sectTracks = 0, totTracks = 0;
705 // tracking through sectors
708 for (sector = 0; sector < fSectorNum; sector++) {
709 cout << "\rSector " << sector << ": " << endl;
711 for(curv = 0; curv < fCurvNum; curv++) {
712 cout << "- curvature step " << curv + 1;
713 cout << " (" << fCurvCut[curv] << "): ";
715 nUnits = CreateNeurons(sector, curv);
717 cout << "no neurons --> skipped" << endl;
720 cout << nUnits << " units, " << flush;
722 nLinks = LinkNeurons();
724 cout << "no connections --> skipped" << endl;
727 cout << nLinks << " connections, " << flush;
731 if (display) Display(display);
732 TObjArrayIter iter(fNeurons);
734 AliITSneuron *n = (AliITSneuron*)iter.Next();
739 cout << i << " cycles --> " << flush;
742 nTracks = Save(sector);
743 cout << nTracks << " tracks" << endl;
744 sectTracks += nTracks;
746 totTracks += sectTracks;
747 //cout << sectTracks << " tracks found (total = " << totTracks << ") " << flush;
750 cout << endl << totTracks << " tracks found!!!" << endl;
751 cout << endl << "Saving results in file " << filesave << "..." << flush;
752 TFile *f = new TFile(filesave, "recreate");
753 fChains->Write("TreeC");
757 //--------------------------------------------------------------------------------------------
759 Int_t AliITSNeuralTracker::CreateNeurons(Int_t sectoridx, Int_t curvidx)
761 // Fills the neuron arrays, following the cut criteria for the selected step
762 // (secnum = sector to analyze, curvnum = curvature cut step to use)
763 // It also sets the initial random activation.
764 // In order to avoid a large number of 'new' operations, all existing neurons
765 // are reset and initialized with the new values, and are created new unit only if
766 // it turns out to be necessary
767 // the 'flag' argument is used to decide if the lower edge in the intevral
768 // of the accepted curvatures is given by zero (kFALSE) or by the preceding used cut (kTRUE)
769 // (if this is the first step, anyway, the minimum is always zero)
771 ResetNodes(sectoridx);
773 if (fNeurons) delete fNeurons;
774 fNeurons = new TObjArray;
776 AliITSneuron *unit = 0;
777 Int_t itheta, neurons = 0;
778 TObjArray *lstsector = 0;
781 Double_t vx[6], vy[6], vz[6];
782 AliITSNode *p[6] = {0, 0, 0, 0, 0, 0};
783 for (itheta = 0; itheta < 180; itheta++) {
784 lstsector = (TObjArray*)fPoints[0][itheta]->At(sectoridx);
785 TObjArrayIter lay0(lstsector);
786 while ( (p[0] = (AliITSNode*)lay0.Next()) ) {
787 if (p[0]->GetUser() >= 0) continue;
791 TObjArrayIter lay1(p[0]->Matches());
792 while ( (p[1] = (AliITSNode*)lay1.Next()) ) {
793 if (p[1]->GetUser() >= 0) continue;
794 if (!PassCurvCut(p[0], p[1], curvidx, fVX, fVY, fVZ)) continue;
795 unit = new AliITSneuron;
796 unit->Inner() = p[0];
797 unit->Outer() = p[1];
798 unit->Activation() = gRandom->Rndm() * (fEdge1 - fEdge2) + fEdge2;
799 unit->Gain() = new TObjArray;
800 fNeurons->AddLast(unit);
801 p[0]->InnerOf()->AddLast(unit);
802 p[1]->OuterOf()->AddLast(unit);
807 TObjArrayIter lay2(p[1]->Matches());
808 while ( (p[2] = (AliITSNode*)lay2.Next()) ) {
809 if (p[2]->GetUser() >= 0) continue;
810 if (!PassCurvCut(p[1], p[2], curvidx, vx[1], vy[1], vz[1])) continue;
811 unit = new AliITSneuron;
812 unit->Inner() = p[1];
813 unit->Outer() = p[2];
814 unit->Activation() = gRandom->Rndm() * (fEdge1 - fEdge2) + fEdge2;
815 unit->Gain() = new TObjArray;
816 fNeurons->AddLast(unit);
817 p[1]->InnerOf()->AddLast(unit);
818 p[2]->OuterOf()->AddLast(unit);
823 TObjArrayIter lay3(p[2]->Matches());
824 while ( (p[3] = (AliITSNode*)lay3.Next()) ) {
825 if (p[3]->GetUser() >= 0) continue;
826 if (!PassCurvCut(p[2], p[3], curvidx, vx[2], vy[2], vz[2])) continue;
827 unit = new AliITSneuron;
828 unit->Inner() = p[2];
829 unit->Outer() = p[3];
830 unit->Activation() = gRandom->Rndm() * (fEdge1 - fEdge2) + fEdge2;
831 unit->Gain() = new TObjArray;
832 fNeurons->AddLast(unit);
833 p[2]->InnerOf()->AddLast(unit);
834 p[3]->OuterOf()->AddLast(unit);
839 TObjArrayIter lay4(p[3]->Matches());
840 while ( (p[4] = (AliITSNode*)lay4.Next()) ) {
841 if (p[4]->GetUser() >= 0) continue;
842 if (!PassCurvCut(p[3], p[4], curvidx, vx[3], vy[3], vz[3])) continue;
843 unit = new AliITSneuron;
844 unit->Inner() = p[3];
845 unit->Outer() = p[4];
846 unit->Activation() = gRandom->Rndm() * (fEdge1 - fEdge2) + fEdge2;
847 unit->Gain() = new TObjArray;
848 fNeurons->AddLast(unit);
849 p[3]->InnerOf()->AddLast(unit);
850 p[4]->OuterOf()->AddLast(unit);
855 TObjArrayIter lay5(p[4]->Matches());
856 while ( (p[5] = (AliITSNode*)lay5.Next()) ) {
857 if (p[5]->GetUser() >= 0) continue;
858 if (!PassCurvCut(p[4], p[5], curvidx, vx[4], vy[4], vz[4])) continue;
859 unit = new AliITSneuron;
860 unit->Inner() = p[4];
861 unit->Outer() = p[5];
862 unit->Activation() = gRandom->Rndm() * (fEdge1 - fEdge2) + fEdge2;
863 unit->Gain() = new TObjArray;
864 fNeurons->AddLast(unit);
865 p[4]->InnerOf()->AddLast(unit);
866 p[5]->OuterOf()->AddLast(unit);
875 // END OF NEW VERSION
878 for (ilayer = 0; ilayer < 6; ilayer++) {
879 for (itheta = 0; itheta < 180; itheta++) {
880 lstsector = (TObjArray*)fPoints[ilayer][itheta]->At(sectoridx);
881 TObjArrayIter inners(lstsector);
882 while ( (inner = (AliITSNode*)inners.Next()) ) {
883 if (inner->GetUser() >= 0) continue;
884 TObjArrayIter outers(inner->Matches());
885 while ( (outer = (AliITSNode*)outers.Next()) ) {
886 if (outer->GetUser() >= 0) continue;
887 if (!PassCurvCut(inner, outer, curvidx, fVX, fVY, fVZ)) continue;
888 unit = new AliITSneuron;
889 unit->Inner() = inner;
890 unit->Outer() = outer;
891 unit->Activation() = gRandom->Rndm() * (fEdge1 - fEdge2) + fEdge2;
892 unit->Gain() = new TObjArray;
893 fNeurons->AddLast(unit);
894 inner->InnerOf()->AddLast(unit);
895 outer->OuterOf()->AddLast(unit);
903 fNeurons->SetOwner();
909 Int_t AliITSNeuralTracker::LinkNeurons() const
911 // Creates the neural synapses among all neurons
912 // which share a point.
915 TObjArrayIter iter(fNeurons), *testiter;
916 AliITSneuron *neuron = 0, *test = 0;
918 neuron = (AliITSneuron*)iter.Next();
921 testiter = (TObjArrayIter*)neuron->Inner()->OuterOf()->MakeIterator();
923 test = (AliITSneuron*)testiter->Next();
925 neuron->Add2Gain(test, fGain2CostRatio, fAlignExponent);
929 testiter = (TObjArrayIter*)neuron->Outer()->InnerOf()->MakeIterator();
931 test = (AliITSneuron*)testiter->Next();
933 neuron->Add2Gain(test, fGain2CostRatio, fAlignExponent);
943 Bool_t AliITSNeuralTracker::Update()
945 // A complete updating cycle with the asynchronous method
946 // when the neural network is stable, kTRUE is returned.
948 Double_t actVar = 0.0, totDiff = 0.0;
949 TObjArrayIter iter(fNeurons);
952 unit = (AliITSneuron*)iter.Next();
954 actVar = Activate(unit);
955 // calculation the relative activation variation
958 totDiff /= fNeurons->GetSize();
959 return (totDiff < fStabThreshold);
964 void AliITSNeuralTracker::CleanNetwork()
966 // Removes all deactivated neurons, and resolves the competitions
967 // to the advantage of the most active unit
969 AliITSneuron *unit = 0;
970 TObjArrayIter neurons(fNeurons);
971 while ( (unit = (AliITSneuron*)neurons.Next()) ) {
972 if (unit->Activation() < fActMinimum) {
973 unit->Inner()->InnerOf()->Remove(unit);
974 unit->Outer()->OuterOf()->Remove(unit);
975 delete fNeurons->Remove(unit);
981 AliITSneuron *enemy = 0;
983 while ( (unit = (AliITSneuron*)neurons.Next()) ) {
984 nIn = (Int_t)unit->Inner()->InnerOf()->GetSize();
985 nOut = (Int_t)unit->Outer()->OuterOf()->GetSize();
986 if (nIn < 2 && nOut < 2) continue;
989 TObjArrayIter competing(unit->Inner()->InnerOf());
990 while ( (enemy = (AliITSneuron*)competing.Next()) ) {
991 if (unit->Activation() > enemy->Activation()) {
992 enemy->Inner()->InnerOf()->Remove(enemy);
993 enemy->Outer()->OuterOf()->Remove(enemy);
994 delete fNeurons->Remove(enemy);
997 unit->Inner()->InnerOf()->Remove(unit);
998 unit->Outer()->OuterOf()->Remove(unit);
999 delete fNeurons->Remove(unit);
1004 if (removed) continue;
1007 TObjArrayIter competing(unit->Outer()->OuterOf());
1008 while ( (enemy = (AliITSneuron*)competing.Next()) ) {
1009 if (unit->Activation() > enemy->Activation()) {
1010 enemy->Inner()->InnerOf()->Remove(enemy);
1011 enemy->Outer()->OuterOf()->Remove(enemy);
1012 delete fNeurons->Remove(enemy);
1015 unit->Inner()->InnerOf()->Remove(unit);
1016 unit->Outer()->OuterOf()->Remove(unit);
1017 delete fNeurons->Remove(unit);
1028 Bool_t AliITSNeuralTracker::CheckOccupation() const
1030 // checks if a track recognized has a point for each layer
1032 for (i = 0; i < 6; i++) if (fPoint[i] < 0) return kFALSE;
1038 Int_t AliITSNeuralTracker::Save(Int_t sectorid)
1040 // Creates chains of active neurons, in order to
1041 // find the tracks obtained as the neural network output.
1043 // every chain is started from the neurons in the first 2 layers
1044 Int_t i, check, stored = 0;
1045 Int_t a = sectorid; a = 0;
1046 Double_t testact = 0;
1047 AliITSneuron *unit = 0, *cursor = 0, *fwd = 0;
1048 AliITSNode *node = 0;
1049 TObjArrayIter iter(fNeurons), *fwditer;
1050 TObjArray *removedUnits = new TObjArray(0);
1051 TObjArray *removedPoints = new TObjArray(6);
1052 removedUnits->SetOwner(kFALSE);
1053 removedPoints->SetOwner(kFALSE);
1055 for (i = 0; i < 6; i++) fPoint[i] = -1;
1056 unit = (AliITSneuron*)iter.Next();
1058 if (unit->Inner()->GetLayer() > 0) continue;
1059 fPoint[unit->Inner()->GetLayer()] = unit->Inner()->PosInTree();
1060 fPoint[unit->Outer()->GetLayer()] = unit->Outer()->PosInTree();
1061 node = unit->Outer();
1062 removedUnits->AddLast(unit);
1063 removedPoints->AddAt(unit->Inner(), unit->Inner()->GetLayer());
1064 removedPoints->AddAt(unit->Outer(), unit->Outer()->GetLayer());
1066 testact = fActMinimum;
1067 fwditer = (TObjArrayIter*)node->InnerOf()->MakeIterator();
1070 cursor = (AliITSneuron*)fwditer->Next();
1072 if (cursor->Used()) continue;
1073 if (cursor->Activation() >= testact) {
1074 testact = cursor->Activation();
1079 removedUnits->AddLast(fwd);
1080 node = fwd->Outer();
1081 fPoint[node->GetLayer()] = node->PosInTree();
1082 removedPoints->AddAt(node, node->GetLayer());
1085 for (i = 0; i < 6; i++) if (fPoint[i] >= 0) check++;
1089 for (i = 0; i < 6; i++) {
1090 node = (AliITSNode*)removedPoints->At(i);
1093 fwditer = (TObjArrayIter*)removedUnits->MakeIterator();
1095 cursor = (AliITSneuron*)fwditer->Next();
1105 Int_t AliITSNeuralTracker::Save(Int_t sectoridx)
1106 // Creates chains of active neurons, in order to
1107 // find the tracks obtained as the neural network output.
1109 // Reads the final map of neurons and removes all
1110 // units with too small activation
1111 cout << "saving..." << flush;
1113 // every chain is started from the neurons in the first 2 layers
1114 // 00111111 00011111 00101111 00110111
1115 Int_t allowmask[] = { 0x3F, 0x1F, 0x2F, 0x37,
1117 // 00111011 00111101 00111110
1119 Double_t test_fwd = 0., testback = 0.;
1120 Int_t ilayer, itheta;
1121 AliITSNode *node = 0;
1122 AliITSneuron *fwd = 0, *back = 0;
1123 TList *listsector = 0;
1125 cout << "A -" << fActMinimum << "-" << flush;
1126 for (ilayer = 0; ilayer < 6; ilayer++) {
1127 for (itheta = 0; itheta < 180; itheta++) {
1128 listsector = (TList*)fPoints[ilayer][itheta]->At(sectoridx);
1129 TListIter iter(listsector);
1130 while ( (node = (AliITSNode*)iter.Next()) ) {
1131 TListIter fwditer(node->InnerOf());
1132 TListIter backiter(node->OuterOf());
1133 testfwd = testback = fActMinimum;
1134 while ( (fwd = (AliITSneuron*)fwditer.Next()) ) {
1135 if (fwd->Activation() > testfwd) {
1136 testfwd = fwd->Activation();
1137 node->fNext = fwd->Outer();
1140 while ( (back = (AliITSneuron*)backiter.Next()) ) {
1141 if (back->Activation() > testback) {
1142 testback = back->Activation();
1143 node->fPrev = back->Inner();
1150 cout << "B" << flush;
1151 for (ilayer = 0; ilayer < 5; ilayer++) {
1152 for (itheta = 0; itheta < 180; itheta++) {
1153 listsector = (TList*)fPoints[ilayer][itheta]->At(sectoridx);
1154 TListIter iter(listsector);
1155 while ( (node = (AliITSNode*)iter.Next()) ) {
1157 if (node->fNext->fPrev != node) node->fNext = 0;
1163 cout << "C" << flush;
1165 Int_t stored = 0, l1, l2, i, mask, im;
1166 AliITSNode *temp = 0;
1168 for (itheta = 0; itheta < 180; itheta++) {
1169 list[0] = (TList*)fPoints[0][itheta]->At(sectoridx);
1170 list[1] = (TList*)fPoints[1][itheta]->At(sectoridx);
1171 for (i = 0; i < 2; i++) {
1172 TListIter iter(list[i]);
1173 while ( (node = (AliITSNode*)iter.Next()) ) {
1174 //cout << endl << node << flush;
1175 AliITSneuralChain *chain = new AliITSneuralChain;
1176 for (temp = node; temp; temp = temp->fNext) {
1177 chain->Insert((AliITSNeuralPoint*)temp);
1180 mask = chain->OccupationMask();
1181 for (im = 0; im < 7; im++) ok = ok || (mask == allowmask[im]);
1186 //cout << " lab " << flush;
1187 chain->AssignLabel();
1188 //cout << " add " << flush;
1189 fTracks->AddLast(chain);
1191 //cout << " " << stored << flush;
1196 cout << "D" << flush;
1203 Double_t AliITSNeuralTracker::Activate(AliITSneuron* &unit)
1205 // Computes the new neural activation and
1206 // returns the variation w.r.t the previous one
1208 if (!unit) return 0.0;
1209 Double_t sumgain = 0.0, sumcost = 0.0, input, actOld, actNew;
1211 // sum gain contributions
1212 TObjArrayIter *iter = (TObjArrayIter*)unit->Gain()->MakeIterator();
1215 link = (AliITSlink*)iter->Next();
1217 sumgain += link->Linked()->Activation() * link->Weight();
1220 // sum cost contributions
1221 TObjArrayIter *testiter = (TObjArrayIter*)unit->Inner()->InnerOf()->MakeIterator();
1222 AliITSneuron *linked = 0;
1224 linked = (AliITSneuron*)testiter->Next();
1226 if (linked == unit) continue;
1227 sumcost += linked->Activation();
1230 testiter = (TObjArrayIter*)unit->Outer()->OuterOf()->MakeIterator();
1232 linked = (AliITSneuron*)testiter->Next();
1234 if (linked == unit) continue;
1235 sumcost += linked->Activation();
1238 //cout << "gain = " << sumgain << ", cost = " << sumcost << endl;
1240 input = (sumgain - sumcost) / fTemperature;
1241 actOld = unit->Activation();
1242 #ifdef NEURAL_LINEAR
1243 if (input <= -2.0 * fTemperature)
1245 else if (input >= 2.0 * fTemperature)
1248 actNew = input / (4.0 * fTemperature) + 0.5;
1250 actNew = 1.0 / (1.0 + TMath::Exp(-input));
1252 unit->Activation() = actNew;
1253 return TMath::Abs(actNew - actOld);
1258 void AliITSNeuralTracker::AliITSneuron::Add2Gain(AliITSneuron *n, Double_t multconst, Double_t exponent)
1260 // Adds a neuron to the collection of sequenced ones of another neuron
1262 AliITSlink *link = new AliITSlink;
1264 Double_t weight = Weight(n);
1265 link->Weight() = multconst * TMath::Power(weight, exponent);
1266 fGain->AddLast(link);
1271 Double_t AliITSNeuralTracker::AliITSneuron::Weight(AliITSneuron *n)
1273 // computes synaptic weight
1274 TVector3 me(Outer()->X() - Inner()->X(), Outer()->Y() - Inner()->Y(), Outer()->Z() - Inner()->Z());
1275 TVector3 it(n->Outer()->X() - n->Inner()->X(), n->Outer()->Y() - n->Inner()->Y(), n->Outer()->Z() - n->Inner()->Z());
1277 Double_t angle = me.Angle(it);
1278 Double_t weight = 1.0 - sin(angle);
1287 AliITSNeuralTracker::AliITSNode::AliITSNode(const AliITSNode &t): AliITSNeuralPoint((AliITSNeuralPoint&)t),
1288 fPosInTree(t.fPosInTree),
1289 fInnerOf(t.fInnerOf),
1290 fOuterOf(t.fOuterOf),
1291 fMatches(t.fMatches),
1298 AliITSNeuralTracker::AliITSNode& AliITSNeuralTracker::AliITSNode::operator=(const AliITSNode& t)
1300 // assignment operator
1301 this->~AliITSNode();
1302 new(this) AliITSNode(t);
1307 AliITSNeuralTracker::AliITSneuron::AliITSneuron(const AliITSneuron &t)
1308 : TObject((TObject&)t),
1310 fActivation(t.fActivation),
1318 AliITSNeuralTracker::AliITSneuron& AliITSNeuralTracker::AliITSneuron::operator=(const AliITSneuron& t)
1320 //assignment operator
1321 this->~AliITSneuron();
1322 new(this) AliITSneuron(t);
1327 AliITSNeuralTracker::AliITSlink::AliITSlink(const AliITSlink &t)
1328 : TObject((TObject&)t),
1335 AliITSNeuralTracker::AliITSlink& AliITSNeuralTracker::AliITSlink::operator=(const AliITSlink& t)
1337 // assignment operator
1338 this->~AliITSlink();
1339 new(this) AliITSlink(t);