The overall effect was a little like going to the gym with his brain. He did not notice any manifest change at first, but by the end of it he found himself with new powers of observation and retention.

“They really fine-tuned skills I never thought I had,” he says.

He was then ready to join part of a larger intelligence and counterterrorism effort that Commissioner Raymond Kelly had initiated in the aftermath of 9/11. The NYPD had learned that it could not count on the federal government to protect the city of New York.

That long and unnerving lesson had begun after the assassination of Rabbi Meir Kahane of the Jewish Defense League in the ballroom of a Manhattan hotel in 1990. City detectives afterward seized two file cabinets from the Brooklyn apartment of a prime suspect. The detectives transported the evidence to their squad room and stepped out for dinner. They returned to discover that the FBI had taken the cabinets before they could study what was inside. The contents included drawings of the World Trade Center and a paper bearing the words “al Qaeda.”

But the FBI did not get around to translating the stuff in the files until after the first bombing of the World Trade Center in 1993, during Kelly’s first tenure as police commissioner. The bombers included at least one of the conspirators in the Kahane killing and several other militants long known to the FBI. An informant had alerted the FBI that these individuals were in the midst of a major bomb plot. There is some speculation that the bombing was carried out with a timing device supplied by the informant. There is no disputing that an FBI supervisor ordered the agents to break off contact with the informant shortly before the bomb was readied.

“And then oops! It went off,” the informant afterward told an agent.

When the towers were attacked again eight years later, city cops were struck not by how much the FBI had known but how little, despite receiving a number of significant leads. Kelly began his second tenure as police commissioner three months after 9/11, and he was understandably reluctant to place his city’s fate in the hands of the FBI, even though city detectives had long been embedded with federal agents in a Joint Terrorism Task Force.

Kelly decided the NYPD needed its own intelligence and counterterrorism capability. And to that end he telephoned David Cohen, who had retired after 35 years with the CIA, at one point serving as the agency’s senior official in New York and at another as its deputy director for operations. Kelly now invited Cohen to become the NYPD’s deputy commissioner for intelligence.

“You would not say no to Commissioner Kelly,” Cohen says.

Kelly and Cohen agreed that they first had to determine as best they could the nature and magnitude of this new threat. They had no idea if other al Qaeda cells were out there ready to strike.

“Everybody knew nothing and had a lot to learn,” Cohen recalls. “It was a whole new world.”

As the result of a federal lawsuit filed on behalf of a group of civil rights activists who felt unfairly targeted by police more than four decades ago, the NYPD had entered into what were called the Handschu Guidelines. Only a small unit within the intelligence division was permitted to investigate political activity and then only when it had specific information that a person or group “is engaged in, about to engage in or has threatened to engage in conduct which constitutes a crime.” There were also tight restrictions on surveillance, information gathering, and the use of undercovers.

Cohen now submitted to the court a formal declaration arguing that when it accepted the guidelines “the NYPD had no idea of the problems it would face in protecting this city and its people from international terrorism.” He argued that the criminal activity requirement “may effectively shield from discovery the lawful preparatory activities which invariably precede terrorist attacks.”

“In the case of terrorism, to wait for an indication of crime before investigating it is to wait far too long,” he wrote.

He later noted of his written declaration, “It’s not like this was a secret document. This was a public document.”

The same federal judge who had presided over the original settlement agreed to Modified Handschu Guidelines in keeping with rules governing the FBI that had been relaxed after 9/11. The police could now initiate an investigation with information that served only to “reasonably indicate” the possibility of a future crime.

“You use the authorities you’re given,” Cohen says.

He started with a basic principle: “There is no guarantee of success, but there has to be a guarantee of effort.”

Cohen established a Demographics Unit—later renamed the Zone Assessment Unit—that used U.S. Census data to map what he called the New New York. Everybody already knew there were Chinese in Chinatown and Italians in Little Italy. The question now was where were the equivalents for the various Muslim ethic groups? That would help answer questions about those who might be hoping to mount new terror attacks.

“Where would they go to blend in? … To try to recruit people?” Cohen says.

The unit noted the location of mosques and Internet cafés and other gathering places. Detectives recruited informants who sometimes proved as unreliable and problematic as they are in narcotics or any other area of law enforcement. Informants generally are motivated by money or the hope of a reduced sentence or both.

“But undercovers do it because they take an oath,” a longtime member of the intelligence division notes.

