/*---------------------------------------------
                   CLUSTERISING PROCEDURES
  ---------------------------------------------*/

objectvar clusterVect         // Vector for the i-th cluster, containing the id-sections 
objectvar clusterParentVect   // Vector containing the parent sections for the i-th cluster
objectvar nclusterVect        // Vector containing the number of sections for each cluster
objectvar cutOffVect          // Vector containing the cutOff-values to clusterise


proc clusteriseAleFunz1(){local ii,icluster,sec
	clusterParent = new Vector(9,0)
	clusterParentPos = new Vector(9,0)

    // the first cluster is the soma and the axon
    clusterList.append(somaVector) 
    clusterList.append(axonVector)     
            
    nclusterVect = new Vector(9,0)

    for ii=0,somaVector.size()-1{
	  clusterMarker.set(somaVector.x[ii], 0)
	  nclusterVect.x(0)=nclusterVect.x(0)+1        
    }
    for ii=0,axonVector.size()-1{
	  clusterMarker.set(axonVector.x[ii], 1)
	  nclusterVect.x(1)=nclusterVect.x(1)+1        
    }

    access soma[0]
    distance()

    sec = 0    
    forall {
           xdist = distance(.5)
           if (issection("user5.*")&& xdist<=100) {
        	  clusterMarker.set(sec, 2)
        	  nclusterVect.x(2)=nclusterVect.x(2)+1              
           }else if (issection("user5.*")&& xdist>100) {
        	  clusterMarker.set(sec, 3)
        	  nclusterVect.x(3)=nclusterVect.x(3)+1              
           }else if (issection("apical_dendrite.*")&& xdist<=100) {
        	  clusterMarker.set(sec, 4)
        	  nclusterVect.x(4)=nclusterVect.x(4)+1              
           }else if (issection("apical_dendrite.*")&& xdist>100 && xdist<=300) {
        	  clusterMarker.set(sec, 5)
        	  nclusterVect.x(5)=nclusterVect.x(5)+1              
           }else if (issection("apical_dendrite.*")&& xdist>300) {
        	  clusterMarker.set(sec, 6)
        	  nclusterVect.x(6)=nclusterVect.x(6)+1              
           }else if (issection("dendrite.*")&& xdist<=100) {
        	  clusterMarker.set(sec, 7)
        	  nclusterVect.x(7)=nclusterVect.x(7)+1              
           }else if (issection("dendrite.*")&& xdist>100) {
        	  clusterMarker.set(sec, 8)
        	  nclusterVect.x(8)=nclusterVect.x(8)+1              
           }                           
           sec = sec+1
    }
       
    if (nclusterVect.x(4)==0){
                   
        sec = 0
        secSel = 0    
        minDist = 300
        forall {
               xdist = distance(.5)
               if (issection("apical_dendrite.*")&& xdist>100 && xdist<=300 && xdist<minDist){
                  minDist = xdist
                  secSel = sec
               }
               sec = sec+1
        }
        nclusterVect.x(4) = nclusterVect.x(4)+1 
        nclusterVect.x(5) = nclusterVect.x(5)-1 
        clusterMarker.set(secSel, 4)
    }    
    
    
    clusterParent.set(1,0)
    clusterParentPos.set(1,1)    
    clusterParent.set(2,0)
    clusterParentPos.set(2,1)     
    clusterParent.set(3,2)
    clusterParentPos.set(3,1)
    if (nclusterVect.x(4)>1){
        clusterParent.set(4,2)
        clusterParentPos.set(4,0.5)
        clusterParent.set(5,4)
        clusterParentPos.set(5,1) 
    }else{
        clusterParent.set(4,2)
        clusterParentPos.set(4,0.5)
        clusterParent.set(5,3)
        clusterParentPos.set(5,0.2)     
    
    }
    clusterParent.set(6,3)
    clusterParentPos.set(6,0.5)    
    clusterParent.set(7,0)
    clusterParentPos.set(7,0)     
    clusterParent.set(8,7)
    clusterParentPos.set(8,1)    
    
  
    for ii=2,8{
        clusterVect = new Vector(nclusterVect.x(ii),0)
        icluster=0
        for sec=1,NSEC-1{
            if (clusterMarker.x(sec)==ii){clusterVect.set(icluster, sec)  icluster=icluster+1}
        }
        
        if (icluster>0) {
            clusterList.append(clusterVect)
        }
    }  
        
    
}