An oath, and a sense of purpose that propelled the young undercover who had taken the NYPD entrance exam with dreams of ESU and lights and sirens. He instead found himself shooting hoops alone in a New Jersey park. He had been told that the area was frequented by Islamist extremists who were often seen in New York. He would learn only later that two in particular had repeatedly spoken of killing Americans and were pals with Samir Khan, editor of al Qaeda’s online magazine, Inspire. The undercover was not even given photos beforehand.

“As soon as you see them, you’ll know right away” is all he remembers being told as he set out.

He did indeed, and he just kept shooting hoops until some associates of the two main targets approached. He explained that he was new to the neighborhood. He could only hope that it was not obvious how new he also was to working undercover.

“Heart pounding,” he recalls.

He became ever more comfortable in the role as he passed one year after another after another living in an undercover apartment. He watched the news rather than cop shows so he could keep up with current events. He had to give up White Castle hamburgers because they aren’t halal.

“I was a big White Castle guy,” he laments.

He lost his girlfriend after he had to say one time too many that he could not see her because he had to work.

“You have to keep telling yourself, ‘This is for the greater good,’” he says.

“They had full beards, camo pants, and they’re talking the talk,” the undercover recalls. “I thought, ‘This is it!’”

Three years before, a local resident had sent a tip via an FBI website that Alessa, a U.S. citizen with Palestinian and Jordanian parents, and Almonte, a naturalized U.S. citizen from the Dominican Republic, were forever watching videos about holy war and saying Americans and non-Islamists were the enemy “and they all must be killed.” The FBI had interviewed Almonte and conducted a consensual search of his computer, which contained writings by Osama bin Laden. The encounter no doubt had left the two highly suspicious, and the undercover was apparently right in sensing that he had been watched all along.

“They saw me do my laundry, they saw me buy my groceries,” he says.

The two now told the undercover that they had heard a lot about him and that he was said to be a good guy.

“Thank you, where are you going with this?” the undercover remembers asking.

He surmised that the two were testing him when they began to speak openly about jihad. They likely would have expected an informant or an undercover to encourage such talk. He instead cautioned them to be more discreet.

“Look, guys, you can’t be talking about this publicly,” he recalls saying.

He pointed to somebody standing nearby.

“For all I know, that guy over there is a cop,” he remembers telling them.

He himself did not even grow a beard until after he was asked why he did not have one. He explained that his did not grow in well and he was told to go ahead and grow one anyway. He then had to lie to his parents.

“It’s only a bet at work,” he told them.

Alessa and Almonte further urged him to dress more in keeping with their views.

“Lose the shirt,” he remembers them telling him. “Wear this robe.”

They advised him to rub olive oil into his hair to make it grow better.

“I used to go home and shower, trying to get it off,” he says. “I smelled like food all day.”

He was invited to a house where he would be instructed to remove the battery from his cellphone and leave the phone while he went into the next room. The two talked to the undercover of jihad and played jihadi videos, including attacks on American troops. The videos also included an interview with Anwar al-Awlaki, in which he sought to justify the killing of civilians while waging jihad. Awlaki seemed a particular favorite because he was American-born and yet was very forthright about his hatred for the U.S.

“They loved that,” the undercover says.

During a car ride, Almonte played for the undercover a cellphone video showing Awlaki delivering a lecture on “Constants on the Path to Jihad.” In it Awlaki said a person does not have to wait for a leader and can undertake jihad on his own initiative.

On another ride, Alessa played a video of explosions and automatic weapons fire and screams. A voiceover in Arabic called for violent jihad. Alessa and Almonte gave the undercover tapes of Awlaki sermons that he played in his undercover apartment.

What seems to have upset him the most was watching Alessa and Almonte radicalize a local teenager he calls “a regular street kid.”

“They saw he wasn’t into anything, not too many friends,” the undercover says. “That’s what these guys feed on.”

The two filled the void with talk of jihad.

“It really gave him a purpose,” the undercover says. “He belonged to something.”

The teen was now a jihadi.

“I saw this happen in front of me,” the undercover says.

Alessa and Almonte told the undercover they had gone to Jordan in early 2007 hoping to sign up for jihad but had been turned away. They now planned to go to Somalia and join the terrorist group al-Shabaab.

“We’ll start doing killing here, if I can’t do it over there,” Alessa was recorded saying.

Alessa also said, “They only fear you when you have a gun and when you, when you start killing them and when you take their head, and you go like this, and you behead it on camera … My soul cannot rest until I shed blood.”

He voiced a twisted kind of jealousy of Nidal Hasan, who shot and killed 13 at Fort Hood in November 2009.

“Freaking Major-Nidal-shaved-face-Palestinian-crazy guy, he’s not better than me,” Almonte said. “I’ll do twice what he did. I wanna, like, be the world’s [best] known terrorist.”

Almonte was recorded as saying: “I’m gonna get a gun. I’m the type of person to use it anytime … I’ll have more bodies on it than … the hairs on my beard. You know what I’m saying? It’s already enough that you don’t worship Allah, so … that’s a reason for you to die.”

The undercover listened to such rantings and managed to stay in character.

“You’re not really their friend, but you have to act like they are,” he says.

The two invited the undercover to come to Somalia with them. He joined them in the conditioning they said was needed to prepare, hiking in T-shirts up a snowy New Jersey mountain, simulating combat at a paintball range, and lifting weights in a gym. Alessa proposed that more muscles translate into the ability to kill more non-Muslims. He indicated he would be happy to wage jihad if sent back to America as others had been.

“Only way I would come back here is if I was in the land of jihad and the leader ordered me to come here and do something here,” he was recorded saying. “Ah, I love that.”

The undercover says he remained careful not to encourage the two or facilitate their plans in any way. He did not want to make them suspicious or open himself up to accusations of entrapment later on.

“I had to let them lead the way,” he says.

He did not offer advice even when they asked for it.

“Listen, man, I have no idea,” he recalls telling them.

On June 5, 2010, the undercover drove with Alessa and Almonte to John F. Kennedy International Airport. The two were arrested as they prepared to fly off to Somalia. Both pleaded guilty in April 2013 to conspiracy. Alessa, then 23, was sentenced to 22 years in prison. Almonte, 27, got 20 years.

For the undercover, the closing of the case meant the end of five years of living a double life. His superiors said he could now tell his family, and they suggested he start with his sister.

He asked her to meet him at a restaurant so he could tell her something very important about himself. She arrived to find him with Cohen and an NYPD chief. They informed her that her brother was a police officer.

“Oh my God, I thought you were going to tell me you were gay,” the sister exclaimed.

The parents were next. They were told in the same way at the same restaurant.

“They were in tears,” the undercover says. “Tears of joy.”

The father gave him an affectionate smack on the back of the head.

“You couldn’t tell me?” the father said. “Come on.”

The undercover son finally got a shield.

“You always knew you’re a part of the department, but now it makes it real,” he says.

He could flash his shield to a fellow cop and say he is on The Job.

“Now I’m waiting to get pulled over, but it hasn’t happened,” he says.

He no longer had to listen to Awlaki tapes at home. He watched TV shows such as Homeland instead.

“That’s a good one,” he says.

The undercover now became a handler. He continued serving in the NYPD intelligence division as it came under harsh criticism by Associated Press reporters Matt Apuzzo and Adam Goldman, first in articles that won a Pulitzer Prize, more recently in the book Enemies Within.

The book centers on the case of Najibullah Zazi, an Afghan-American who brought bomb-making materials to New York from Colorado just before the eighth anniversary of 9/11, intending to join two former high school pals in mounting an attack in the subway. He pleaded guilty to conspiracy in February 2010.

The authors seek to make something of a hero of Don Borelli, the FBI assistant special agent in charge of the Joint Terrorist Task Force in New York, and something of a boob of Cohen. Zazi and his pals were just the kind of people Cohen’s outfit hoped to detect, and the book makes much of the cops’ failure to do so in this instance.

“New Yorkers had no idea they were paying for something that, at the most important moment, had proven useless,” the book says.

But in the telling of Zazi’s drive to his home neighborhood of Flushing, Queens, the book unintentionally demonstrates a significant lapse by the FBI.

The book suggests that “the easiest and most logical route to Queens was to pass through the Island of Manhattan,” via the Holland Tunnel or the Lincoln Tunnel. The reality is that folks from Flushing generally loathe driving through Manhattan traffic and are most likely to take the George Washington Bridge and not one of the tunnels, as the FBI reportedly supposed. The book quotes the out-of-towner reaction at the FBI command center when Zazi took the route that almost anybody from Flushing would take.

“Oh, shit!”

“He’s going to the bridge! He’s going to the bridge!”

The FBI had arranged for the Port Authority Police to make apparently random stops at both tunnels when Zazi approached. But there seem to have been no equivalent preparations at the George Washington Bridge. Zazi rightly sensed that he was being singled out when he was stopped. A cop walked around the car with a dog but was unaware of the exact nature of the investigation. Zazi was cleared to go.

“They fucking let explosives into New York City,” Cohen says of the FBI-led effort.

For its part, the FBI—along with the book—suggests that the NYPD subsequently screwed up by showing photos of Zazi and his pals to an imam informant who then alerted Zazi’s family. Cohen notes that this was after Zazi had decided to abandon the plot and discarded the bomb makings. Cohen adds that the photos were provided by the feds.

“Once you give them to us, you expect us to use them,” he says.

But in what some view as scapegoating as well as an implicit admission that there had been a screw-up, a high-ranking and well-respected supervisor in the NYPD intelligence division was transferred to the highway division. Cohen says the NYPD was only trying to dampen the controversy.

“We just wanted to lower the heat,” he says.

A senior NPYD official suggests that much of the heat from the FBI derives less from any particular case than from the very idea that cops would dare challenge its “monopoly” in domestic intelligence.

“If you’re not under their control, then you’re out of control,” the official says.

Among its many complaints in Enemies Within, the FBI says the NYPD conducted a yearlong investigation into a would-be jihadi named Abdel Hameed Shehadeh but had not made the feds aware of him until he was on his way to the airport for a flight to Pakistan and terror training. Cohen says a newbie undercover had only been on the case for about a month and the NYPD wanted to vet his reports before passing on any information. Cohen insists the NYPD alerted the FBI the moment it knew Shehadeh was about to depart. Cohen suggests the NYPD deserves credit, not blame for having identified someone who might otherwise have returned from Pakistan to mount an attack.

The book suggests the NYPD’s posting of detectives in faraway countries was at best a waste of resources. Cohen says it raised the department’s “global stature.” The book reports that the demographics unit never made a single case. Cohen reports that it was never intended to do anything but map information such as the AP itself is supposed to have requested after the Boston Marathon bombing. An NYPD official says an AP reporter called to ask where people of Chechen descent might congregate in New York City.

Cohen and Kelly contend that Enemies Within is filled with errors and “half truths.” The authors have posted online numerous NYPD documents that they say support their claims. A number of former members of the intelligence division say some of the criticisms have merit, particularly those purporting excessive use of informants and its chilling effect on the Muslim community. Cohen insists the NYPD has remained within the court-approved guidelines and has done nothing beyond the bounds of law or of ethics. He says Kelly would have sent him packing otherwise.

“He would not have allowed me to do anything illegal,” Cohen says. “He would have had me out of here in a second.”

Cohen adds, “Just because I’m an analyst doesn’t mean I’m stupid.”

A recent lawsuit filed in federal court alleges that the NYPD has engaged in “an unlawful practice of religious profiling.” The NYPD strenuously denies the allegation, just as it did when another suit alleged that the NYPD’s stop-and-frisk policies constituted racial profiling. A judge agreed regarding stop-and-frisk. The city has appealed, noting that even the plaintiffs acknowledge that 90 percent of the stops were legitimate.

Just as the NYPD contends that stop-and-frisk is a vital part of an effort that has reduced the murder rate in New York to a record low, it insists that the efforts of the intelligence division have been essential to keeping the city from being hit by another successful terrorist attack despite being targeted by at least 16 plots.

“Public safety and security,” Cohen says of the overall result of the NYPD’s effort under Kelly. “Both from terrorism and street crime.”

In describing the continuing threat from terrorism, Cohen notes that while al Qaeda has been significantly downgraded as an organization, it has evolved into an ideology that is spread by the Internet and difficult to counter. He mentions the continued online presence of Awlaki and Khan’s Inspire, despite the two having been “droned” in Yemen in 2011. Cohen refrained from noting that the FBI had cause to arrest Awlaki on prostitution charges back in November 2001 but let him slip away, escaping arrest and disgrace to become an Internet immortal. Cohen warned that the city and the country cannot afford to become complacent in the face of the continued threat.

“We can’t go to sleep,” he says.

With the arrival of a new mayor in January, Kelly and Cohen are likely to be replaced. Those who will remain include the undercover, a patriot of his country and his city who says he has no regrets about giving up five years of his life.

“You have to keep telling yourself, ‘It’s for the greater good,’” he says. “I accomplished what I set out to do.”

He met a new young woman and proposed to her. She said yes despite where he had taken her on an early date.